Click here to Skip to main content
Licence 
First Posted 12 Jun 2007
Views 15,855
Downloads 241
Bookmarked 15 times

How to create user define window messages

By | 12 Jun 2007 | Article
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

About the Author

venura c.p.w. goonatillake

Software Developer (Senior)

Sri Lanka Sri Lanka

Member

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++.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberravishankar.rh2:47 12 Oct '10  
GeneralInheritance, delegation, composition PinmemberSiva Sankar Koyi23:15 12 Jun '07  
GeneralRe: Inheritance, delegation, composition Pinmembervenura c.p.w. goonatillake16:30 13 Jun '07  
GeneralRe: Inheritance, delegation, composition PinmemberEd Gadziemski16:07 30 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Jun 2007
Article Copyright 2007 by venura c.p.w. goonatillake
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid