MFC closes the parent window through a child window to implement the exit program

Source: Internet
Author: User

This should be an old question, I would like to summarize the following: (I just touch windows programming I hope you have a lot of advice)


1. Come up with one of the most reckless.

Directly through exit (0), exitprocess (0); //Bad usage

Guessing that resources are not recycled

The reasons are as follows:

http://blog.csdn.net/jiang1013nan/article/details/4831020


2. Send the WM_CLOSE message to the parent window, and then close your own window.

Gets the handle of the parent window passed directly to the child window, and the child window can close the parent window directly.

The sample code is as follows:


Definition of the parent window class:

Cmfc_biometic_authenticationdlg dialog box class Cmfc_biometic_authenticationdlg:public cdialogex{//construction public:cmfc_ Biometic_authenticationdlg (cwnd* pparent = NULL);//Standard constructor//dialog data enum {IDD = Idd_mfc_biometic_authentication_dialog}; protected:virtual void DoDataExchange (cdataexchange* pDX);//DDX/DDV support//implement Protected:hicon m_hicon;// Generated message map function virtual BOOL OnInitDialog (); afx_msg void OnSysCommand (UINT NID, LPARAM LPARAM); afx_msg void OnPaint (); afx_msg Hcursor Onquerydragicon ();D eclare_message_map () public:afx_msg void OnBnClickedOK (); afx_msg void Onstnclickedpasswordtipsstatic (); afx_msg void Onstnclickedusernamestatic (); afx_msg void OnEnChangeEdit3 (); afx_msg void Onenchangefingertipsedit ();p rivate:cstring m_username;private:cstring m_password;private:cstring M_message_ return;public:afx_msg void Onbnclickedokbutton (); afx_msg void Onstnclickedpassmessagestatic (); afx_msg void Onbnclickedinstructionlog ();p rivate:cuserdialog m_userlog;public:afx_msg void OnClose ();};


Definition of child window class

Class Cuserdialog:public Cdialogex{declare_dynamic (Cuserdialog) public:cuserdialog (cwnd* pParent = NULL);   The standard constructor virtual ~cuserdialog ();//Dialog data enum {IDD = idd_user_dialog};p rotected:virtual void DoDataExchange ( cdataexchange* PDX);    


CWnd *m_pparentwnd; A pointer in the child window class to store the parent window


Important step: Pass the pointer to the child window (through the constructor):

Cuserdialog::cuserdialog (cwnd* pparent/*=null*/): CDialogEx (Cuserdialog::idd, pparent), M_pParentWnd (pParent) {}

The process of closing is by: SendMessage (...);

void Cuserdialog::onclose () {//TODO:  Add the message handler code here and/or call the default value//exitprocess (0);//::D Estroywindow (m_ pParentWnd);:: SendMessage (M_pparentwnd->m_hwnd, wm_close, 0, 0); Cdialogex::onclose ();}

3. Another suggested practice,use a global variable to indicate whether you want to exit the program, assign a value in the child window, and when the child window is called, the parent window determines whether to exit the program at the call. (Have not tried, welcome to do friends message, posted important code or private messages i) Thank you very much ~ ~



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MFC closes the parent window through a child window to implement the exit program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.