Results 1 to 8 of 8

Thread: VB6 - Justify text in RichTextBox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Smile VB6 - Justify text in RichTextBox

    hi, I was looking for this for sometime, and finally found it.

    Though I now search in the forum, then I do find it posted in one two threads. I posted it here in this Code-Bank section, because I think this one is really handy.
    Usage:
    SetAlignment RichTextBox1.HWND, ercParaJustify
    VB Code:
    1. Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Const WM_USER = &H400
    4. Const EM_SETTYPOGRAPHYOPTIONS = WM_USER + 202
    5. Const TO_ADVANCEDTYPOGRAPHY = 1
    6. Const EM_SETPARAFORMAT = WM_USER + 71
    7. Private Const PFA_LEFT = 1
    8. Private Const PFA_RIGHT = 2
    9. Private Const PFA_CENTER = 3
    10. Private Const PFA_JUSTIFY = &H4
    11. Const MAX_TAB_STOPS = 32
    12. Private Type PARAFORMAT2
    13.     cbSize                     As Long
    14.     dwMask                     As Long
    15.     wNumbering                 As Integer
    16.     wEffects                   As Integer
    17.     dxStartIndent              As Long
    18.     dxRightIndent              As Long
    19.     dxOffset                   As Long
    20.     wAlignment                 As Integer
    21.     cTabCount                  As Integer
    22.     rgxTabs(MAX_TAB_STOPS - 1) As Long
    23.     dySpaceBefore              As Long
    24.     dySpaceAfter               As Long
    25.     dyLineSpacing              As Long
    26.     sStyle                     As Integer
    27.     bLineSpacingRule           As Byte
    28.     bOutlineLevel              As Byte
    29.     wShadingWeight             As Integer
    30.     wShadingStyle              As Integer
    31.     wNumberingStart            As Integer
    32.     wNumberingStyle            As Integer
    33.     wNumberingTab              As Integer
    34.     wBorderSpace               As Integer
    35.     wBorderWidth               As Integer
    36.     wBorders                   As Integer
    37. End Type
    38. Public Enum ERECParagraphAlignmentConstants
    39.    ercParaLeft = PFA_LEFT
    40.    ercParaCentre = PFA_CENTER
    41.    ercParaRight = PFA_RIGHT
    42.    ercParaJustify = PFA_JUSTIFY
    43. End Enum
    44. Private Const PFM_ALIGNMENT = &H8&
    45.  
    46. Private Function SetAlignment(lHwnd As Long, ByVal eAlign As ERECParagraphAlignmentConstants)
    47.     Dim tP2 As PARAFORMAT2
    48.     Dim lR As Long
    49.     tP2.dwMask = PFM_ALIGNMENT
    50.     tP2.cbSize = Len(tP2)
    51.     tP2.wAlignment = eAlign
    52.     lR = SendMessageLong(lHwnd, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
    53.     lR = SendMessage(lHwnd, EM_SETPARAFORMAT, 0, tP2)
    54. End Function

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    hmm

    I makes me wonder why this isn't included in the properties of the normal rich text box, if it's done with sendmessage, it obviouslt most hold the information already, or ?
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Smile hi

    Actually not just this property, but also making wavy lines( the ones which ms word uses to mark a spell error), hiding some text in a rtfbox, have not being exposed to VB programmers.

  4. #4
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    Where did you find the info on what messages it accepts?

    cjqp

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Well, frankly speaking I dont remember.

    From some time, I have being looking in rtf control and searching various sites. I may have got it from some site.


  6. #6
    New Member
    Join Date
    Aug 2011
    Posts
    1

    Re: VB6 - Justify text in RichTextBox

    First of all, I'd like to thank "veryjonny" for sharing this valuable code with us, but it needs some modifications.

    This code can correctly "justify" the text in a RichTextBox, but it does not work on a Right-To-Left paragraph.

    For languages such as "Persian, Arabic, Hebrew" that have a Right-To-Left paragraph direction, it changes the paragraphy direction to Left-To-Right.

    Can anyone change the code to make it applicable to Right-To-Left paragraphs?

    By the way, How can we use "dySpaceBefore" & "dySpaceAfter" for a paragraph with this code?

    Thank you again.

  7. #7
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Smile Re: VB6 - Justify text in RichTextBox

    Quote Originally Posted by veryjonny View Post
    hi, I was looking for this for sometime, and finally found it.

    Though I now search in the forum, then I do find it posted in one two threads. I posted it here in this Code-Bank section, because I think this one is really handy.

    VB Code:
    1. Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal HWND As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Const WM_USER = &H400
    4. Const EM_SETTYPOGRAPHYOPTIONS = WM_USER + 202
    5. Const TO_ADVANCEDTYPOGRAPHY = 1
    6. Const EM_SETPARAFORMAT = WM_USER + 71
    7. Private Const PFA_LEFT = 1
    8. Private Const PFA_RIGHT = 2
    9. Private Const PFA_CENTER = 3
    10. Private Const PFA_JUSTIFY = &H4
    11. Const MAX_TAB_STOPS = 32
    12. Private Type PARAFORMAT2
    13.     cbSize                     As Long
    14.     dwMask                     As Long
    15.     wNumbering                 As Integer
    16.     wEffects                   As Integer
    17.     dxStartIndent              As Long
    18.     dxRightIndent              As Long
    19.     dxOffset                   As Long
    20.     wAlignment                 As Integer
    21.     cTabCount                  As Integer
    22.     rgxTabs(MAX_TAB_STOPS - 1) As Long
    23.     dySpaceBefore              As Long
    24.     dySpaceAfter               As Long
    25.     dyLineSpacing              As Long
    26.     sStyle                     As Integer
    27.     bLineSpacingRule           As Byte
    28.     bOutlineLevel              As Byte
    29.     wShadingWeight             As Integer
    30.     wShadingStyle              As Integer
    31.     wNumberingStart            As Integer
    32.     wNumberingStyle            As Integer
    33.     wNumberingTab              As Integer
    34.     wBorderSpace               As Integer
    35.     wBorderWidth               As Integer
    36.     wBorders                   As Integer
    37. End Type
    38. Public Enum ERECParagraphAlignmentConstants
    39.    ercParaLeft = PFA_LEFT
    40.    ercParaCentre = PFA_CENTER
    41.    ercParaRight = PFA_RIGHT
    42.    ercParaJustify = PFA_JUSTIFY
    43. End Enum
    44. Private Const PFM_ALIGNMENT = &H8&
    45.  
    46. Private Function SetAlignment(lHwnd As Long, ByVal eAlign As ERECParagraphAlignmentConstants)
    47.     Dim tP2 As PARAFORMAT2
    48.     Dim lR As Long
    49.     tP2.dwMask = PFM_ALIGNMENT
    50.     tP2.cbSize = Len(tP2)
    51.     tP2.wAlignment = eAlign
    52.     lR = SendMessageLong(lHwnd, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
    53.     lR = SendMessage(lHwnd, EM_SETPARAFORMAT, 0, tP2)
    54. End Function
    Hi

    I have tested your API code, It worked very well... Thank you for sharing it

    But, I have found another way without using API (based on TextRTF Property):

    Code:
    Private Sub RichTextBox1_Change()
    
        'Guide: True is Right To Left And False OR Default is Left To Right
    
        Call RichTextSetAlignment(RichTextBox1, True)
    
    End Sub
    
    Public Sub RichTextSetAlignment(TheControl As Control, Optional RightToLeftSupport As Boolean)
    
        Select Case RightToLeftSupport
        
        Case False
        
            ' For Left To Right Justify Support
            With TheControl
                
                Select Case InStr(.TextRTF, "\ql\")
                    Case Is <= 0
                        'If there is not ql in the codes
                        .TextRTF = Replace(.TextRTF, "\ltrpar\", "\ltrpar\qj\")
                    Case Else
                        'If there is at least 1 ql in the code
                        .TextRTF = Replace(.TextRTF, "\ltrpar\ql\", "\ltrpar\qj\")
                End Select
                
            End With
            
        Case True
        
            ' For Right To Left Justify Support
            With TheControl
                
                Select Case InStr(.TextRTF, "\ql\")
                    Case Is <= 0
                        'If there is not ql in the codes
                        .TextRTF = Replace(.TextRTF, "\ltrpar\", "\rtlpar\qj\")
                    Case Else
                        'If there is at least 1 ql in the code
                        .TextRTF = Replace(.TextRTF, "\ltrpar\ql\", "\rtlpar\qj\")
                End Select
              
            End With
            
        End Select
        
    End Sub

  8. #8
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Re: VB6 - Justify text in RichTextBox

    Quote Originally Posted by nicebasic View Post
    First of all, I'd like to thank "veryjonny" for sharing this valuable code with us, but it needs some modifications.

    This code can correctly "justify" the text in a RichTextBox, but it does not work on a Right-To-Left paragraph.

    For languages such as "Persian, Arabic, Hebrew" that have a Right-To-Left paragraph direction, it changes the paragraphy direction to Left-To-Right.

    Can anyone change the code to make it applicable to Right-To-Left paragraphs?

    By the way, How can we use "dySpaceBefore" & "dySpaceAfter" for a paragraph with this code?

    Thank you again.
    Hi

    Unfortunately, The Classic RichTextBox doesn't support it

    It's impossible even with my function

    But If you use (my function) + ( @Krool OCX ) it's possible (i have tested it)

    1) Go to http://www.vbforums.com/showthread.p...mmon-controls)

    2) Download ComCtlsDemo.zip.docx

    3) Rename to .zip

    4) Unzip it

    5) Read the readme file and do the steps

    6) Open the standard exe file

    7) Add a new Form and then add a RichTextBox to it and set its RightToLeft Property to True

    8) Add my function (that you can see in the last post)

    9) Replace:

    Code:
        Case True
        
            ' For Right To Left Justify Support
            With TheControl
                
                Select Case InStr(.TextRTF, "\ql\")
                    Case Is <= 0
                        'If there is not ql in the codes
                        .TextRTF = Replace(.TextRTF, "\ltrpar\", "\rtlpar\qj\")
                    Case Else
                        'If there is at least 1 ql in the code
                        .TextRTF = Replace(.TextRTF, "\ltrpar\ql\", "\rtlpar\qj\")
                End Select
              
            End With
            
        End Select
    With

    Code:
        Case True
        
            ' For Right To Left Justify Support
            With TheControl
                
                Select Case InStr(.TextRTF, "\ql\")
                    Case Is <= 0
                        'If there is not ql in the codes
                        .TextRTF = Replace(.TextRTF, "\ltrpar\", "\rtlpar\qj\")
                    Case Else
                        'If there is at least 1 ql in the code
                        .TextRTF = Replace(.TextRTF, "\ltrpar\ql\", "\rtlpar\qj\")
                End Select
    
                .TextRTF = Replace(.TextRTF, "\rtlpar\qr\", "\rtlpar\qj\")
              
            End With
            
        End Select
    10) Enjoy Full Support

    Good news: The New RichTextBox Also Supports Unicode

    Thank you @Krool
    Last edited by Mahdi Jazini; Jan 9th, 2017 at 03:23 AM.

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