VB SendMessage API Simple usage

Source: Internet
Author: User

VB SendMessage API Simple usage
1.

In Windows programming, to text box controls, list controls, button controls, and so on, are the controls we most often touch. However, in VB, these controls sometimes do not meet our needs. At this point, we can extend the functionality of these controls simply by using the Windows API functions.
As the name implies, the SendMessage function is a function that sends a message to a window (where the window refers to a control that has an HWND property, such as a button, list box, edit box, and so on), and the function is defined as follows:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd as Long, _
ByVal wmsg as Long, _
ByVal WParam as Long, _
LParam as any) as Long

Where HWND specifies the window that accepts the message, the parameter wmsg specifies the message value, and the parameter wparam lparam defines the additional parameters passed to the window, respectively. In many of the Windows system messages, there is more than just a window message to provide. They can control the actions and properties of the window. Here I will introduce you to the application of the SendMessage function in extending the functionality of the basic control.
A. List (ListBox) control
In Windows, there is a series of list messages starting with Lb_, and here is an application that uses a ListBox controlled by the LB message
1. Make the cursor in the list move to a different list item with different hints (tooltip)
There is a ToolTipText property in the ListBox control that determines the hint text that appears when the cursor moves over the list box. But how do you make the prompt text different when the cursor is moved on a different list item? The key to the problem is to know the index of the list item where the cursor is when the cursor moves, and to use the SendMessage function to send a lb_itemfrompoint message. Here is an example of the program:

Option Explicit

Const Lb_itemfrompoint = &h1a9

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd as Long, _
ByVal wmsg as Long, _
ByVal WParam as Long, _
LParam as any) as Long

Private Sub Form_Load ()
Dim I

For i = 1 to 200
List1.AddItem Str (i) + "Samples in this list is" + str (i)
Next I
End Sub

Private Sub list1_mousemove (Button As Integer, Shift As Integer, X as single, Y as single)
Dim Lxpoint as Long
Dim Lypoint as Long
Dim LIndex as Long

If button = 0 Then ' determines that the function key or mouse button is not pressed while moving the mouse
' Gets the position of the cursor, in pixels
Lxpoint = CLng (X/SCREEN.TWIPSPERPIXELX)
Lypoint = CLng (y/screen.twipsperpixely)
‘
With List1
' Gets the index of the header row where the cursor is located
LIndex = SendMessage (. hwnd, Lb_itemfrompoint, 0, _
ByVal ((Lypoint * 65536) + lxpoint))
' Set the ToolTip for the listbox to the text of the header row
If (lIndex >= 0) and (LIndex <=. ListCount) Then
. ToolTipText =. List (LIndex) ' Return the text =. List (LIndex)
Else
. ToolTipText = ""
End If
End with
End If
End Sub

First add a ListBox control to Form1, and then add the above code to the Form1 Code window. Run the program, and when the cursor moves through the list, you can see that the text is different depending on the list item where the cursor is located.

2. Adding a horizontal scroll bar to the list allows you to browse long list items
When you add a list item to a list that exceeds the list's display range, the list does not appear with a horizontal scroll bar so you can scroll to browse through the entire contents of the item. The Lb_sethorizontalextent message lets you set the horizontal scroll bar for the list and the scrolling length. Here is the sample program:
Option Explicit

Private Type RECT
Left as Long
Top as Long
Right as Long
Bottom as Long
End Type

Private Declare Function DrawText Lib "user32" Alias "Drawtexta" _
(ByVal hdc as Long, _
ByVal LpStr as String, _
ByVal Ncount as Long, _
LpRect as RECT, _
ByVal Wformat as Long) as long


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd as Long, _
ByVal wmsg as Long, _
ByVal WParam as Long, _
LParam as any) as Long

Const lb_sethorizontalextent = &h194
Const Dt_calcrect = &h400

Public Function listtextwidth (ByRef lstthis as ListBox) as Long
Dim I as Long
Dim TR as RECT
Dim LW as Long
Dim Lwidth as Long
Dim Lhdc as Long

With LstThis.Parent.Font
. Name = LstThis.Font.Name
. Size = LstThis.Font.Size
. Bold = LstThis.Font.Bold
. Italic = LstThis.Font.Italic
End with

LHDC = LstThis.Parent.hdc

' Calendar all the list items to find the longest item
For i = 0 to Lstthis.listcount-1
DrawText Lhdc, Lstthis.list (i),-1, TR, Dt_calcrect
LW = Tr.right-tr.left + 8
If (LW > Lwidth) Then
Lwidth = LW
End If
Next I

' Returns the length of the longest list item (in pixels)
Listtextwidth = Lwidth
End Function

Private Sub Form_Load ()
Dim Astr as String
Dim I
Dim L as Long

L = list1.fontsize * 20/screen.twipsperpixelx
For i = 1 to 10
ASTR = Astr + "We are a very long item" + STR (i)
Next I
List1.AddItem astr + "AAA"
' Join a very long list of items
L = listtextwidth (List1)

SendMessage List1.hwnd, Lb_sethorizontalextent, L, 0
End Sub

First add a ListBox control to Form1, and then add the above code to the Form1 Code window. Run the program, you can see the horizontal scroll bar appears in the list, and the scrolling range is exactly the length of the list item.

3. Make the list responsive to user keystrokes
Sometimes we need the list to automatically adjust the list's ListIndex to the nearest list item based on the user's typed string, just like the editor that dynamically senses user input control properties in VB. The key to the problem is how to find the list item with the specified string in the list, using the lb_findstring message to find the specified string in the list. Here's an example:

Private Declare Function sendmessagestr Lib "user32" Alias "SendMessageA" _
(ByVal hwnd as Long, _
ByVal wmsg as Long, _
ByVal WParam as Long, _
ByVal LParam as String) as Long

Const lb_findstring = &h18f
Dim Astr as String

Private Sub form_keypress (KeyAscii as Integer)
Dim L as Long

Astr = Astr + Chr (keyascii)

L = sendmessagestr (List1.hwnd, Lb_findstring,-1, ASTR)

If L Then
List1.listindex = L
End If
End Sub

Private Sub Form_Load ()
' Adding list items to lists
For i = 85
For j = 85
List1.AddItem chr (i) + Chr (j)
Next J
Next I
End Sub

Private Sub List1_dblclick ()
' Clear the original lookup string
Astr = ""
End Sub

Private Sub list1_keypress (KeyAscii as Integer)
' If you press the letter key, the keystroke message is passed to Form1
If ((KeyAscii >= and KeyAscii <=) Or (KeyAscii >= 97 _
Or keyascii <= 122) Then
KeyAscii = 0
End If
End Sub

First add a ListBox control to Form1, and then add the above code to the Form1 Code window. and set the sorted property of the List1 to true. Run the program, type characters in the list, such as "AV" "GM", the list will highlight similar list items, double-click the list to clear the original input.

In the previous article I introduced you to the application of ListBox class control messages, and in this chapter I will show you how to manipulate a TextBox class control with a message.

1. Get the row and column where the cursor is located
Generally, a more sophisticated text editor typically has the ability to display the rows and columns of the current cursor in the status bar. Use SendMessage to send Edit control type messages to a TextBox control. You can also implement such a function. Let's look at the program first and then analyze it.
First, create a new project in VB and add a TextBox control and two label controls to the Form1. Set the Multiline property of the TextBox control to True. Then add the following code to the Form1 code window:

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "Sendmessagew" _
(ByVal hwnd As Long, ByVal wmsg as Long, ByVal WParam as Long, _
LParam as any) as Long

Private Declare Function sendmessagebyref Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wmsg as Long, wParam as Long, _
LParam as long) as long

Const Em_linefromchar = &hc9
Const Em_lineindex = &hbb
Const Em_getline = &hc4
Const Em_getsel = &hb0

Dim Ilinex, Iliney as Long

Sub Getcurpos (txta as TextBox)
Dim L, L1, L2 as Long
Dim ASTR as String * 256

L = SendMessage (Txta.hwnd, Em_lineindex,-1, 0)
Iliney = SendMessage (Txta.hwnd, Em_linefromchar, L, 0)

Sendmessagebyref Txta.hwnd, Em_getsel, L1, L2
Ilinex = L1-l
Label1.Caption = "column:" + Str (Ilinex)
Label2.Caption = "line:" + Str (Iliney)
End Sub

Private Sub Form_Load ()
Dim IFile
Dim Astr as String

Label1.height = 300:label2.height = 300
Text1.left = 0:text1.top = 0
Text1.Text = ""
Label1.Caption = ""
Label2.Caption = ""

IFile = FreeFile
Open "C:\windows\readme.txt" for Input as #iFile
Do
Line Input #iFile, Astr
Text1.Text = Text1.Text + astr + vbCrLf
Loop Until EOF (iFile)
Close IFile
End Sub

Private Sub form_resize ()
Label1.top = me.scaleheight-300
Label2.top = me.scaleheight-300

Label1.left = 0:label2.left = 1200
Label1.width = 1200
Label2.width = 1200

Text1.width = Me.scalewidth
Text1.height = Me.scaleheight-label1.height
End Sub

Private Sub Text1_click ()
Getcurpos Text1
End Sub

Private Sub text1_keyup (keycode As Integer, Shift as Integer)
Getcurpos Text1
End Sub

Before running the program, make sure that you have c:\windows\readme.txt this file on your hard drive. Otherwise the program will make an error. Then run the program. When you are editing text, you can see the row, column value at the bottom of the window where the current cursor is located. In the program above. We first send a EM_LINEINDEX message that sends the message to return the position of the first character of a line in the entire text control, and if the wparam parameter is set to-1, the character position of the current line is returned. It then sends Em_linefromchar, which sends the message to return the line number where the character is located, based on the character position specified by the parameter wparam, where the first line of the text is 0. This uses these two messages to get the line number where the current cursor is located. To get the column number, send the Em_getsel message first, send the message to return the starting position of the currently selected text, and if no text is selected, returns the position of the character in the text where the current cursor is located. Because the EM_LINEINDEX message above returns the position of the first character of the current line in the text. So subtracting the two value is the column position of the character where the cursor is located. In the above program, if you have Chinese characters in your text, when your cursor moves in a position in the Chinese character, you will see that the column position in the label is increased by 2, because the message sent by SendMessage is not supported in Mandarin, and it makes two characters in the text. This is also a bug in the program (which is why I want to use the Em_getsel message instead of using the TextBox control's SelStart property to get the character position of the cursor, because if you use the value returned by SelStart to count a Chinese character, the same em_ A negative value may be obtained by subtracting Lineindex return values).

2. Declaration API

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd as Long, ByVal wmsg as Long, ByVal Wpara M as long, lParam as any) as long ' declared API


' API constants Declaration

Public Const Wm_sethotkey = &h32
Public Const Hotkeyf_shift = &h1
Public Const Hotkeyf_control = &h2
Public Const Hotkeyf_alt = &h4


Example: Send a message to the form with SendMessage! Let him show it.

Private Sub Form_Load ()
Dim L as Long
Dim Whotkey as Long
Whotkey = (Hotkeyf_alt Or hotkeyf_control) * (2 ^ 8) + 66 ' Set Hotkey
L = SendMessage (Me.hwnd, Wm_sethotkey, Whotkey, 0) ' Wm_sethotkey set Hotkey message
MsgBox "Just press Ctl+alt+b to bring up the form"
me.windowstate = 1 ' form minimized
End Sub

VB SendMessage API Simple usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.