RichTextBox中设置和取消自动换行

Public Const WM_USER = &H400
Public Const EM_SETTARGETDEVICE = (WM_USER + 72)

Public Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Public Sub AutoSwitchLine(ByRef RichText As RichTextBox, ByVal bSwitch As Boolean)
If bSwitch Then
'设置 RichTextBox 自动换行
Call SendMessage(RichText.hWnd, EM_SETTARGETDEVICE, _
GetDC(RichText.hWnd), RichText.Width / 15)
If RichText.RightMargin = 0 Then
RichText.RightMargin = 1
Else
RichText.RightMargin = 0
End If
Else
'设置 RichTextBox 不自动换行
Call SendMessage(RichText.hWnd, EM_SETTARGETDEVICE, 0, 1)
End If
End Sub


调用方法:
AutoSwitchLine RichTextBox1, False
上面的代码使RichTextBox1不自动换行。

上面的是比较正规的方法,如果简单的话,下面两句代码就可以了:

RichTextBox1.MultiLine=True
RichTextBox1.RightMargin=10000

你可能感兴趣的:(text)