我正在开发一个应用程序,我想证明在 RichTextbox 中输入的文本,类似于 Word 文档。
我已经浏览了此链接中的代码
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/455a6427-a3a2-4215-89a3-557114dbcddd
有人在VB代码中有更简单的解决方案吗?
谢谢
我正在开发一个应用程序,我想证明在 RichTextbox 中输入的文本,类似于 Word 文档。
我已经浏览了此链接中的代码
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/455a6427-a3a2-4215-89a3-557114dbcddd
有人在VB代码中有更简单的解决方案吗?
谢谢
我找到了一个简单的 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
看看扩展 RichTextBox。它类似于 MSDN 上的示例,但更简单一些。
尝试类似的东西
rtbox.SelectionStart = 0 'rtbox 是富文本框的名称
rtbox.SelectionLength = rtbox.Text.Length
rtbox.SelectionAlignment =TextAlignment.Justify
'对于其他文本对齐方式
'rtbox.SelectionAlignment = TextAlignment.Left
'rtbox.SelectionAlignment = TextAlignment.Right
'rtbox.SelectionAlignment = TextAlignment.Center
该解决方案对我不起作用,除非我添加:
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