Powerful SendMessage function

zhaozj2021-02-11  218

Powerful SendMessage Functions Windows API (Application Interface) is a powerful "weapon library" provided by the Windows Series software for program developers. In this weapon, there are a lot of powerful weapons, and SendMessage is one of them. Its function is very rich, flexible uses this function, which 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. First, the SendMeaAsge function brief introduction, 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 understanding the formats and functions of the SendMessage function, let us take a look at its power. Second, SendMessage Functions Using Example 1 Multi-line TEXTBOX Fast Processing Functions in TEXTBOX We often encounter the following situations: I hope to know how many lines of text 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 demonstrates these two features: New project, add three TextBox on Form1 (names for 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 writes the following statement (where the SendMessage function can be replicated from the VB "API Browser"):

Message Constant Name Message Value WParamlParam Return Value Em_getLineCount & HBA Unused Row Em_getLine & HC4 To find the line number result of the resulting string of the resulting string of the resulting String 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 Sub Command1_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 in txtLineCount = sendMessage (Text1.hWnd, EM_GETLINECUNT, 0, 0)' Get data in the third line in STR, conversion After the string is displayed in the text box txt1.hwnd, em_getline, 2, str (0) txtString = str, 2, and vbunicode) End Sub, press F5 to run the program, type a few lines in the multi-line text box Text, then press the [OK] button, appear as shown, indicating that the program correctly counts the number of the total number 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.

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

New Post(0)