Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can’t receive message. How do that?

There are 3 solutions:

Solution 1: Send the message to the main window.

Send/Post the message to the main frame or the view, and then call some functions of the document.

But if in a MDI app, it does not work, because the routing becomes too complex. Typically, you can send message to a view, but that has risks if there are multiple views on the same document and the one you post message to has been closed.

Solution 2: Use WM_COMMAND message.

A document can’t recerive a message, but it can recerives the commands through MFC’s command mechanism. Then you can use ::PostMessage to post WM_COMMAND message, and repose it in the document.

Solution 3:

Create an invisible top-level window in the CDocument, that represents its message sink. You send/post message to it. Thus will eliminate the complex route policy between the main frame and the views and you don’t worry about the view closing.

Tags: , , , , ,