Powerful sendmessage Function

Source: Internet
Author: User

 

Powerful sendmessage Function

Windows API (application interface) is a powerful "weapons library" provided by Windows software for program developers. In this weapon library, there are many powerful weapons, sendmessage is one of them. It has rich functions. flexible use of this function will bring a lot of convenience to programming. This article takes Visual Basic as an example, this section describes the functions of a function based on several examples.

1. Introduction to sendmessage Functions

As the name suggests, the sendmessage function is used to send a message to a specified object (such as an operating system, window, or control, to generate specific actions (such as scrolling and modifying the Object Appearance ).

The sendmessage function in VB is described 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

The meanings and descriptions of the four independent variables are as follows:

  • Hwnd: the handle of the object.

If you want to transmit a message to an object, you can use the handle of the object as a real parameter. In VB, you can simply use the object. hwnd "to get the handle of an object. For example, text1.hwnd and form1.hwnd can get the handles of text1 and form1 respectively.

  • Wmsg: The sent message.

Different messages are transmitted as real parameters based on specific requirements and different objects to produce the expected actions.

  • Wparam and lparam: additional message information.

These two are optional parameters used to provide more information about wmsg messages. Different wmsg may use 0, 1, or 2 of these two parameters. If no additional parameter is required, then, the real parameter is assigned null (0 in VB ).

After a brief understanding of the formats and functions of the sendmessage function, let's take a few examples to see its power:

Ii. Example of sendmessage Function

Example 1: fast processing in multi-row textbox

When processing multi-line Textbox, we often encounter the following situations:

  • You want to know how many lines of text exist in multi-row textbox;

  • To quickly return to
  • N lines of text;

In the above case, if you use VB statements or functions to implement it, you need to write a very short code, and because you need to use the sequential search method to complete it, therefore, code execution efficiency is also low. If the sendmessage function is used, the amount of code can be greatly reduced and the execution efficiency can be greatly improved.

Using the sendmessage function to complete the preceding two tasks is very simple. Each task simply sends a message to multiple rows of textbox. The two messages are em_getlinecount, em_getline, the following table lists other parameters and return values:

Message constant name

Message Value

Wparam

Lparam

Return Value

Em_getlinecount

& HbA

Unused

Unused

Number of rows

Em_getline

& Hc4

The row number.

Byte string of the stored result

Result The number of bytes of the byte string.

 

The following uses a simple example to demonstrate these two functions:

Create a project and add three textbox names on form1: text1, txtlinecount, and txtstring. Set the multiline attribute of text1 to true, three labels, and one command button. Add a module moudle1 to the project and write the following declaration in it (the declaration of sendmessage function can be copied from VB's "API Browser ):

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_getlinecount = & HbA

Public const em_getline = & hc4

Write the following code in the form1 code module:

Private sub commandementclick ()

Dim STR (256) as byte

STR (1) = 1' can store up to 256 characters

'Obtain the total number of rows. The result is displayed in txtlinecount.

Txtlinecount = sendmessage (text1.hwnd, em_getlinecount, 0, 0)

'Put the 3rd rows of data in STR, convert it to a string, and display it in the text box txtstring.

Sendmessage text1.hwnd, em_getline, 2, STR (0)

Txtstring = strconv (STR, vbunicode)

End sub

Then, press F5 to run the program, enter a few lines of text in the multi-line text box, and then press "OK" to display the screen, the program correctly counts the total number of lines and 3rd lines of text.

Two additional notes: When sendmessage is called to obtain the n-th line string, lparam needs to be described as a byte array. After the call is complete, the byte array is converted to a string. In addition, before calling, you must specify the maximum length of lparam in the first two bytes. The first byte is low, and the second byte is high. In this example, STR (1) is high )) if the value is set to 1, a maximum of 256 characters can be stored.

 

Example 2: Program Control: Pull down or collapse the drop-down list of the combo box

Generally, you need to use a keyboard or mouse to pull down or collapse the drop-down list of the combo box, sometimes we want to automatically pull the drop-down list (for example, in some demo programs) at a certain time when the program runs. To achieve this, we only need to use the sendmessage function, the method is to send a cb_showdropdown (& h14f) message to the combo box.

When sending a cb_showdropdown message, the wparam parameter determines whether to pull down the list (= true) or collapse the list (= false). lparam is useless (set to 0)

To illustrate the specific usage, the following provides simple program snippets:

First, make the following declaration 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 long

Const cb_showdropdown = & h14f

When you need to pull the combo1 list of the combo box somewhere in the program, write the following call statement:

Sendmessage combo1.hwnd, cb_showdropdown, true, 0

When you need to collapse the combo1 list of the combo box, write the following statement:

Sendmessage combo1.hwnd, cb_showdropdown, false, 0

Example 3: Search for matched items in the list box

In the Win95 style help system, there is generally an "Index" page. The index page contains a text box and a list box. When you enter text in the text box, the drop-down list displays the items that match the text in the text box dynamically, providing users with the greatest convenience. This effect can be easily implemented in the Help System of the application (as long as the normal production process of the Win95 help system is implemented ), if you want to implement this feature elsewhere in the application, you have to worry about it.

Using the sendmessage function to implement the above features is very simple, and even only one statement is enough, that is, to send an lb_findstring (& h18f) message to the list box in the change event of the text box, the message tells The ListBox to search for matched items in the list.

When sending the lb_findstring message, the wparam parameter indicates which item in the list box is followed by the query. In general, this parameter can be set to-1, indicating that from list1 (0) that is, the first item starts Backward Search, and lparam is passed to the string to be searched (values must be passed ).

The specific code and running screen are combined with example 4 for demonstration.

 

Example 4: Add a horizontal scroll bar for ListBox

In VB, the list box control only provides a vertical scroll bar without the ability to set a horizontal scroll bar. When the text width of some items is long, the text beyond the width of the list box cannot be displayed, it is necessary to add a horizontal scroll bar for ListBox to facilitate operations.

To add a horizontal scroll bar, you only need to send an lb_sethorizontalextent (& h194) message to the list box. When a message is sent, wparam is the scroll bar length (in pixels, an accurate length can be obtained through calculation, or a number greater than the maximum text width can be randomly given, such as 250 in this example). lparam is useless.

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

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 lb_findstring = & h18f

Public const lb_sethorizontalextent = & h194

Private sub form_load ()

List1.additem "Software"

List1.additem "Computer Games"

List1.additem "TV"

List1.additem "TV station"

List1.additem "computer"

List1.additem "computer game software"

'Add a horizontal scroll bar to the list box in the next sentence.

Sendmessage list1.hwnd, lb_sethorizontalextent, 250, 0

End sub

Private sub text=change ()

'Note! When lparam is passed in a string, it must be passed in byval.

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

End sub

Through the above examples, you must have a preliminary understanding of the powerful functions of the sendmessage function. In fact, we can use this function to complete more and better tasks, for example, you can control the automatic scrolling of text boxes, implement the Undo function during text editing, and manipulate the form control menu of applications. For more information, see Windows API.

 

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.