Results 1 to 15 of 15

Thread: [RESOLVED] RicTextBox - Formatting with Change in Font

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Resolved [RESOLVED] RicTextBox - Formatting with Change in Font

    When loading in a previously formatted document (text file) created by someone else, the document may contain vbCRLF, vbLF, etc.
    If the RTB Font Size used is the same as or close to the Font Size in which the document was created it displays correctly.

    However, if a larger font size is needed and used for displaying the document, the formatting may be an issue.

    Is there a BEST way to handle this (e.g. strip out all vbCRLF and make them just vbCR) ???

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: RicTextBox - Formatting with Change in Font

    You don't. the RTF actually doesn't see Cr/LF vs CR ... it simply has a /p which is an end of paragraph. That would be a hard carriage return... a soft one (like a Shift+Enter) I think might be a /n .... BUT it's up to the rendering control to then interpret that correctly. Create an RTF with the different line breaks, then open it in a text editor. you'll see the differences.

    Sorry - it's \para and \line for the two types of returns.
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 Test line 1 - hard return\par
    test line 2 with \line soft return. then a hard one\par
    and yet one more line.\par
    \par
    }
    And I believe, I can turn all of that all on one line and it will still render just fine.

    Yup... and indeed that is the case:
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 Test line 1 - hard return\par test line 2 with \line soft return. then a hard one\par and yet one more line.\par \par
    }

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: RicTextBox - Formatting with Change in Font

    techgnome:

    Thanks for responding.
    Boy am I confused.

    1)
    For test purposes I'm using a book -- "Tale of Two Cities" by Charles Dickens.
    In examining this with a hex editor, I see the normal OD OA (vbCRLF).
    Now if the Font Size is say 9, the book displays correctly, because the line breaks occur within the RTB prior to encountering the RTB right width (FWIW MultiLine = True).
    Now if the Font Size is increased to 40, then the Font encounters the RTB right width prior to encountering the vbCRLF, so because of Multiline (I assume) the text
    is automatically wrapped to the next line where the vbCRLF is then encountered and causes the new line to start at the RTB left margin.

    So I'm NOT sure where your getting the \line and \par values and how I would go about seeing them -- unless -- you've saved the file as an RTF and then examined that file.

    So if I can load the text file in, then save it as an RTF and this will solve the issue, I'm all for it -- will do a little test after this post -- but if NOT is there a solution or is your response
    You don't
    applicable?
    Last edited by dw85745; Sep 12th, 2014 at 09:09 PM. Reason: Correct Typos

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: RicTextBox - Formatting with Change in Font

    Sorry, what I posted was RTF (Rich Text Format) ... which is what an RTB (Rich Text Box) typically uses... if you're not display RTF and it's just straight up text, then every thing I posted is largely irrelevant.

    I'm off to go get dinner so I'll have to respond more later.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RicTextBox - Formatting with Change in Font

    I'm not seeing any difference between a text file with CRLF delimiters and one with LF delimiters.

    But in general I think your issue might arise from not setting a "canvas width" for the RichTextBox. This seems to be what the RightMargin property actually does.

    See the demo, attached. Includes CRLF and LF versions of the same sample data.
    Attached Files Attached Files

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: RicTextBox - Formatting with Change in Font

    techgnome: Thanks for heads up. Hoped you enjoy(ed) your dinner.

    dilettante:
    Tried your example.
    Don't know where the font size cutoff point is -- your 20 seems to work -- but when I increase it to 48 -- it appears to have the same "break" problem I have.
    For low vision readers 20 may be to small.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RicTextBox - Formatting with Change in Font

    I'm not sure how it is "my" 20, I was just using a sample range of sizes... just change the 20 to 48 then. If you specify a canvas wider than the 16 inches used in my demo then line-wrap will occur later.

    As far as I know there isn't any "infinity" setting available.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: RicTextBox - Formatting with Change in Font

    dilettante:

    Playing with Font Size the breakpoint seems to be 36 *both yours' and mine.
    As previously stated (you can change right width to Canvas if you wish):
    the Font encounters the RTB right width prior to encountering the vbCRLF, so because of Multiline (I assume) the text
    is automatically wrapped to the next line where the vbCRLF is then encountered and causes the new line to start at the RTB left margin.
    So believe RTB is functioning as designed, with No solution because of the large font size.
    Last edited by dw85745; Sep 14th, 2014 at 08:44 AM.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RicTextBox - Formatting with Change in Font

    If I simply do something like replace the 20 points size in my menu by 36 instead, and then I set the RTB.RightMargin to 32 inches (just arbitrarily doubling the 16 inches that I was using) I don't get any line wrapping with the sample text I am using.

    So I have no idea why you think there is "no solution." You just have to use a wide enough RightMargin value.

    For that matter you could use the TextWidth property to fingure out the necessary value on the fly if need be.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: RicTextBox - Formatting with Change in Font

    dilettante:

    So I have no idea why you think there is "no solution." You just have to use a wide enough RightMargin value.
    You're correct. Going with an RTB.Right.Margin = 24 and Font.Size of 48 seems to do the trick at a 48 Font.Size.

    Understand the ScaleX, but will have to do some research on the RTB.Properties to make sure I fully understand what's occurring
    with the Canvas and the Font Size.
    ALso want to do a little research on the "res" file you used. Aware of "res" files but never used one for a reason I don't recall.
    I know the "res" is outside of this thread issue but might as well learn something new while I'm at it.

    Will keep this thread open for now in case I have some additional questions.

    Thanks
    David

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: RicTextBox - Formatting with Change in Font

    It was just a way to embed an initial RTF file into the program, since the RTFText property is not available at design time.


    Don't hold the thread open.

    The way these forums work is you ask a question and get answers. Once you have your answer the thread is done. These are not "personal" threads, you are publishing to the world for everyone's benefit when you post.

    If you have a new question start a new thread.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: RicTextBox - Formatting with Change in Font

    dilettante:

    Don't hold the thread open.

    The way these forums work is you ask a question and get answers. Once you have your answer the thread is done. These are not "personal" threads, you are publishing to the world for everyone's benefit when you post.
    The reason I wanted to hold the thread open was to give me a chance to play with RTB.RightMargin to see if I fully understood what was going on.

    I tried setting the RTB.RightMargin using the following code with both the Form and RTB using Pixels:
    Code:
    'For Setting Right Margin
    Dim dl As Long
    Dim r As RECT
       dl = GetClientRect(RichTextBox1.hwnd, r)
       If dl > 0 Then
          RichTextBox1.RightMargin = (r.Right - r.Left + 1000)
       Else
          Call MsgBox("Resize Right Margin Error")
       End If
    It appears the key is the "+1000". That is some value which will extend the RTB Canvas past the RTB.Width to accomodate the Font.Size.
    As you pointed out above, using .TextWidth (i.e returning the longest string) maybe the best way to set this value (+1000 or whatever).
    In playing with it the RTB Horiz Scrollbar appears to be linked to RTB.RightMargin and adjusts accordingly to accomodate the Font.Size.
    Last edited by dw85745; Sep 15th, 2014 at 09:42 PM.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: [RESOLVED] RicTextBox - Formatting with Change in Font

    I found a nice class which does a great job of Setting the RightMargin using .TextWidth.
    Thank you Mr. Anderson. Hope it helps someone else.

    Code:
    '-----------------------
    'Class:  CTextSize.cls
    'Author:  Joacim Andersson
    
    'Notes:
    'This class only contains a Font property and a TextWidth and a TextHeight method
    'TextWidth|Height will always return the width/height of the supplied text in pixels.
    'If a multilined text is supplied to the TextWidth method, the return value will be
    'the length of the longest line.
    'TextHeight will return the height needed for all lines to be printed.
    'If no Font has been supplied the class will use MS Sans Serif, 8pt, with normal font style (not bold or italic)."
    
    'Example"
    
       'Dim o As CTextSize
       'Set o = New CTextSize
       
       'With o.Font
       '   .Name = "Times New Roman"
       '   .Size = 12
       '   .Bold = True
       'End With
       'MsgBox o.TextWidth("Hello World!")
    '--------------------------------------------------------------------------------
    
    '===========  CLASS STARTS HERE ==============
    
    '  MultiUse = -1  'True
    '  Persistable = 0  'NotPersistable
    '  DataBindingBehavior = 0  'vbNone
    '  DataSourceBehavior = 0   'vbNone
    '  MTSTransactionMode = 0   'NotAnMTSObject
    'End
    Option Explicit
    
    Private Const LF_FACESIZE = 32
    
    Private Type LOGFONT
       lfHeight As Long
       lfWidth As Long
       lfEscapement As Long
       lfOrientation As Long
       lfWeight As Long
       lfItalic As Byte
       lfUnderline As Byte
       lfStrikeOut As Byte
       lfCharSet As Byte
       lfOutPrecision As Byte
       lfClipPrecision As Byte
       lfQuality As Byte
       lfPitchAndFamily As Byte
       lfFaceName(LF_FACESIZE) As Byte
    End Type
    
    Private Const FW_NORMAL = 400
    Private Const FW_BOLD = 700
    Private Const FF_DONTCARE = 0
    Private Const DEFAULT_QUALITY = 0
    Private Const DEFAULT_PITCH = 0
    Private Const DEFAULT_CHARSET = 1
    
    Private Declare Function CreateFontIndirect _
     Lib "gdi32" Alias "CreateFontIndirectA" ( _
     lpLogFont As LOGFONT) As Long
    
    Private Declare Function GetDC _
     Lib "user32.dll" ( _
     ByVal hWnd As Long) As Long
    
    Private Declare Function MulDiv _
     Lib "kernel32" ( _
     ByVal nNumber As Long, _
     ByVal nNumerator As Long, _
     ByVal nDenominator As Long) As Long
    
    Private Declare Function DeleteObject _
     Lib "gdi32" ( _
     ByVal hObject As Long) As Long
    
    Private Declare Function GetDeviceCaps _
     Lib "gdi32" ( _
     ByVal hdc As Long, _
     ByVal nIndex As Long) As Long
    
    Private Const LOGPIXELSY = 90
    
    Private Type Rect
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End Type
    
    Private Declare Function DrawText _
     Lib "USER32" Alias "DrawTextA" ( _
     ByVal hdc As Long, _
     ByVal lpStr As String, _
     ByVal nCount As Long, _
     lpRect As Rect, _
     ByVal wFormat As Long) As Long
    
    Private Const DT_CALCRECT = &H400
    
    Private Declare Function SelectObject _
     Lib "gdi32" ( _
     ByVal hdc As Long, _
     ByVal hObject As Long) As Long
    
    Private m_Font As StdFont
    
    Public Property Get Font() As StdFont
        Set Font = m_Font
    End Property
    
    Public Function TextWidth(ByVal sText As String) As Long
        Dim r As Rect
        r = GetFontSize(sText)
        TextWidth = r.Right
    End Function
    
    Public Function TextHeight(ByVal sText As String) As Long
        Dim r As Rect
        r = GetFontSize(sText)
        TextHeight = r.Bottom
    End Function
    
    Private Sub OLEFontToLogFont(fntThis As StdFont, ByVal hdc As Long, tLF As LOGFONT)
        Dim sFont As String
        Dim iChar As Integer
        Dim b() As Byte
        
        ' Convert an OLE StdFont to a LOGFONT structure:
        With tLF
            sFont = fntThis.Name
            b = StrConv(sFont, vbFromUnicode)
            For iChar = 1 To Len(sFont)
                .lfFaceName(iChar - 1) = b(iChar - 1)
            Next iChar
            ' Based on the Win32SDK documentation:
            .lfHeight = -MulDiv((fntThis.Size), (GetDeviceCaps(hdc, LOGPIXELSY)), 72)
            .lfItalic = fntThis.Italic
            If (fntThis.Bold) Then
                .lfWeight = FW_BOLD
            Else
                .lfWeight = FW_NORMAL
            End If
            .lfUnderline = fntThis.UnderLine
            .lfStrikeOut = fntThis.Strikethrough
            .lfCharSet = fntThis.Charset
        End With
    End Sub
    
    Private Sub Class_Initialize()
        'set a standard font
        Set m_Font = New StdFont
        m_Font.Name = "MS Sans Serif"
        m_Font.Size = 8
    End Sub
    
    Private Function GetFontSize(ByVal sText As String) As Rect
        Dim hdc As Long
        Dim tLF As LOGFONT
        Dim hFnt As Long, hFntOld As Long
        Dim tR As Rect
        
        hdc = GetDC(0)
        OLEFontToLogFont m_Font, hdc, tLF
        hFnt = CreateFontIndirect(tLF)
        hFntOld = SelectObject(hdc, hFnt)
        DrawText hdc, sText, -1, tR, DT_CALCRECT
        SelectObject hdc, hFntOld
        DeleteObject hFnt
        GetFontSize = tR
    End Function
    
    Private Sub Class_Terminate()
        Set m_Font = Nothing
    End Sub

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] RicTextBox - Formatting with Change in Font

    I'm not sure why you'd need to add a length to the width of the RTB, just use the length.

    And you don't need that ton of code to get a usable .TextWidth() method.

    You can just use the Form's (assuming you set the Form font to the same font as the RTB). Or if for some reason you can't do that, then just add a small invisible PictureBox, set its font to match the RTB, and use the PB's .TextWidth() method.


    But for that matter since the value is in physical units (Twips) just pick sizes scaled to your font size or just pick a big size. Otherwise you'll have to scan the document to find the longest line. There must be some reasonable limit to how large you will let users choose for the font.

    This ain't rocket science. I think you are working far too hard at something very simple.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: [RESOLVED] RicTextBox - Formatting with Change in Font

    dilettante:
    Thanks for further input.

    I'm not sure why you'd need to add a length to the width of the RTB, just use the length.
    If you're referring to this line of code:
    Code:
         RichTextBox1.RightMargin = (r.Right - r.Left + 1000)
    With CTextSize, I just set the RTB.RightMargin = CTextSize.TextWidth and abandoned the need to GetClientRect.

    And you don't need that ton of code to get a usable .TextWidth() method.
    I had thought of using the Form's Font Size to get TextWidth (a hidden PBox is another good suggestion), but decided the Class would be better in case I needed to adjust the Form Font independently.
    Whether I use a Form or hidden Picturebox, to get the .TextWidth (longest line) from either, I believe I would have to
    id the longest line first. Whether this would be slower than using the Class, I'd have to test.

    I think you are working far too hard at something very simple.
    Probably

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width