Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

757 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

19 Experts available now in Live!

Get 1:1 Help Now