Click here to Skip to main content
15,906,455 members
Articles / Desktop Programming / MFC
Article

How to create user define window messages

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
12 Jun 20072 min read 37.7K   837   16   4
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


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
I am working as a Tech Lead. I love VC++.
I am trying to get into new technologies coming with VC++ and also in other areas too.

Currently I am working in C# .Net as well...

Now I have sound knowledge in C# as well as in VC++.

Comments and Discussions

 
GeneralMy vote of 4 Pin
ravishankar.rh12-Oct-10 2:47
ravishankar.rh12-Oct-10 2:47 
GeneralInheritance, delegation, composition Pin
Siva Sankar Koyi12-Jun-07 23:15
Siva Sankar Koyi12-Jun-07 23:15 
GeneralRe: Inheritance, delegation, composition Pin
venura c.p.w. goonatillake13-Jun-07 16:30
venura c.p.w. goonatillake13-Jun-07 16:30 
I think you can get a good idea from these sites

Hi,

Inheritance Vs Composition

http://www.artima.com/designtechniques/compoinh.html

Inheritance Vs Deligation

http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf

In pure inheritance, you will inherit all the inheritable items in the super class to base class (depending on the scope of the attributes and methods in super class). But there is some situations where you cannot see direct relationship between classes, where you can not inherit straight-away.

It depends on your design, when you can see a seperate class which involves in your subclass
which actually do not have a direct inhritance to your subclass, we will make that class as a attribute of your sub class, this scenario known as deligation.

Thank You
Venura

VENURA GOONATILLAKE, SRI LANKA

GeneralRe: Inheritance, delegation, composition Pin
Ed Gadziemski30-Jul-07 16:07
professionalEd Gadziemski30-Jul-07 16:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.