Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-26
3
Medium Priority
?
3,459 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

722 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