VBGood网站全文搜索 Google

首页 - 经验之谈 - Retrieving cursor information from a Rich Textbox control
发表评论(0)作者:David Goben, 平台:VB6.0+Win98, 阅读:8344, 日期:2001-07-18
Retrieving cursor information from a Rich Textbox control

In a previous tip, we showed you how to count the lines of text
in a Rich Textbox control. However, if you注释:re going to use this
control to create a text editor, you注释:ll need to know more than
just the number of used lines. For instance, it注释:s often useful
to display the cursor注释:s current position with the control. To do
so, you注释:ll need to call on the SendMessageByNum() API function.
This function uses the following declaration:

Private Declare Function SendMessageByNum Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long

To obtain the cursor line and index information, you注释:ll pass in
the following to constants, which you also place in the general
declarations section:

Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB

To obtain the appropriate values, you simply pass the applicable
constant into the SendMessageByNum() function, which returns the
position value you注释:ve requested. For instance, to obtain the current
line value, you might create a function like so:

Public Function GetCurrentLine(TxtBox As Object) As Long
With TxtBox
GetCurrentLine = SendMessageByNum(.hwnd, EM_LINEFROMCHAR, _
CLng(.SelStart), 0&) + 1
End With
End Function

Of course, with a minor modification, you use the same code to
retrieve the current column, as in:

Public Function GetCurrentColumn(TxtBox As Object) As Long
With TxtBox
GetCurrentColumn = .SelStart - SendMessageByNum(.hwnd, _
EM_LINEINDEX, -1&, 0&) + 1
End With
End Function

The following two Click() events show how you注释:d use the two procedures,
(assuming you注释:ve placed a Rich Textbox control and two Command
buttons on a form):

Private Sub Command1_Click()
MsgBox GetCurrentLine(RichTextBox1)
End Sub

Private Sub Command2_Click()
MsgBox GetCurrentColumn(RichTextBox1)
End Sub

This tip was contributed by David Goben, davgoben@merrymech.com.