Adding event handler to user defined window classes - mfc

I am using MFC MDI. I create a window the main document window. I create another window(lets call it second window) as child of MDI Window View, then I create child window(third window) of second window. Again I need to create another window, child window of third window. I have a button on the ribbon and I want to call message handler in the third window to handle this command. I have added message map and all required stuff but it is not calling the message handler. If I define the message handler in MDI window view, it catches the event. If I dont delare the message handler in my main CWinApp class or MDI window View class the button appears disabled.
All my child view classes are inherited from CWnd and main MDI window is derived from CView. Please let me know what I am missing here.
Saba

I am at the loss here. You are using MFC; why using events? use command messages and command message handlers instead events.
Use MFC implemented and well working command routing.

MFC doesnt route messages generated by clicking Ribbon buttons to child windows. If we want to send messages to child windows of MDI windows we need to do it ourselves. Ribbon button's messages are either passed to active MDI window or CWinApp class.

Related

Adding CDialog inside CDockablePane and application becomes unresponsive

I want to add a CDialog control inside CDockablePane. When I use CDialog.DoModal() to display the dialog window, it makes the MFC application unresponsive and waiting for the CDialog result.
How can I make the application display the dialog and continue running without waiting for the CDialog result?
You cannot use DoModal to display the dialog. That displays a modal dialog, which prevents interaction with any other windows in your application until the dialog has been dismissed. Just like a message box does.
To display a non-modal dialog, you call the Create member function. Use the instance of your CDockablePane as the dialog's parent. You will also need to ensure that the dialog itself is a child window, without a border.
It might be easier to use a class derived from CFormView or CPaneDialog.

Post messages to model dialog

I'm planning to create modeless dialog and to receive messages from another thread. According to my understanding to creating modeless dialog I need to create CWinThread and some frame or dialog inside of CWinThread derived class object. But do I really need to create frame/dialog? Why I can't post messages direct to CWinThread derived class object?
You don't need a dialog to receive messages, but you do need a window handle. That window handle does not need to be associated with something visual. What I think you need is a message only window.
In MFC you can do this with CWnd using the CreateEx method to create the message only window. Pass HWND_MESSAGE to the hWndParent parameter of CreateEx.
It is true that you can create a hidden modeless dialog just to receive messages. However, this is grossly wasteful. What's more, MFC is designed around the concept that dialogs are created in the main UI thread. So for a number of different reasons you really do want to avoid creating a dialog just to receive messages in a thread. Use a message only window.
The message handler posts/sends messages to Window handlers, so you need a placeholder window to receive that message and handle as needed.
You can post messages to a CWinThread derived object using PostThreadMessage. But there are some cases where this is dangerous, discussed here:
http://www.codeproject.com/Articles/225755/PostThreadMessage-Demystified

Visual C++ MFC Application- Event not receiving in child dialog

I have created a derived control handling events as mouse movement, left button down up,down etc.
And I have a popup dialog and within that dialog, three child dialogs.
When I use the derived control in my popup dialog, it is working properly.
But when I place my control in any of the child dialog, the mouse events are not handled.
Please suggest a solution.
Thanks in advance,
Madhan
Must to pass forward the events. Catch them on the child dialog with the respective message handlers and then forward them using the ::SendMessage method.
The popup dialog possibly catched up automatically the mouse events and enabled to be forwarded towards.

How I can get a focus message in MFC?

I have a dialog box with some CListCtrl. I want that when I click on one of them, receive killfocus or setfocus message.
How I can get it?
The CListCtrl class wraps the Win32 ListView control. That control communicates with its parent (your dialog) via WM_NOTIFY messages.
So you can process WM_NOTIFY messages from your list control in your dialog class. Use the Properties window to create an OnChildNotify handler function and write a switch statement that handles the notification message(s) of interest.
The possible notification messages are listed here in the Windows SDK documentation.

CDockablePane as a tabbed document does not send WM_SETFOCUS or WM_MDIACTIVATE

I have a class derived from CDockablePane. I need to do something when the view is focused, so I handle WM_SETFOCUS and it all works nicely most of the time.
But when the pane is docked in Tabbed Document mode (TDI), and the user activates it, the WM_SETFOCUS is not called.
I used Spy and noticed the WM_MDIACTIVATE message is sent to the pane's parent window.
However if I handle WM_MDIACTIVATE inside the pane or inside the mainframe, it does not get called either.
Any ideas what I need to handle?
You may need to inherit the frame class and trigger the sending of a custom message to your views when WM_MDIACTIVATE is received by the frame.