Solved

MFC, C++: Creating a user-defined message that calls a function with several parametres

Posted on 2007-11-26
3
3,349 Views
Last Modified: 2013-12-14
Hello,

It has become necessary to send a user defined message. I intend to do this using SendMessage().

I can obtain a handle to the CWnd I want to send the message to.

In essence, I want to call an identical function from any one of various classes by sending a message to the object.

I can see from http://msdn2.microsoft.com/en-us/library/ms644950.aspx that SendMessage() has three parametres, but the function I wish to call has several parametres.

This is one of the class functions (the other ones are identical anyway):

void CSearch::OnChangedRowUpdate(CString description, CString dept, CString source, CString supplier, CString cos)
{
      int i = ProductReport.GetFirstSelectedItem();
      ListReport.SetItemText(i,0,description);
      ListReport.SetItemText(i,2,dept);
      ListReport.SetItemText(i,3,source);
      ListReport.SetItemText(i,4,supplier);
      ListReport.SetItemText(i,5,"cos);

}

How should I go about defining a WM_APP message that will call this (or an identical) function, and how should I send that message so that it will call the function with specified parametres?

Regards,
Sternocera
0
Comment
Question by:sternocera
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20351308
User-defined handlers are declared as

// example for ON_MESSAGE
#define WM_MYMESSAGE (WM_USER + 1)
BEGIN_MESSAGE_MAP( CMyWnd, CMyParentWndClass )
    //{{AFX_MSG_MAP( CMyWnd
    ON_MESSAGE( WM_MYMESSAGE, OnMyMessage )
    // ... Possibly more entries to handle additional messages
    //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

or in your case

#define WM_ROWUPDATE (WM_USER + 1)
BEGIN_MESSAGE_MAP( CMyWnd, CMyParentWndClass )
    //{{AFX_MSG_MAP( CMyWnd
    ON_MESSAGE( WM_ROWUPDATE , OnChangedRowUpdate)
    // ... Possibly more entries to handle additional messages
    //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

However, the problem still remains that there are only two parameters. You could however wrap up yours in a struct and pass a pointer to that as LPARAM, e.g.

struct RowUpdateData
{
 CString description;
 CString dept;
 CString source;
 CString supplier;
 CString cos;
}

LRESULT CSearch::OnChangedRowUpdate(WPARAM, LPARAM lParam)
{
      RowUpdateData* p = (RowUpdateData*) lParam
      int i = ProductReport.GetFirstSelectedItem();
      ListReport.SetItemText(i,0,p->description);
      ListReport.SetItemText(i,2,p->dept);
      ListReport.SetItemText(i,3,p->source);
      ListReport.SetItemText(i,4,p->supplier);
      ListReport.SetItemText(i,5,p->cos);

}
0
 

Author Comment

by:sternocera
ID: 20356504
I'm attempting to call SendMessage:

SendMessage(pParentWindow->m_hWnd,WM_ROWUPDATE, 0, &Mystruct);

Visual studio complains:

error C2660: 'CWnd::SendMessageA' : function does not take 4 arguments

According to http://msdn2.microsoft.com/en-us/library/ms644950.aspx , the function does take 4 arguments. I need to be able to send it to my CDialog's parent window (which varies), a handle to which is obtained by:

CWnd* pParentWindow = GetParent();
pParentWindow->m_hWnd;

Thanks,
Sternocera
0
 

Author Comment

by:sternocera
ID: 20356582
oh, I see.

It was simply a matter of calling ::SendMessage

Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
has77  challenge 9 72
post4 challenge 28 93
Is COM supported from Apache 1 44
JQuery serialize and unserialize 8 49
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now