Home Search Contact us About us
Title Posting Messages
Summary Posting Messages within an application is a simple method of communicating within various windows using message queues that don't tie up the system.
Contributor John McTainsh
Published 23-Sep-2000
Last updated 23-Sep-2000
Page rating   75% for 4 votes Useless Brilliant

Description.

If the handle or pointer to the CWnd is available it is a simple task to post a message to that window with numbers or a pointer to some memory. See the article on "Getting pointers to Documents and Views" to see how to navigate the application.

Setting up the Post/Send message.

A message must be defines higher than WM_USER. A good place for this is in the StdAfx.h
#define UNUSED_MEMORY           (0xcd)              //Initialised memory   
#define FREED_MEMORY            (0xdd)              //Deleted memory
#define ASSERT_USED(x)          ASSERT( (BYTE)x[0] != UNUSED_MEMORY )
#define ASSERT_NOT_DELETED(x)   ASSERT( (BYTE)x[0] != DELETED_MEMORY )
   
#define WM_MY_NUM_MESSAGE       (WM_USER+23)        //Number posting message
#define WM_MY_STRING_MESSAGE    (WM_USER+24)        //Memory posting message
Post or Send the the message to the other window as follows. Note Post will return immediately and be processed when the the system is idle to process messages. Send will be processed immediately. Any memory allocated for the PostMessage should be freed when processed to ensure it is not deleted before it is used. For safety it is best to do this for SendMessage as well.
    CWnd* pWndOther = AfxGetMainWnd();    //Get pointer to the window
    ASSERT( pWndOhter );
    
    //Post some numbers and Wait for return
    pWndOther->SendMessage( WM_MY_NUM_MESSAGE, wSomeWord, lSomeLong );

    //Post some Memory
    LPCTSTR lpszName = new TCHAR[MY_STR_LEN+1];
    if( lpszName != NULL )
    {
        strncpy( lpszName, _T("Some name data"), MY_STR_LEN );
        lpszName[MY_STR_LEN-1] = NULL;
        pWndOther->PostMessage( WM_MY_STRING_MESSAGE, wSomeWord, lpszName );
    }
    else
    {
        //TODO : Process no memory error
    }

Processing the Message.

Notification of the message will be come through the message handler the functions should be defined in the header/definition.
    //}}AFX_MSG
    afx_msg LONG OnNummber( WPARAM wSomeWord, LPARAM lSomeLong );
    afx_msg LONG OnString( WPARAM wSomeWord, LPARAM lName );
    DECLARE_MESSAGE_MAP()
In the implementation file add to the message map as follows.
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_MY_NUM_MESSAGE, OnNumber)
    ON_MESSAGE(WM_MY_STRING_MESSAGE, OnString)
END_MESSAGE_MAP()
Now process the message.
// ***************************************************************************
//DESCRIPTION:
//      Do something with the numbers
//PARAMS:
//      wSomeWord   Blah blah ....
//      lSomeLong   Blah blah ....
//RETURN:
//      no relevant for PostMessage but used for SendMessage
//CREATED:
//    26-6-2000, 14:59:17 by john@mctainsh.com
// ***************************************************************************
LONG CMainFrame::OnNumber( WPARAM wSomeWord, LPARAM lSomeLong )
{
    TRACE( _T("CMainFrame::OnNumber( %d, %ld )\n"), wSomeWord, lSomeLong );
    return( wSomeWord + lSomeLong );
}



// ***************************************************************************
//DESCRIPTION:
//      Do something with the string
//PARAMS:
//      wSomeWord   Blah blah ....
//      lName       Blah blah ....
//RETURN:
//      no relevant for PostMessage but used for SendMessage
//CREATED:
//    26-6-2000, 14:59:17 by john@mctainsh.com
// ***************************************************************************
LONG CMainFrame::OnString( WPARAM wSomeWord, LPARAM lName )
{
    TRACE( _T("CMainFrame::OnString( %d, %ld )\n"), wSomeWord, lName );
    LPCTSTR lpszName = (LPCTSTR)lName;           //Case MUST be appropiate
    ASSERT( lpszName != NULL );
    ASSERT_USED( lpszName );
    ASSERT_NOT_DELETED( lpszName );
    SetWindowTitle( lpszName  );
    
    delete [] lpszName;
    
    //Return something useful only if the SendMessage is used
    return( wSomeWord + 1 );
}
Comments Date
Home Search Contact us About us