2012-04-26 57 views

回答

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 

窗體設計器代碼放在這裏


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 
0

看一看Extending RichTextBox。它類似於MSDN的例子,但有點簡單。

+0

我發現在VB一個簡單的代碼,它工作得很好對我來說,這裏是鏈接http://social.msdn.microsoft.com/Forums/en-US/ winforms/thread/25045a28-b8c9-4146-9820-1231a58c5925 /我會在某段時間後發佈一個單獨的答案。 – 2012-04-26 10:53:45

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.Cent呃

+1

WinForms RichTextBox只有Horizo​​ntalAlignment枚舉的中心,左側和右側可用於SelectionAlignment屬性。 – LarsTech 2012-04-26 21:12:12

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 
+0

這適用於我。但是當我改變縮放因子時,righmargin被忽略。我怎樣才能避免這種情況? – Simon 2017-10-03 14:25:22