Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

How to create user define window messages

3.00/5 (7 votes)
12 Jun 20072 min read 1   844  
This is to understand and impliment user define window mesages, and how to pass arguments through message
Screenshot - WindowMsgEx.jpg

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()
     //}}AFX_MSG_MAP
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here