RegisterWindowMessage

RegisterWindowMessage function

 

Defines a new window message that is guaranteed to be unique throughout the system. The message value can be used when sending or posting messages.

Syntax

 
UINT WINAPI RegisterWindowMessage(
  _In_  LPCTSTR lpString
);

Parameters

lpString [in]

Type: LPCTSTR

The message to be registered.

Return value

Type:

Type: UINT

If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications.

If two different applications register the same message string, the applications return the same message value. The message remains registered until the session ends.

Only use RegisterWindowMessage when more than one application must process the same message. For sending private messages within a window class, an application can use any integer in the range WM_USERthrough 0x7FFF. (Messages in this range are private to a window class, not to an application. For example, predefined control classes such as BUTTONEDITLISTBOX, and COMBOBOX may use values in this range.)

Samples:

1.sender

static UINT sBroadcastCommand = ::RegisterWindowMessage( _T("BroadcastCommand"));

int _tmain(int argc, _TCHAR* argv[])
{    

    ::PostMessage( (HWND)HWND_BROADCAST, sBroadcastCommand, 111 ,    233 );
    
    return 0;
}

2.receiver

static UINT UM_BROADCAST_CMD = ::RegisterWindowMessage( _T("BroadcastCommand")); 

BEGIN_MESSAGE_MAP(Ctest_receive_broadcastmsgDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_REGISTERED_MESSAGE(UM_BROADCAST_CMD, OnBroadcastCommand)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// Ctest_receive_broadcastmsgDlg message handlers

LRESULT Ctest_receive_broadcastmsgDlg::OnBroadcastCommand(WPARAM wParam, LPARAM lParam)
{
    printf("OnBroadcastCommand: %d  , %d  
", wParam, lParam);

    return S_OK;
}

3.console application receiver test failed.

int _tmain(int argc, _TCHAR* argv[])
{

    DWORD pid = GetCurrentProcessId();   
    DWORD tid = GetCurrentThreadId();   

UINT UM_BROADCAST_CMD = ::RegisterWindowMessage( _T("BroadcastCommand"));  UINT msgSigInt
= RegisterWindowMessage((LPCWSTR)"SIGINT"); UINT msgSigTerm = RegisterWindowMessage((LPCWSTR)"SIGTERM"); MSG msg;/* Make a message queue -- this is the method suggested by MS */ //PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); printf("My process id: %d ", pid); printf("My thread id: %d ", tid); printf("SIGINT message id: %d ", msgSigInt); printf("SIGTERM message id: %d ", msgSigTerm); printf("Entering loop... "); fflush(stdout); while (GetMessage(&msg, NULL, 0, 0)) { printf("Received message: %d ", msg.message); if (msg.message == msgSigInt) { printf("SIGINT received, continuing. "); } else if (msg.message == msgSigTerm) { printf("SIGTERM received, continuing. "); } else if (msg.message == UM_BROADCAST_CMD) { ////////////////////////////////////////////////////////////////////////// //HWND_BROADCAST:消息被寄送到系统的所有顶层窗口,包括无效或不可见的非自身拥有的窗口、 被覆盖的窗口和弹出式窗口。消息不被寄送到子窗口 //导致失败。。。。。。。。。。。。。。。。。控制台接收不到。 printf("sBroadcastCommand received, continuing %d:%d. ", msg.wParam, msg.lParam); } fflush(stdout); } printf("Exiting. "); fflush(stdout); return 0; }

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644947(v=vs.85).aspx

原文地址:https://www.cnblogs.com/iclk/p/3560049.html