Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnClose 1

Status
Not open for further replies.

bitmapbrother

Programmer
Apr 21, 2005
12
CZ
Hi, I derived class from CWnd. I need to catch OnClose event but it doesnt work. Thx for any tips.

class MyWin : public CWnd
{
protected:
afx_msg void OnClose(void);
public:
MyWin();
~MyWin(void);
bool Init(void); // creates window by CWnd::CreateEx with parent = NULL
void Show(void); // shows window by CWnd::ShowWindow
};

// this is never called after i click [X] icon in windows title
afx_msg void MyWin::OnClose(void)
{
Msg(_T("OnClose")); // Msg is customized MessageBox
CWnd::OnClose();
}
 
Did you remember to add the
Code:
ON_WM_CLOSE()
to the message map?
Code:
BEGIN_MESSAGE_MAP(MyWin, CWnd)
	ON_WM_CLOSE()
END_MESSAGE_MAP()
 
Not sure how you are creating this window so here's an idea.
Code:
bool MyWin::Init(void)
{
	CString myClassName = AfxRegisterWndClass(
							CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
                            LoadCursor(NULL, IDC_ARROW),
                            (HBRUSH)GetStockObject(COLOR_BTNFACE+1),
							LoadIcon(NULL, IDI_APPLICATION));

	return (CWnd::CreateEx(0,
					myClassName,
				   "MyWin",
				   WS_POPUP | WS_CAPTION | WS_SYSMENU | MFS_BLOCKSYSMENU,
				   CRect(100, 100, 350, 420),NULL,NULL))?true:false;
}
Then in your main app create the window thus
Code:
	if(m_myWin)
		delete m_myWin;
	m_myWin = new MyWin();
	if(m_myWin->Init())
	{
		m_myWin->Show();
	}
where
Code:
MyWin* m_myWin;
when you press the close on this window you will be able to capture it.

If this window does not have a system menu then you will have to capture the WM_DESTROY instead.
 
Finally I got it working, I forget WS_CAPTION style. I am developping app for Pocket PC and CWnds are fullscreen here with caption in global system bar which has [x] icon too - but that [x] icon doesnt work like [x] icon in CWnd caption bar (it closes windows but it doesnt call mapped OnClose). Thx for suggestions Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top