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 will bring a lot of convenience to programming work. This paper uses Visual Basic as an example, combined with several specific examples to introduce the function of the function.
I. Introduction to the SendMeaasge 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 lib "user32" Alias "SendMessagea" (Byval Hwnd As Long, Byval WParam As Long, LParam as an AS Long
The meaning of four of these independent variables 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 understanding the formats and functions of the SendMessage function, let us take a look at its power.
Second, sendMessage functions use examples
Example 1 Multi-line TEXTBOX's quick processing feature we often encounter the following situations when handling multi-line TextBox:
I hope to understand how many lines of writing in multi-line TextBox.
I 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 method of completing 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 Schedule.
Below with a simple example demonstrate these two features:
New project, add three TextBox on Form1 (names for Text1, TXTLINECount, TXTString, Multi to Text1
Line attribute is set to true), three tags and a command button. Add a module moudle1 to the project, write the following statement (where
The declaration of the SendMessage function can be copied from the VB's "API Browser":
Message constant name message Value WParamlParam Return Value EM_GETLINECOUNT & HBA Unused line number EM_GETLINE & HC4 to find the line number result of the line number result of the result of the number of bytes of the number of bytes
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const EM_GETLINECOUNT = & HBAPublic Const EM_GETLINE = & HC4 write the following code in Form1 code module : Private subss1_click () Dim Str (256) AS BYTE STR (1) = 1 'Maximum allowable storage 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) 'Getting the data of the third line in the STR, converted to the string to the character string SendMessage Text1.hwnd, Em_getline, 2, Str (0) txtString = String = Str, and Str, vbunicode, and 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, the program correctly statistics 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, and this example will be high (ie, STR (1)). Note Maximum allowable 256 characters. Example 2 Program Control pulls down or put away the pull-down column of the combination box, in order to pull or collect the drop-down list of the combination box, you need to operate with a keyboard or mouse, and sometimes we want to automatically Pull out the drop-down list (for example in some demo procedures), in order to achieve this, we only have to send a CB_SHOWDROPDOWN message to the combo box by means of the SendMessage function. When sending a CB_SHOWDROPDOWN message, the WPARAM parameter determines whether it is a list (= true) or a list of collapses (= false), LParam is useless (set to 0). To illustrate the specific method, a simple program fragment is provided below.
First, make the following statements in the code module: Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongConst CB_SHOWDROPDOWN = & H14F When the program somewhere When you need to pull a list of combination boxes, write the following call statement: sendMessage Combol.hwnd, CB_Showdropdown, True, 0 When you need to hold a list of combine box combol, write the following statement: sendMessage Combol.hwnd, Cb_Showdropdownn, False, 0 Example 3 Finding items in the list box generally have a "index" page in the Win95 style help system, 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 items in text boxes to provide users 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 achieved), if you want this feature if you want to achieve this property elsewhere, 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 For the Listbox Add Level Roller 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. Come out, so it is necessary to add a horizontal scroll bar to Listbox to make it easy. To add a horizontal scroll bar, just 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, can also give a number greater than the maximum text width, such as this example of 250), LParam is useless.