AppWizard and More in MFC - Messages and the Properties Window continued
(Page 4 of 4 )
Overriding the messages is an easy task -- nothing devilish. Just select the corresponding class from the Class View Tab, right click on it, and select the Properties. A new window will appear in which you can find the messages tab. It is located near the lightning-like icon. If you still have trouble finding it, glance over at the image below.

The image shows that the list will be populated by all the receivable messages in our class. In our case, that is the CMFC_exampleView class, so it has many messages in the list. Now if we want to complete a specific command at the pushdown of the left button in the client area, for example, we seek the WM_LBUTTONDOWN message and choose to override it. MFC will then complete several codes.
First it declares the message map in the header file:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
Following the implementation of the message map, observe the presence of the ON_WM_LBUTTONDOWN() message.
BEGIN_MESSAGE_MAP(CMFC_exampleView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
The saga is completed by adding the function at the end of the implementation file and taking you (the cursors) there, so you can continue with coding the commands that you are going to need.
// CMFC_exampleView message handlers
void CMFC_exampleView::OnLButtonDown(UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
After the TODO line, you may add all what you want after the pushdown of the left mouse button.
That’s all, in short. These few steps can be taken individually, if you know the corresponding message name and the fact that each function is preceded by the “On” prefix added for its message. If you want to follow this path, you can use the MSDN help files, because there you will find tons of information about each message and all of their possible implementation(s).
Now you can use the power of copy and paste to speed up this process. But why the heck should you write this in when, with a few clicks, you’ll get the same result? Also, near the message list in the Properties Window, you will find the possible events and overrides under the other tab.
Conclusions
Throughout this article, we demystified every aspect of AppWizard. We also learned how to respond to any message that we get from Windows. With this knowledge in your “pocket,” we can finally proceed to pure MFC coding.
The next article is dedicated to MFC syntax accompanied by the GDI related classes and commands. We'll start with a brief description of the Device Contexts. Text writing to the client area near the CPen and CBrush class will be presented and demonstrated as well.
The true adventure is only starting at this point; the MFC’s power will reveal itself to you. Until now, we have learned how it works, but have only seen minimal results. Not anymore, because the next article will be full of results, mostly visible ones, so you don’t want to miss it. Trust me on this.
As usual, have a fantastic day and stay tuned because the next article is coming up soon. It is going to be published here on DevArticles too. However, in the meantime, if there are any further questions or unresolved issues related to this article or IT in general, visit the friendly ever-growing community at the DevHardware Forums.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |