Introduction
This is a simple application, that enables people to understand how to create there own window messages. Here my main attempt is to give a basic understanding of window messages and how to implement it in your application.
Background
When we are talking about window messages there are two ways to send messages. All these messages going through message queue. This guarantees that each function call attach to windows message will not execute simultaneously. So the two ways are:
- Post
Post message will return soon after message call sends to message queue, it does not wait message executes its' attached function. - Send
Send message only returns after it executes its attached function.
Using the code
ON_MESSAGE Vs ON_REGISTERED_MESSAGE
ON_MESSAGE and ON_REGISTERED_MESSAGE will perform almost similar functionality but the usage is differed. Above functions will need two arguments.
Parameters
message
The message ID.
memberFxn
The name of the message-handler function to which the message is mapped.
If programmer uses his own define value as message id, then ON_MESSAGE becomes valid
function call. But this do not have any guarantee that user defined value as message id is
unique value in the application. This will cause more problems.
To have a unique value user can use following statement,
static const UINT UWM_CALL_MSG_WITH_PARAMETERS = ::RegisterWindowMessage(_T("UWM_CALL_MSG_WITH_PARAMETERS"));
RegisterWindowMessage( ) will return a unique value. If programmer derived his message id
from this function, he has to use ON_REGISTERED_MESSAGE function. If you need more
information about these, please refer to MSDN online document.
When you define a message function, you have to use following syntaxes
LRESULT onClickCallWindowMessages(WPARAM wParam, LPARAM lParam);
In your application message map, you have to map user define message id with the function
as follows.
BEGIN_MESSAGE_MAP(CwindowMsgExDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SHOW, OnBnClickedButtonShow)
ON_REGISTERED_MESSAGE(UWM_ON_CLICK_SHOW_BUTTON,onClickCallWindowMessages)
ON_REGISTERED_MESSAGE(UWM_CALL_MSG_WITH_PARAMETERS,onCallWindowMessageWithParameters)
ON_BN_CLICKED(IDC_BUTTON_CALL_WITH_PARAM, OnBnClickedButtonCallWithParam)
END_MESSAGE_MAP()
In my demo application, it demonstrates how to trigger user define window message and how
to pass arguments through window messages. Pass an argument to window message you
should allocate it heap, because it's not another function call, when message is called local
variable scope get lost, so you should create it in heap and should pass the reference to
message function.
m_ctrlCHECKWithParam.EnableWindow(true);
m_ctrlCHECKWithParam.SetCheck(BST_CHECKED);
m_lEDITInWParam = *(long*)wParam;
m_strEDITInLParam = *(CString*)lParam;
UpdateData(false);
delete (long*)wParam;
delete (CString*)lParam;
It is your responsibility to delete created variable in head in the message function.
m_ctrlCHECKWithParam.EnableWindow(true);
m_ctrlCHECKWithParam.SetCheck(BST_CHECKED);
m_lEDITInWParam = *(long*)wParam;
m_strEDITInLParam = *(CString*)lParam;
UpdateData(false);
delete (long*)wParam;
delete (CString*)lParam;
I hope you will get some benifit from this code to your knowledge. In future I am thinking to
submit much more advance programming examples to assist you. Hope you enjoy this.