Solved

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

Posted on 2007-11-26
3
3,360 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

813 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

10 Experts available now in Live!

Get 1:1 Help Now