What's new

mfc close message

mesman00

What's that...?
i'm using mfc. what is the message sent by the window when the close button in clicked. in the regular win api it's WM_CLOSE, but it's apparently differently in mfc. i tried including

Code:
ON_COMMAND(WM_CLOSE, OnClose)

in my message map, but to no avail. thanks.
 

zenogais

New member
You always have the ability to override this method, but to that you need to override it in the class definition. Here's an example from my emulator:

NeoMain.h:

Code:
class NeoMain : public CFrameWnd
{
public:
	NeoMain();
protected:
	int  OnCreate(LPCREATESTRUCT lpCreateStruct);
	void OnClose();

	std::string m_strVersion; ///< String dictating the current NeoPSX verison.

	DECLARE_MESSAGE_MAP();
};

NeoMain.cpp:

Code:
....................
void NeoMain::OnClose()
{
	//===================================================
	// Handling Resource Destruction And Destroy Window
	//===================================================
	DestroyWindow();
}
.......................

Hope this will help you.
 
OP
mesman00

mesman00

What's that...?
yah i tried to overload the method, didn't work. this is my code:

CMainWin.h
Code:
#include <afxwin.h>		//main mfc lib
#include <afxdlgs.h>	//for file dialogs
#include <CString>
#include <string>
#include "DCMconvert.h"
#include "mainMenu.h"
#include "CWebView.h"

using namespace std;

#define IDF_OpenDialog 301
#define IDS_Label 401

// Declare the main window class
class CMainWin : public CFrameWnd
{
	private:
		
		CWebView *webView;
		CStatic *label;
		CFileDialog *openDialog;
		CFileDialog *saveDialog;
		DCMconvert convert;
		CString fileExt;
		CString fileName;
		CString filePath;
		CString savePath;
		string htmlFilePath;
		string xmlFilePath;
		string txtFilePath;
		string currentView;
		bool fileLoaded;
	
	public:

		CMainWin();
		//~CMainWin();

		afx_msg void OnItemFileLoad();
		afx_msg void OnItemFileSaveAs();
		afx_msg void OnItemFilePrint();
		afx_msg void OnItemFileExit();
		afx_msg void OnItemViewHTML();
		afx_msg void OnItemViewXML();
		afx_msg void OnItemViewTXT();
		afx_msg void OnItemHelpAbout();
		void OnClose();
		
		DECLARE_MESSAGE_MAP()
};

and here is the implementation in CMainWin.cpp
Code:
void CMainWin::OnClose()
{
	MessageBox("test", "", MB_OK);
}

when i hit the x button the message box never comes up. do i need to add something tot he message map as well? thanks.
 

Doomulation

?????????????????????????
You're doing it wrong afaik ;)
ON_WM_CLOSE() is the one you're looking for. MFC simply wraps windows API, it doesn't change it. WM_CLOSE is sent to your window, it is.

You can also use spy++ to monitor sent messages to your windows.
 
OP
mesman00

mesman00

What's that...?
Doomulation said:
You're doing it wrong afaik ;)
ON_WM_CLOSE() is the one you're looking for. MFC simply wraps windows API, it doesn't change it. WM_CLOSE is sent to your window, it is.

You can also use spy++ to monitor sent messages to your windows.

ON_WM_CLOSE is not a method, unless its in another library other than CFrameWnd.
 

zenogais

New member
mesman00 said:
ON_WM_CLOSE is not a method, unless its in another library other than CFrameWnd.

Its a macro, you add it in between your BEGIN_MESSAGE_MAP() END_MESSAGE_MAP() sequence.
 

Doomulation

?????????????????????????
zenogais said:
Its a macro, you add it in between your BEGIN_MESSAGE_MAP() END_MESSAGE_MAP() sequence.
zenogais is correct ;)
Shame on you for not understanding that :evil:

Now have phun programming =)
 

Top