PostMessage And SendMessage in C Language

Source: Internet
Author: User

 

PostMessage:

Function: This function puts a message (sent) to the Message Queue associated with the thread created in the specified window. It is an asynchronous message mode and will be returned without waiting for the thread to process the message. Messages in a message queue are obtained by calling GetMessage and PeekMessage.

 

Function prototype: B00L PostMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );

 

Parameters

 

HWnd: the handle of the window in which the window program receives messages. Two values with specific meanings are recommended:

 

HWND_BROADCAST: all top-level windows sent to the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows. Messages are not sent to subwindows.

 

NULL: the operation of this function is the same as that of the PostThreadMessage function that calls the dwThread parameter to set it to the current thread identifier.

 

Msg: Specifies the message to be sent.

 

WParam: specify the specific information of the attached message.

 

IParam: specify the specific information of the attached message.

 

Return Value: If the function call is successful, a non-zero value is returned. If the function call fails, the return value is zero. To obtain more error information, call the GetLastError function.

 

Note: applications that need to communicate in HWND_BROADCAST mode should use the RegisterwindwosMessage function to obtain unique messages for inter-Application Communication.

 

If you send a message below the WM_USER range to the asynchronous message function (PostMessage. sendpolicymessage, SendMesssgeCallback), the Message Parameter cannot contain a pointer. Otherwise, the Operation will fail. The function will receive a message before the thread processes the message, and the sender will be released before the memory is used.

 

Quick query: Windows NT: 3.1 and later; Windows: 95 and later; Windows CE: 1.0 and later; header file: winuser. h. Input Library: user32.lib; Unicode: Unicode and ANSI in Windows NT.

 

P.S. Why does the Message Parameter cannot contain pointers if a message below the WM_USER range is sent to the asynchronous message function (PostMessage. sendpolicymessage, SendMesssgeCallback. Otherwise, the Operation will fail.

PostMessage in HTML5 Standard

PostMessage is added to HTML5. postMessage supports Cross-Document message transmission, Internet Explorer 8, Firefox 3, Opera 9, Chrome 3, and Safari 4.

 

You can bind the message event of the window to listen for cross-document message transmission content.

 

SendMessage:

Function. This function sends a specified message to one or more windows. This function calls the window program for the specified window until the window program finishes processing the message and returns it. The PostMessage function returns a message immediately after it is sent to the Message Queue of a thread.

 

LRESULT SendMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM IParam );

 

Parameters:

 

HWnd: the handle of the window whose window program will receive messages. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows, however, messages are not sent to subwindows.

 

Msg: Specifies the message to be sent.

 

WParam: Specify the additional message.

 

IParam: Specify the attached message.

 

Return Value: the return value specifies the message processing result, depending on the sent message.

 

Note: applications that need to communicate with HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for communications between applications.

 

If the specified window is created by a calling thread, the window program is called as a subroutine immediately. If the specified window is created by different threads, the system switches to the thread and calls the appropriate window program. Messages between threads are processed only when the thread executes the message retrieval code. The sending thread is blocked until the receiving thread finishes processing the message.

 

Windows CE: Windows CE does not support all messages supported by Windows desktop. Before using SendMesssge, check whether the sent message is supported.

 

Quick query: Windows NT: 3.1 and later versions: Windows: 95 and later versions; Windows CE: 1.0 and later versions; header file: winuser. h. Input Library: user32.lib; Unicode: Unicode and ANSI in Windows NT.

 

SendMessage function instance

 

Program Control to pull down or collapse the drop-down column 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 ).

------------- C ++ -----------------

 

# Include "windows. h"

 

Int main (int argc, char * argv [])

 

{

 

HWND hWnd =: FindWindow (NULL, "QQ2010 ");

 

If (hWnd! = NULL)

 

{

 

: SendMessage (hWnd, WM_CLOSE, 0, 0 );

 

}

 

Return 0;

 

}

 

SendMessage Function Description: sends a specified message to one or more windows. This function is used to call the window process specified by the window. It is not returned until the Window Process completes message processing.

. Function prototype:

LRESULT SendMessage (

HWND hWnd, // target window handle

UINT Msg, // message to be sent

WPARAM wParam, // The first message Parameter

LPARAM lParam // The second Message Parameter

);

 

. Parameters:

HWnd

Window handle for receiving messages during the window process. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows of the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows. Messages are not sent to subwindows.

Msg

Specifies the message to be sent;

WParam

Specify the specific information of the additional message;

LParam

Specify the specific information of the additional message.

 

. Return Value:

The Return Value Returns the message processing result, which depends on the sent message.

 

. Note:

Applications that need to communicate in HWND_BROADCAST mode should use the RegisterWindowMessage function to obtain unique messages for inter-Application Communication.

If the specified window is created through the call thread, the window process is called immediately as a subroutine. If the specified window is created by calling different threads, the system switches to this thread and calls the appropriate window process. Messages between threads are processed only when the receiving thread executes the message retrieval code. The sending thread will be blocked until the receiving thread finishes processing the message.

Windows CE: Windows CE does not support all messages supported by Windows desktop. Before using the SendMessage function, check whether the sent message is supported by Windows CE.

 

. Environment:

Windows NT: 3.1 or later;

Windows: 95 or later;

Windows CE: 1.0 or later;

Header file: winuser. h;

Input Library: user32.lib;

Unicode: Unicode and ANSI are implemented in windows.

 

. Sample Code:

Case WM_KEYDOWN:

Switch (wParam)

{

Case VK_HOME:

SendMessage (hwnd, WM_VSCROLL, SB_TOP, 0 );

Break;

 

Case VK_END:

SendMessage (hwnd, WM_VSCROLL, SB_BOTTOM, 0 );

Break;

 

Case VK_PRIOR:

SendMessage (hwnd, WM_VSCROLL, SB_PAGEUP, 0 );

Break;

...

}

Return 0;

...

 

. FAQs

1) Use SendMessage to cut, copy, and paste messages.

 

SendMessage (hwnd, WM_COPY, 0, 0 );

SendMessage (hwnd, WM_CUT, 0, 0 );

SendMessage (hwnd, WM_PASTE, 0, 0 );

2) differences between SendMessage and PostMessage

PostMessage puts the message into the message queue and returns immediately. SendMessage is not returned until the message is processed in the window.

3) SendMessage send WM_COPYDATA messages to transmit data between processes

A WM_COPYDATA message is designed to allow a small amount of read-only data to be transmitted between processes. We recommend that you use the SendMessage () function in the SDK documentation. The receiver does not return data before the data is copied, so that the sender cannot delete or modify data.

For example:

Std: string strData = "VC Knowledge Base VCKBASE. COM ";

COPYDATASTRUCT cds;

Cds. dwData = 0;

Cds. cbData = strData. Length ();

Cds. lpData = strData. c_str ();

: SendMessage (hwnd, WM_COPYDATA, NULL, (LPARAM) & cds );

Related Article

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.