Powerful SendMessage function

zhaozj2021-02-11  273

Powerful SendMessage function

Windows API is a powerful "weapon library" provided by the Windows Series software for program developers. In this weapon database, there are a lot of powerful weapons, and SendMessage is one of them, and its function is very rich. , Flexible use this function, which will bring a lot of convenience to programming work. This article uses Visual Basic as an example, combined with several specific examples to introduce the functionality of the function.

I. Introduction to SendMessage Function

As the name suggests, the functionality of the SendMessage function is "Send Message", which is to send a message to the specified object (operating system, window or control, etc.) to produce a specific action (such as scrolling, modifying the object appearance, etc.).

The function of the SendMessage function in VB is 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 LONGs These four arguments And the description is as follows:

HWND: The handle of the object.

I hope to transfer the message to which object is transmitted, and the handle of the object is sent as a real parameters. You can get a handle of an object with "object .hwnd" in VB, such as Text1.hWnd and Form1.hWnd you can get Text1 And the handle of Form1.

WMSG: The message sent.

Depending on the specific needs and different objects, different messages are transmitted as real parameters to produce expected action.

WPARAM, LPARAM: Additional message information.

These two are optional parameters to provide more information about WMSG messages, different WMSGs may use 0, 1 or 2 in these two parameters. If you don't need any additional parameters, you will be entered. For NULL (0% in VB).

After briefly understand the format and functionality of the SendMessage function, let us take a few examples to see its power:

Second, sendMessage functions use examples

Example 1. Quick processing function in multi-line TEXTBOX

We often encounter the following situations when dealing with multi-line TextBox:

I hope to understand how many lines of text in multi-line TEXTBOX; want to quickly return the text of the Nth line;

For the above situation, if you are implemented with VB itself, you must write a short code, and because the order lookup is used to complete, the execution efficiency of the code is also very low. If you use the SendMessage function, you can reduce the amount of code and greatly improve the performance efficiency.

The way to complete the above two tasks with the SendMessage function is very simple. Each task simply sends a message to multi-line TextBox, two messages are: em_getlinecount, em_getline, other parameters, and return values ​​See the table below:

Message constant name message Value WPARAM LPARAM Return Value EM_GETLINECOUNT & HBA Unused row number EM_GETLINE & HC4 to find the line number of the line number of the resulting string result

Below with a simple example demonstrate these two features:

New projects, add three TextBox on Form1, named Text1, TXTLINECount, TXTString, set the multiline property of Text1 to True, three tags, and a command button. Add a module moudle1 to the project, which is written in it (where the SendMessage function can be replicated from the "API browser" of VB): 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 const EM_GETLINECUNT = & HBA public const EM_GETLINE = & HC4

Write the following code in the code module of Form1:

Private submmand1_click ()

Dim Str (256) as Byte

Str (1) = 1 'maximum allowable 256 characters

'Get the total number of lines, the result is displayed in the text box TXTLINECOUNT

TXTLINECount = sendMessage (text1.hwnd, em_getlinecount, 0, 0)

'Get data on the third line in the STR, convert to a string to display in text box txtString

SendMessage Text1.hWnd, Em_getline, 2, Str (0)

TXTSTSTRING = STRCONV (STR, VBUNICODE)

End Sub

After that, press F5 to run the program, type a few lines of text in the multi-line text box, then press the "OK" button, appear as shown, indicating that the program correctly counts the total number of lines and the third line.

Two points to supplement: When calling sendMessage to get the nth row string, LPARAM needs to be explained as the byte array. After the call is completed, the byte array is converted to a string; in addition, the first two of the LPARAM before calling The byte refers to the maximum length allowed to be stored, where the first byte is low, the second byte is high, this example will be high (ie, STR (1)) 1, indicating that the maximum allowable storage of 256 characters.

Example 2, program control pulls down or puts the drop-down list of combination boxes

In general, in order to pull or collect the drop-down list of the combo box, you need to operate with a keyboard or mouse, and sometimes we want to automatically pull the drop-down list at a time, such as in some demo programs), in order to implement This goal, we also only send a CB_SHOWDROPDOWN message to the combo box by means of the SendMessage function.

When you send a CB_SHOWDROPDOWN message, the WPARAM parameter determines whether it is a list (= true) or the column list (= false), LParam is useless (set to 0)

To illustrate specific usage, provide a simple program fragment:

First, do the following statement in the code module:

Declare function sendMessage Lib "User32" Alias ​​"SendMessagea" (Byval HWnd As Long, Byval WParam As Long, LParam as aless as longconst CB_Showdropdown = & H14F

When you need a list of combine box COMBO1, you need to pull a list of combine box combo1, write the following:

SendMessage Combo1.hWnd, Cb_Showdropdown, True, 0

Write the following statement when you need to collect the list of combine box combo1, write the following statement:

SendMessage Combo1.hWnd, Cb_Showdropdown, False, 0

Example 3, find a matching item in the list box

In Win95 style help, there is generally a "index" page, the index page contains a text box and a list box, when the user enters text in the text box, the drop-down list will dynamically display the most matching text box in text box Projects provide the user with the greatest convenience. This effect is easy to implement in the application's help system (as long as the normal production process of WIN95 helps system) can be implemented, if you want to achieve this property in other parts of the application, you will have a thought.

It is very simple to use the SendMessage function. It is very simple, even a statement is enough, that is, send a lb_findstring (& H18F) message to the list box in the Change event of the text box, which tells the list box to find match in the list s project.

When the LB_FINDSTRING message, the WPARAM parameter represents which item from the list box starts to find it, in general, the parameter can be set to -1, indicating that the first item starts from LIST1 (0), and the LParam is passed. Controversy strings (must be passed).

The specific code and running screen are demonstrated together with the following example 4.

Example 4, add a horizontal scroll bar for Listbox

In VB, the list box control only provides a vertical scroll bar, and does not set the ability to set a horizontal scroll bar. When some items have a long text width, the text exceeding the list box width is not displayed, so it is necessary to Listbox adds a horizontal scroll bar to facilitate operation.

To add a horizontal scroll bar, simply send a LB_SETHORIZONTALEXTENT (& H194) message to the list box, when sending a message, WPARAM is the length of the scroll bar (in pixels, can be calculated to draw accurate length, or casually A number greater than the maximum text width, such as 250 of this example, LParam is useless.

The following is the code and running screen combined together with Example 3 and Example 4:

Declare function lib "user32" Alias ​​"SendMessagea" (Byval Hwnd As Long, Byval WParam As Long, LParam as an AS Long

Public const lb_findstring = & h18f

Public const lb_sethorizontaleXtent = & H194

Private sub flow_load ()

List1.additem "software"

List1.additem "Computer Game"

List1.additem "TV"

List1.additem "TV" list1.additem "Computer"

List1.additem "Computer Game Software"

'The next sentence is the list box to add a horizontal scroll bar

SendMessage List1.hWnd, LB_SethorizontalExtent, 250, 0

End Sub

Private sub text1_change ()

'note! When LPARAM is incorporated in a string, you must pass byval.

List1.listindex = sendMessage (List1.hwnd, lb_findstring, -1, byval text1.text)

End Sub

Through several examples, you must have a preliminary understanding of the powerful features of the SendMessage function. In fact, we can also complete more and better tasks, such as the automatic scroll screen of the text box, implement the text editing process. The Undo function, manipulating the form of the application, etc., the readers of interest, please refer to information about the Windows API.

转载请注明原文地址:https://www.9cbs.com/read-4103.html

New Post(0)