1. 程式人生 > >MFC學習(18)MFC中利用HWND_BROADCAST 向其它所有視窗廣播訊息

MFC學習(18)MFC中利用HWND_BROADCAST 向其它所有視窗廣播訊息

利用HWND_BROADCAST可以向其它程序,或DLL檔案的視窗等傳送訊息.

在MSDN中如此描述的:

    Handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
遮擋的視窗也可收到訊息.

首先要定義訊息:

必須使用RegisterWindowMessage,是Windows的註冊訊息變數;

如:

static UINT NEAR WM_OVER_LOAD= RegisterWindowMessage(_T("OVER_LOAD"));

其次巨集宣告為
ON_REGISTERED_MESSAGE(WM_OVER_LOAD, OnOverLoad) 函式宣告和普通訊息的函式宣告一樣
afx_msg LRESULT OnOverLoad(WPARAM wParam, LPARAM lParam);

傳送訊息例如:

::SendMessage( (HWND)HWND_BROADCAST,WM_OVER_LOAD, 引數1 ,引數2 );

::PostMessage( (HWND)HWND_BROADCAST,WM_OVER_LOAD, 引數1 ,引數2 );

如此所有的註冊了此訊息的視窗都可以收你發的訊息了