Solved

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

Posted on 2007-11-26
3
3,434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

623 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