RichTextBox中的Justify选项

时间:2012-04-26 09:12:02

标签: vb.net winforms

我正在开发一个应用程序,我想在RichTextbox中输入文本,类似于Word文档。

我已浏览了此链接中的代码

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/455a6427-a3a2-4215-89a3-557114dbcddd

有人在VB代码中有更简单的解决方案吗?

由于

4 个答案:

答案 0 :(得分:1)

我找到了一个简单的VB代码来做到这一点,它工作得很好。这是链接。

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/25045a28-b8c9-4146-9820-1231a58c5925/

我也会放置代码以防丢失链接。

' Constants and structures from richedit.h
Const MAX_TAB_STOPS = 32 ' expanded individually
Const PFA_JUSTIFY = 4 ' Left = 1, Center = 2, Right = 3
Const CBSIZE = 188 ' Size of PARAFORMAT2 structure
Const PFM_ALIGNMENT = &H8

Const WM_USER = &H400
Const EM_DISPLAYBAND = WM_USER + 51
Const EM_FORMATRANGE = WM_USER + 57
Const EM_SETPARAFORMAT = WM_USER + 71
Const EM_SETTARGETDEVICE = WM_USER + 72

<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT2
    Public cbSize As Int16 'UINT cbSize;
    Public dwMask As Int32 'DWORD dwMask;
    Public wNumbering As Int16 'WORD  wNumbering;
    Public wEffects As Int16  'WORD  wEffects;
    Public dxStartIndent As Int32  'LONG  dxStartIndent;
    Public dxRightIndent As Int32  'LONG  dxRightIndent;
    Public dxOffset As Int32  'LONG  dxOffset;
    Public wAlignment As Int16  'WORD  wAlignment;
    Public cTabCount As Int16  'SHORT cTabCount;
    Public rgxTabs1 As Int32   'LONG  rgxTabs[MAX_TAB_STOPS];
    Public rgxTabs2 As Int32
    Public rgxTabs3 As Int32
    Public rgxTabs4 As Int32
    Public rgxTabs5 As Int32
    Public rgxTabs6 As Int32
    Public rgxTabs7 As Int32
    Public rgxTabs8 As Int32
    Public rgxTabs9 As Int32
    Public rgxTabs10 As Int32
    Public rgxTabs11 As Int32
    Public rgxTabs12 As Int32
    Public rgxTabs13 As Int32
    Public rgxTabs14 As Int32
    Public rgxTabs15 As Int32
    Public rgxTabs16 As Int32
    Public rgxTabs17 As Int32
    Public rgxTabs18 As Int32
    Public rgxTabs19 As Int32
    Public rgxTabs20 As Int32
    Public rgxTabs21 As Int32
    Public rgxTabs22 As Int32
    Public rgxTabs23 As Int32
    Public rgxTabs24 As Int32
    Public rgxTabs25 As Int32
    Public rgxTabs26 As Int32
    Public rgxTabs27 As Int32
    Public rgxTabs28 As Int32
    Public rgxTabs29 As Int32
    Public rgxTabs30 As Int32
    Public rgxTabs31 As Int32
    Public rgxTabs32 As Int32
    Public dySpaceBefore As Int32 'LONG  dySpaceBefore;
    Public dySpaceAfter As Int32 'LONG  dySpaceAfter;
    Public dyLineSpacing As Int32 'LONG  dyLineSpacing;
    Public sStyle As Int16 'SHORT sStyle;
    Public bLineSpacingRule As Byte 'BYTE  bLineSpacingRule;
    Public bOutlineLevel As Byte 'BYTE  bOutlineLevel;
    Public wShadingWeight As Int16 'WORD  wShadingWeight;
    Public wShadingStyle As Int16 'WORD  wShadingStyle;
    Public wNumberingStart As Int16 'WORD  wNumberingStart;
    Public wNumberingStyle As Int16 'WORD  wNumberingStyle;
    Public wNumberingTab As Int16 'WORD  wNumberingTab;
    Public wBorderSpace As Int16 'WORD  wBorderSpace;
    Public wBorderwidth As Int16 'WORD  wBorderWidth;
    Public wBorders As Int16 'WORD  wBorders;
End Structure

<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, _
                                ByVal msg As Int32, _
                                ByVal wParam As Int32, _
                                ByVal lParam As IntPtr) As Int32
End Function

  

FORM DESIGNER CODE到这里


Private Sub btnSetAlignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetAlignment.Click

    Dim pf2 As PARAFORMAT2

    rtb.SelectAll()

    pf2.cbSize = CBSIZE
    pf2.dwMask = pf2.dwMask Or PFM_ALIGNMENT
    pf2.wAlignment = PFA_JUSTIFY

    ' Allocate memory for the PARAFORMAT2 struct and
    ' copy the contents of the struct to this memory
    Dim lParam As IntPtr
    lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(pf2))
    Marshal.StructureToPtr(pf2, lParam, False)

    Dim iRet As Integer
    iRet = SendMessage(rtb.Handle(), EM_SETPARAFORMAT, 0, lParam)

    ' Free allocated memory
    Marshal.FreeCoTaskMem(lParam)

End Sub

答案 1 :(得分:0)

看看Extending RichTextBox。它类似于MSDN上的示例,但有点简单。

答案 2 :(得分:0)

尝试类似

的内容

rtbox.SelectionStart = 0'rtbox是richtextbox的名称
rtbox.SelectionLength = rtbox.Text.Length
rtbox.SelectionAlignment = TextAlignment.Justify

'用于其他文字对齐
'rtbox.SelectionAlignment = TextAlignment.Left
'rtbox.SelectionAlignment = TextAlignment.Right
'rtbox.SelectionAlignment = TextAlignment.Center

答案 3 :(得分:0)

解决方案对我没有用,除非我补充说:

SendMessage(Handle, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)

完整的代码:

 Private Const EM_SETTYPOGRAPHYOPTIONS As Long = WM_USER + 202
 Private Const TO_ADVANCEDTYPOGRAPHY As Long = 1

 Private sub SetAlignJustify()
    Dim Para As New Paraformat2
    With Para
       .cbSize = CUInt(Marshal.SizeOf(Para))
       .dwMask = PFM_ALIGNMENT
       .wAlignment = value
    End With

    SendMessage(Handle, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)


    Dim lpar As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(Para))
    Marshal.StructureToPtr(Para, lpar, False)
    Dim result As Integer = SendMessage(Handle, EM_SETPARAFORMAT, 0, lpar)
    Marshal.FreeCoTaskMem(lpar)
End Sub