Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi I have a simple MFC applications which when I press "X" or "Close” it should be sent a WM_close message which I try to catch it in my PreTranslateMessage(MSG* pMsg). But It dosent come. What should I do to catch the WM_close message?
Thanks in advance
M.H

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	TRACE("\n MSG %d", pMsg->message);
	if(pMsg->message == WM_CLOSE)
		MessageBox("WM_CLOSE");
	else if(pMsg->message == WM_QUIT)
		MessageBox("WM_QUIT");
	else if(pMsg->message == WM_DESTROY )
		MessageBox("WM_DESTROY");
    return __super::PreTranslateMessage(pMsg); // allow default behavior
}
Posted
Updated 10-Nov-11 8:44am
v2

The Big [X] or system menu (in the icon) of Close (Alt+F4) comes in through "ON_WM_SYSCOMMAND". In your processing look for "SC_CLOSE"

C#
void YourClass::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == SC_CLOSE)
        {
        }
}
 
Share this answer
 
v2
Comments
merh 10-Nov-11 17:29pm    
It works, Thanks a lot
M.H
you need to handle the WM_CLOSE message seperatly

add ON_WM_CLOSE() to your message map, and then add a function to CMainFrame of type afx_msg void OnClose();
 
Share this answer
 
Have a look at WM_SYSCOMMAND. I think that is how to override default close and exit type behaviour.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900