2
votes

Let's say I have a list component class called ListCtrl that derives from CWnd.

And let's say I also have a dialog class called DialogA that derives from CDialog.

DialogA uses ListCtrl to map it to a list component. For example,

void DialogA::DoDataExchange(CDataExchange* pDX) 
{ 
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_CONTROL, listCtrl);
}

Where

ListCtrl listCtrl;

So if ListCtrl were to call the SendMessage(), can DialogA handle it?

If not, how can I have DialogA handle something that ListCtrl does.

Ultimately, I want DialogA to use a "copy" function of it's own when the ListCtrl right-click menu option for "Copy" is clicked, and prevent the copy function of ListCtrl from executing.

2

2 Answers

4
votes

CWnd::SendMessage will send a message to the window wrapped by that CWnd derived class. So if you use SendMessage from your ListCtrl (which is a child window of your dialog), the dialog won't see it.

You either need to have the raw HWND of the dialog window and use the global SendMessage like:

::SendMessage(hWnd, WM_WHATEVER, 0, 0); // note the "::" scoping operator

Or you can possibly use the parent window of your list control (assuming that the dialog is its parent):

GetParent()->SendMessage(WM_WHATEVER, 0, 0);

In this last case, it would be more robust to ensure that GetParent() does not return NULL so perhaps:

CWnd *pParent = GetParent();
if (pParent != NULL)
    pParent->SendMessage(WM_WHATEVER, 0, 0);
else
    // error handling
1
votes

Any window (and a control is child window) can send a message to any window in the same process.

The question is if that's a good idea for your use case.

Perhaps, if you're going to derive a class for the list control anyway, just pass it a pointer to an object it can call member functions on as appropriate for whatever it's doing.