Windows API: SendMessage bad!

tonofsteel

Active member
Joined
Jul 10, 2003
Messages
35
I have been trying to successfully get the following code to work for about 8 hours now. Its starting to get a little frustrating to say the least, especially since it is probably something small I am missing. Anyways I am using Visual basic .net Standard and the following code is all in a module. I have excluded all the non relevant code, any help would be appreciated.


Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Const EM_GETLINECOUNT As Long = &HBA
Public Const EM_GETSEL As Long = &HB0
Public Const EM_LINEFROMCHAR As Long = &HC9
Public Const EM_LINEINDEX As Long = &HBB
Public Const EM_LINELENGTH As Long = &HC1
Public Const EM_LINESCROLL As Long = &HB6
Public Const EM_SCROLLCARET As Long = &HB7
Public Const EM_SETSEL As Long = &HB1
Public Const WM_SETREDRAW As Long = &HB




In the following the StartofLine sendmessage always returns a 0 no matter what I try. The Length of line always returns the length of the first line. And the net result is the first line being highlighted, ALWAYS. Even if i manually edit LineNumber to say 3 or something.



Dim StartofLine As Long
Dim LineNumber As Long
Dim LengthofLine As Long
Dim CharacterIndex As Long

CharacterIndex = FindMyText(ctl, HighString, 0, 0)
LineNumber = ctl.GetLineFromCharIndex(CharacterIndex)
StartofLine = SendMessage(ctl.Handle, EM_LINEINDEX, LineNumber, 0)
LengthofLine = SendMessage(ctl.Handle, EM_LINELENGTH, LineNumber, 0)
ctl.Select(StartofLine, LengthofLine)






The following Code works with sendMessage, so why not EM_LINEINDEX??????????

Public Function CountLines(ByVal ctl As RichTextBox) As Integer
Return (SendMessage(ctl.Handle, EM_GETLINECOUNT, 0, 0))
End Function



Any help would be appreciated
 
why would you use EM_GETLINECOUNT in vb.net? all you need to do to get the amount of lines is this :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer = TextBox1.Lines.Length /// amount of lines in the textbox
        MessageBox.Show("There are : " & i & " lines in the textbox!")
    End Sub

to show each line 1 at a time you can do this :
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer
        For i = TextBox1.Lines.GetLowerBound(0) To TextBox1.Lines.GetUpperBound(0)
            MessageBox.Show(TextBox1.Lines(i))
        Next
    End Sub
 
To get all the functions working that sendmessage can use all you have to do is change it to:

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Integer


Changing everything from long to integer seems to do the trick.
As well you did not answer my question at all because you commented on the only function that was working in the program.

I managed to find a rich text box that you can highlight lines of text or words with colors, since M$ cant seem to think anyone would want this. So I search a word out, but i want the whole line to be highlighted with this word on it. EM_LINEINDEX finds the index of the first character, and EM_LINELENGTH finds the length of the line so i can selectively highlight that line.
 
Back
Top