【问题标题】:Justify the text in a richtextbox对齐富文本框中的文本
【发布时间】:2016-05-06 12:43:10
【问题描述】:

我知道如何在文本框和richtextbox 中证明右居中和左对齐,但我搜索并没有找到答案是你能像在word 或所有其他程序中一样调整文本吗?我尝试使用写字板然后复制它,效果很好,只是当我无法单击命令按钮并对齐文本时,所有行都排成一行,我就停止了, 我正在使用的代码就在这里

    Option Explicit

Private Const WM_USER As Long = &H400&
Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60
Private Const EM_SETTYPOGRAPHYOPTIONS As Long = WM_USER + 202
Private Const TO_ADVANCEDTYPOGRAPHY As Long = 1

Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Private Document As tom.ITextDocument

Private Sub cmdJustify_Click()
    'Justify the text of the first paragraph:

    'Select first paragraph in the document.
    With Document.Range(0, 0)
        .MoveEnd tomParagraph, 1
        .Para.Alignment = tomAlignJustify 'Apply justification.
        .Collapse tomStart 'Collapse the selection.
    End With
End Sub

Private Sub Form_Load()
    Dim Unknown As Object

    SendMessage rtb.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(Unknown)
    Set Document = Unknown
    SendMessage rtb.hWnd, _
                EM_SETTYPOGRAPHYOPTIONS, _
                TO_ADVANCEDTYPOGRAPHY, _
                TO_ADVANCEDTYPOGRAPHY

    rtb.LoadFile "Resources\Sample1.rtf"
End Sub

【问题讨论】:

  • 您的rtb 是RichTextBox 吗?你有一个名为Resources\Sample1.rtf 的文件吗?因为您所做的只是从下面复制并粘贴示例代码,并且该代码在这里完全没有问题。
  • 没有。只有 RichEdit 控件支持 EM_SETTYPOGRAPHYOPTIONS 和具有不同对齐方式的段落的标记。编辑控件全有或全无,仅限于左、右或居中。

标签: textbox vb6 richtextbox justify


【解决方案1】:

您必须向底层 RichEdit 控件发送 EM_SETPARAFORMAT 消息,并通过 wAlignment = PFA_JUSTIFY 向其传递指向 PARAFORMAT2 structure 的指针。

或者使用 TOM 和ITextPara.Alignment = tomAlignJustify(见tomAlignJustify)之类的。

或者除此之外,查找 RTF 标记并使用 .SelRTF

好的,您必须先打开“高级换行和行格式”。在那之后,它就变得一帆风顺了:

Option Explicit

Private Const WM_USER As Long = &H400&
Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60
Private Const EM_SETTYPOGRAPHYOPTIONS As Long = WM_USER + 202
Private Const TO_ADVANCEDTYPOGRAPHY As Long = 1

Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Private Document As tom.ITextDocument

Private Sub cmdJustify_Click()
    'Justify the text of the first paragraph:

    'Select first paragraph in the document.
    With Document.Range(0, 0)
        .MoveEnd tomParagraph, 1
        .Para.Alignment = tomAlignJustify 'Apply justification.
        .Collapse tomStart 'Collapse the selection.
    End With
End Sub

Private Sub Form_Load()
    Dim Unknown As Object

    SendMessage RTB.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(Unknown)
    Set Document = Unknown
    SendMessage RTB.hWnd, _
                EM_SETTYPOGRAPHYOPTIONS, _
                TO_ADVANCEDTYPOGRAPHY, _
                TO_ADVANCEDTYPOGRAPHY

    RTB.LoadFile "Resources\Sample1.rtf"
End Sub

【讨论】:

  • 但那是 C++,而我需要 vb 6?
  • 不需要 C++。前两个可以通过 VB6 程序完成,第三个当然可以。
  • @Bob77:我不确定您是否可以直接向控件发送 EM_SETPARAMFORMAT 消息,因为其中一些功能不在 1.0 RTF 规范中。不过,我确实知道 TOM 技巧有效。
  • 很确定它有效。请记住,在现代,VB6 RTB 包装了更新版本的 RichEdit(v. 3.0?),在其“兼容模式”下工作,而不是旧的 RichEdit 1.0 本身。
  • 得到错误 Private Document As tom.ITextDocument "User-Defined type not defined"
【解决方案2】:

VB6中的RichText控件只直接支持富文本1.0,不支持对齐。正如 Bob77 所建议的,您可以使用 TOM 来执行此操作。本质上,您是在欺骗控件直接公开其 iRichTextOLE 接口,这使您可以访问更高的 RTF 版本。这是我使用的代码:

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
Private Const WM_USER = &H400&
Private Const EM_GETOLEINTERFACE = (WM_USER + 60)

Dim myIUnknown As IUnknown   
Dim tomDoc As ITextDocument  

Sub Form_Load()
    SendMessage(rtcMyControl.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown)
    tomDoc = myIUnknown
End Sub

这与 Bob77 获取 ITextDocument 引用的代码基本相同,只是它的另一个版本。我只是把它放在显示与他的答案的“使用 TOM”部分相对应的部分,以防它有助于澄清这一点。就个人而言,如果我需要做任何像你正在做的事情,TOM 就是我的选择。

【讨论】:

    【解决方案3】:

    仅通过设置属性是不可能的。你必须在它上面做更多的工作并完成你的功能。

    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
    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
    Const WM_USER = &H400
    Const EM_SETTYPOGRAPHYOPTIONS = WM_USER + 202
    Const TO_ADVANCEDTYPOGRAPHY = 1
    Const EM_SETPARAFORMAT = WM_USER + 71
    Private Const PFA_LEFT = 1
    Private Const PFA_RIGHT = 2
    Private Const PFA_CENTER = 3
    Private Const PFA_JUSTIFY = &H4
    Const MAX_TAB_STOPS = 32
    Private Type PARAFORMAT2
        cbSize                     As Long
        dwMask                     As Long
        wNumbering                 As Integer
        wEffects                   As Integer
        dxStartIndent              As Long
        dxRightIndent              As Long
        dxOffset                   As Long
        wAlignment                 As Integer
        cTabCount                  As Integer
        rgxTabs(MAX_TAB_STOPS - 1) As Long
        dySpaceBefore              As Long
        dySpaceAfter               As Long
        dyLineSpacing              As Long
        sStyle                     As Integer
        bLineSpacingRule           As Byte
        bOutlineLevel              As Byte
        wShadingWeight             As Integer
        wShadingStyle              As Integer
        wNumberingStart            As Integer
        wNumberingStyle            As Integer
        wNumberingTab              As Integer
        wBorderSpace               As Integer
        wBorderWidth               As Integer
        wBorders                   As Integer
    End Type
    Public Enum ERECParagraphAlignmentConstants
       ercParaLeft = PFA_LEFT
       ercParaCentre = PFA_CENTER
       ercParaRight = PFA_RIGHT
       ercParaJustify = PFA_JUSTIFY
    End Enum
    Private Const PFM_ALIGNMENT = &H8&
    
    Private Function SetAlignment(lHwnd As Long, ByVal eAlign As ERECParagraphAlignmentConstants)
        Dim tP2 As PARAFORMAT2
        Dim lR As Long
        tP2.dwMask = PFM_ALIGNMENT
        tP2.cbSize = Len(tP2)
        tP2.wAlignment = eAlign
        lR = SendMessageLong(lHwnd, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
        lR = SendMessage(lHwnd, EM_SETPARAFORMAT, 0, tP2)
    End Function
    

    用法

    SetAlignment RichTextBox1.HWND, ercParaJustify

    我从VBForums的代码库部分选择了这个代码

    我在这里发布相同的代码,因为这是一个非常有用的代码。

    【讨论】:

    • 不对齐任何东西!它保持不变,我使用了你的用法,没有错误,但我也没有结果!我从您的链接中得到了相同的结果或实际上没有结果,您知道为什么它不起作用吗?
    • 你能解释一下这段代码是如何工作的,甚至可以上传一个可以运行的示例项目吗?
    • 我不再使用vb了。但是,我在 2005 年使用此代码在我当时正在处理的 VB 项目中对文本进行了对齐,并且当时它对我有用。
    • 我需要任何参考资料吗?如果它适用于 vb5,这应该适用于 vb6 对吗?
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 2018-09-29
    • 2015-07-29
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多