MFC Tenth Day

Source: Internet
Author: User
Tags 04x

MFC dialog box 1 classification mode and non-modal 2 related class CDialog--Parent Class Cwndccommondialog Common dialog box--parent class CDialog CPropertyPage Property Page dialog box--parent class CDialog CPropertySheet class, with  CPropertyPage to complete the creation of the property page together. 3 Create a dialog-based application.   1. The modal dialog box adds a dialog resource that defines the associated dialog class.   Call CDialog::D omodal function to create and display a dialog box.   Close the dialog box by calling Cdialog::onok and Cdialog::oncancel.   Virtual BOOL OnInitDialog ();//Initialize 2 non-modal dialog boxes to add resources, define the associated dialog box class creation and display similar to the normal frame window. Handles the closing of the dialog box. Clicking the Close button is the same as the cancel handler for virtual void OnOK (); virtual void OnCancel (); The function of the parent class is called before it is destroyed. destroywindowvirtual void PostNcDestroy ();//Last Executed function CWnd::P Ostncdestroy Delete the subclass before deleting the parent class. The DoModal function calls the Find dialog resource. HInst = AfxFindResourceHandle (M_lpsztemplatename, Rt_dialog); Hrsrc Hresource =:: FindResource (HInst, M_lpsztemplatename, rt_dialog); hdialogtemplate = LoadResource (HInst, HResource ); Sets the parent window to be unavailable. HWND hwndparent = Premodal (); Afxunhookwindowcreate (); BOOL benableparent = false;if (hwndparent! = NULL &&:: iswindowenabled (hWndParent)) {:: EnableWindow (hWndParent, FALSE); benableparent = TRUE;} Creates a modeless dialog box and enters the dialog message loop. (parent window is not available) Afxhookwindowcreate (this); if (CreatedlgIndirect (Lpdialogtemplate,cwnd::fromhandle (hwndparent), hInst)) {if (M_nflags & Wf_continuemodal) {//Enter modal Loopdword dwFlags = mlf_showonidle;if (GetStyle () & ds_noidlemsg) dwFlags |= mlf_noidlemsg; VERIFY (RunModalLoop (dwFlags) = = M_nmodalresult);} Hide the window before enabling the parent, Etc.if (m_hwnd! = NULL) SetWindowPos (null, 0, 0, 0, 0, swp_hidewindow| swp_nosize| swp_nomove| swp_noactivate| Swp_nozorder);} message loop int Cwnd::runmodalloop (DWORD dwFlags) {ASSERT (:: IsWindow (M_hwnd));//window must be Createdassert (! ( M_nflags & Wf_modalloop)); Window must not already is in modal state//for tracking the idle time statebool bidle = TRUE; LONG lidlecount = 0; BOOL Bshowidle = (dwFlags & mlf_showonidle) &&! (GetStyle () & ws_visible); HWND hwndparent =:: GetParent (m_hwnd); M_nflags |= (wf_modalloop| Wf_continuemodal); msg* PMSG = &afxgetthread ()->m_msgcur;//acquire and dispatch messages until the modal state is donefor (;;) {ASSERT (Continuemodal ());//Phase1: Check to see if we can do idle Workwhile (bidle &&!::P eekmessage (PMSG, NULL, NULL, NULL, Pm_noremove)) {ASSERT (Co Ntinuemodal ());//Show the dialog when the message queue goes Idleif (bshowidle) {ShowWindow (SW_SHOWNORMAL); UpdateWindow (); bshowidle = FALSE;} Call OnIdle and in Bidle Stateif (! DwFlags & mlf_noidlemsg) && hwndparent! = NULL && Lidlecount = = 0) {//Send Wm_enteridle to the parent: : SendMessage (hWndParent, Wm_enteridle, Msgf_dialogbox, (LPARAM) m_hwnd);} if ((DwFlags & Mlf_nokickidle) | |! SendMessage (Wm_kickidle, Msgf_dialogbox, lidlecount++)) {//Stop idle processing next timebidle = FALSE;}} Phase2:pump messages while Availabledo{assert (Continuemodal ());//pump message, but quit on wm_quitif (! Afxgetthread ()->pumpmessage ()) {afxpostquitmessage (0); return-1;} Show the window when certain special Messages Rec ' dif (bshowidle && (pmsg->message = 0x118 | | pmsg->messa GE = = wm_syskeydown)) {ShowWindow (SW_SHOWNORMAL); UpdateWindow (); bshowidle = FALSE;} if (! Continuemodal ()) goto exitmodal;//Reset "No idle" state after pumping "normal" Messageif (Afxgetthread () Isidlemessage (PMSG)) {bidle = True;lidlecount = 0;}} while (::P eekmessage (PMSG, NULL, NULL, NULL, Pm_noremove));} Exitmodal:m_nflags &= ~ (wf_modalloop| Wf_continuemodal); return m_nmodalresult;} When you click the OK and Cancel buttons, the dialog window is hidden and then the parent window is set to the available and active state bool Cwnd::setwindowpos (const cwnd* pwndinsertafter, int x, int y, int cx,int cy, UINT nflags) {ASSERT (:: IsWindow (m_hwnd)), if (M_pctrlsite = = NULL) return:: SetWindowPos (M_hwnd, pwndinsertafter-> GetSafeHwnd (), x, Y, CX, CY, nflags), Elsereturn m_pctrlsite->setwindowpos (pWndInsertAfter, x, Y, CX, CY, nflags);} Call the DestroyWindow function to destroy the dialog box window. Called in the DoModal function. Returns the execution result of the DoModal function. Idok:1 idcancel:0 Release Resources cmainframe::ondlgmodal () line 116_afxdispatchcmdmsg (CCmdTarget * 0x007f4008 {CMainFrame hWnd= 0X00040696}, unsigned int 32771, int 0, void (void) * 0x0040117c cmainframe::ondlgmodal (void), void * 0x00000000, unsigned int, Afx_cmdhandlerinfo * 0x00000000) line 88ccmdtarget::oncmdmsg (unsigned int 32771, int 0, void * 0x00000000, Afx_cmdhandlerinfo * 0x00000000) li NE 302 + bytescframewnd::oncmdmsg (unsigned int 32771, int 0, void * 0x00000000, Afx_cmdhandlerinfo * 0x00000000) Line 8 98 + bytescwnd::oncommand (unsigned int 32771, long 0) line 2099cframewnd::oncommand (unsigned int. 32771, long 0) line 32 1cwnd::onwndmsg (unsigned int 273, unsigned int 32771, long 0, long * 0x0018fc7c) line 1608 + Bytescwnd::windowproc (unsi gned int 273, unsigned int 32771, long 0) line 1596 + bytesafxcallwndproc (CWnd * 0x007f4008 {CMainFrame hwnd=0x00040696 }, hwnd__ * 0x00040696, unsigned int 273, unsigned int 32771, long 0) line 215 + bytesafxwndproc (hwnd__ * 0x00040696, U nsigned int 273, unsigned int 32771, long 0) line 379AfxWndProcBase (hwnd__ * 0x00040696, unsigned int 273, unsigned int 32 771, long 0) line + bytesuser32! 765762FA () user32! 76576D3A () user32! 765777C4 () user32! 76577BCA () CWinThread::P umpmessage () line 853CWinThreAd::run () line 487 + one bytescwinapp::run () line 400AfxWinMain (hinstance__ * 0x00400000, hinstance__ * 0x00000000, char *  0X005458BA, int 1) line + byteswinmain (hinstance__ * 0x00400000, hinstance__ * 0x00000000, char * 0x005458ba, int 1) Line 30WinMainCRTStartup () line + byteskernel32! 763633CA () ntdll! 77149ed2 () ntdll! 77149EA5 () int CDialog::D omodal () {//can be constructed with a resource template or Initmodalindirectassert (m_ Lpsztemplatename! = NULL | | M_hdialogtemplate! = NULL | | M_lpdialogtemplate = NULL);//load resource as necessarylpcdlgtemplate lpdialogtemplate = m_lpdialogtemplate; Hglobal hdialogtemplate = m_hdialogtemplate; HInstance HInst = AfxGetResourceHandle (); if (m_lpsztemplatename! = NULL) {hInst = AfxFindResourceHandle (m_ Lpsztemplatename, Rt_dialog); Hrsrc Hresource =:: FindResource (HInst, M_lpsztemplatename, rt_dialog); hdialogtemplate = LoadResource (HInst, HResource );} if (hdialogtemplate! = NULL) Lpdialogtemplate = (lpcdlgtemplate) lockresource (hdialogtemplate);//return-1 in case of failure to load the dialog template Resourceif (lpdialogtemplate = NULL) return-1;//Disable PA rent (before creating dialog) HWND hwndparent = Premodal (); Afxunhookwindowcreate (); BOOL benableparent = false;if (hwndparent! = NULL &&:: iswindowenabled (hWndParent)) {:: EnableWindow (hWndParent, FALSE); benableparent = TRUE;} try{//Create modeless dialogafxhookwindowcreate (this); if (Createdlgindirect (Lpdialogtemplate,cwnd::fromhandle ( hwndparent) (HInst)) {if (M_nflags & Wf_continuemodal) {//Enter modal Loopdword dwFlags = mlf_showonidle;if (GetStyle ( ) & ds_noidlemsg) dwFlags |= mlf_noidlemsg; VERIFY (RunModalLoop (dwFlags) = = M_nmodalresult);} Hide the window before enabling the parent, Etc.if (m_hwnd! = NULL) SetWindowPos (null, 0, 0, 0, 0, swp_hidewindow| swp_nosize| swp_nomove| swp_noactivate| Swp_nozorder);}} Catch_all (e) {delete_exception (e); m_nmodalresult =-1;} End_catch_allif (benableparent):: EnableWindow (hWndParent, TRUE); if (hwndparent! = NULL &&:: GetActiveWindow () = = m_hwnd):: SetActiveWindow (hwndparent);//Destroy Modal Windowdestroywindow (); Postmodal ();//Unlock/free Resources as Necessaryif (m_lpsztemplatename! = NULL | | M_hdialogtemplate! = NULL) unlockresour CE (hdialogtemplate); if (m_lpsztemplatename! = NULL) FreeResource (hdialogtemplate); return m_nmodalresult;} Dialog data exchange technology DDX binds a control to a member variable of a class, manipulating the member variable to achieve the purpose of manipulating the control. Several of the related functions. DDX_Control binds a control to a variable of the control type DDX_Text binds the control to a variable of the value type. 2 functions that contain some binding functions DoDataExchange data exchange functions. 3 UpdateData Data Update function. Usually requires a programmer to call. UpdateData (TRUE) passes the value of the user's input or selection on the control to its bound variable updatedata (FALSE) variable to the control of the advance binding. DDX uses virtual void DoDataExchange (cdataexchange* pDX); void AFXAPI DDX_Control (cdataexchange* pDX, int nIDC, cwnd& rcontrol) {if (Rcontrol.m_hwnd = = NULL)//Not Subclasse D Yet{assert (!pdx->m_bsaveandvalidate);      HWND Hwndctrl = Pdx->preparectrl (NIDC), if (!rcontrol.subclasswindow (Hwndctrl)) {ASSERT (FALSE); Possibly trying to subclass twice? AfxthrownotSupportedexception ();} #ifndef _afx_no_occ_supportelse{//If The control has reparented itself (e.g., invisible control),/Make sure the CW nd gets properly wired to its control site.if (Pdx->m_pdlgwnd->m_hwnd! =:: GetParent (Rcontrol.m_hwnd)) Rcontrol.attachcontrolsite (Pdx->m_pdlgwnd);} #endif//!_afx_no_occ_support}}ddx_control (CDataExchange * 0x0018f590, int. 1, CWnd & {CWnd hwnd=0x00000000}) line 621 CMyDlg::D odataexchange (CDataExchange * 0x0018f590) line 18cwnd::updatedata (int. 0) Line 3120cdialog::oninitdialog () Line 680 + ten Bytescmydlg::oninitdialog () line + 8 Bytesafxdlgproc (hwnd__ * 0x005d0746, unsigned int 272, unsigned int 591518, unsigned int 591518) line + bytesuser32! 765762FA () user32! 7659F9DF () user32! 7659f784 () user32! 7658AFAC () user32! 765762FA () user32! 76576D3A () user32! 76580d27 () user32! 7658794a () CWnd::D efwindowproca (unsigned int 272, unsigned int 591518, long 0) line 1011 + Bytescwnd::D efault () line 258 Cdialog::handleinitdialog (unsigned int 591518, unsigned int 591518) line 624 + 8 bytescwnd::onwndmsg (unsigned int 272, unsigned int 591518, long 0, long * 0x0018f 9C4) Line 1826 + bytescwnd::windowproc (unsigned int. 272, unsigned int 591518, long 0) line 1596 + Bytesafxcallwndpro  C (CWnd * 0X0018FDFC {CMyDlg hwnd=0x005d0746}, hwnd__ * 0x005d0746, unsigned int 272, unsigned int 591518, long 0) line 215 + Bytesafxwndproc (hwnd__ * 0x005d0746, unsigned int 272, unsigned int 591518, long 0) line 379AfxWndProcBase (hwnd__ * 0x005d0746, unsigned int 272, unsigned int 591518, long 0) line + bytesuser32! 765762FA () user32! 76576D3A () user32! 7657965E () user32! 765a206f () user32! 765A10D3 () user32! 7658b044 () cwnd::createdlgindirect (const dlgtemplate * 0x00417170, CWnd * 0x00000000 {CWnd hwnd=???}, hinstance__ * 0x0040 0000) Line 327 + bytescdialog::D omodal () line 531 + bytescdlgddxapp::initinstance () line 47AfxWinMain (hinstance__ * 0x00400000, hinstance__ * 0x00000000, char * 0x005d589c, int 1) line + one byteswinmain (HINstance__ * 0x00400000, hinstance__ * 0x00000000, char * 0x005d589c, int 1) line 30WinMainCRTStartup () line + + Bytesk ernel32! 763633CA () ntdll! 77149ed2 () ntdll! 77149EA5 () DDX implements the principle DDX_Control, the binding of the control type calls PrepareCtrl, calls SubclassWindow by invoking the resource ID to get the handle of the control, and in the function, calls the Attach function, Binds a variable of the control type to the control handle.    DDX_Text, the binding of the value type calls PrepareEditCtrl, which gets the handle of the control through the resource ID. Depending on the value of the parameter of the UpdateData () function, call Afxgetwindowtext/afxsetwindowtext to get the value of the control assigned to the variable/bar variable value passed to the control. Cmydlg::oninitdialog->cdialog::oninitdialog->updatedata->dodataexchange->ddx_control/ddx_textbool CDialog::OnInitDialog () {//Execute dialog rt_dlginit resourcebool bdlginit;if (m_lpdialoginit! = NULL) Bdlginit = Executedlginit (m_lpdialoginit), Elsebdlginit = Executedlginit (M_lpsztemplatename), if (!bdlginit) {TRACE0 ("Warning: Executedlginit failed during dialog init.\n "); EndDialog ( -1); return FALSE;} Transfer data into the dialog from member Variablesif (! UpdateData (FALSE)) {TRACE0 ("Warning:updatedata failed during dialog init.\n"); EndDialog ( -1); return FALSE;} EnAble/disable Help button automaticallycwnd* Phelpbutton = GetDlgItem (ID_HELP); if (Phelpbutton! = NULL) phelpbutton-> ShowWindow (afxhelpenabled ()?    Sw_show:sw_hide); return TRUE; Set focus to First One}bool CWnd::UpdateData (BOOL bsaveandvalidate) {ASSERT (:: IsWindow (M_hwnd));//Calling UpdateData Before DoModal? CDataExchange DX (this, bsaveandvalidate);//Prevent control notifications from being dispatched during UPDATEDATA_AFX_ thread_state* pthreadstate = AfxGetThreadState (); HWND hwndoldlockout = pthreadstate->m_hlockoutnotifywindow;   ASSERT (hwndoldlockout! = m_hwnd); Must not Recursepthreadstate->m_hlockoutnotifywindow = M_hwnd;       BOOL BOK = FALSE;  Assume Failuretry{dodataexchange (&DX);         -------------------------------> Call Subclass bok = TRUE; It Worked}catch (CUserException, E) {//validation failed-user already alerted, Fall Throughassert (!bok);//Note:delet E_exception_ (e) not Required}and_catch_all (e) {//validation failed due to OOM or other resourceFailuree->reporterror (Mb_iconexclamation, afx_idp_internal_failure); ASSERT (!bok);D elete_exception (e);} End_catch_allpthreadstate->m_hlockoutnotifywindow = Hwndoldlockout;return BOK;} HWND CDataExchange::P reparectrl (int nIDC) {ASSERT (NIDC! = 0); ASSERT (NIDC! =-1); Not Allowedhwnd Hwndctrl;m_pdlgwnd->getdlgitem (NIDC, &hwndctrl), if (Hwndctrl = = NULL) {TRACE1 ("Error:no data Exchange control with ID 0x%04x.\n ", NIDC); ASSERT (FALSE); Afxthrownotsupportedexception ();} M_hwndlastcontrol = Hwndctrl;m_beditlastcontrol = FALSE;   Not a edit item by Defaultassert (Hwndctrl! = NULL); Never return NULL Handlereturn Hwndctrl;} void Cwnd::getdlgitem (int nID, hwnd* phwnd) Const{assert (:: IsWindow (m_hwnd)); ASSERT (Phwnd! = null), if (M_pctrlcont = = null) *phwnd =:: GetDlgItem (M_hwnd, nid); Elsem_pctrlcont->getdlgitem (Nid, Phwnd);} BOOL Cwnd::subclasswindow (HWND hwnd) {if (! Attach (hWnd)) return false;//allow any and subclassing to Occurpresubclasswindow ();//now hooks into the AFX WNDPROCWNDProc* LPLPFN = Getsuperwndprocaddr (); WNDPROC Oldwndproc = (WNDPROC):: SetWindowLong (HWnd, GWL_WNDPROC, (DWORD) Afxgetafxwndproc ());   ASSERT (Oldwndproc! = (WNDPROC) afxgetafxwndproc ()); if (*LPLPFN = = NULL) *lplpfn = Oldwndproc; The first control of that type Created#ifdef _debugelse if (*lplpfn! = Oldwndproc) {TRACE0 ("error:trying to use Subclas Swindow with incorrect cwnd\n "); TRACE0 ("\tderived class.\n"); TRACE3 ("\thwnd = $%04x (nidc=$%04x) is not a%hs.\n", (UINT) hwnd,_afxgetdlgctrlid (hWnd), Getruntimeclass ()->m_ lpszClassName); ASSERT (FALSE);//Undo The subclassing if continuing after Assert::setwindowlong (HWnd, GWL_WNDPROC, (DWORD) oldwndproc);} #endifreturn TRUE;} void AFXAPI DDX_Text (cdataexchange* pDX, int nIDC, cstring& value) {HWND Hwndctrl = pdx-> PrepareEditCtrl (NIDC); if (pdx->m_bsaveandvalidate) {int nlen =:: Getwindowtextlength (Hwndctrl);:: GetWindowText ( Hwndctrl, value. GetBufferSetLength (Nlen), nlen+1); value. ReleaseBuffer ();} Else{afxsetwindowtext (hwnDctrl, value);}} HWND CDataExchange::P repareeditctrl (int nIDC) {HWND Hwndctrl = PrepareCtrl (NIDC); ASSERT (Hwndctrl! = NULL); M_beditlastcontrol = True;return Hwndctrl;} _afx_inline cstring::operator LPCTSTR () const{return m_pchdata;} void AFXAPI Afxsetwindowtext (HWND Hwndctrl, LPCTSTR lpsznew) {int nnewlen = Lstrlen (lpsznew); TCHAR szold[256];//Fast Check to see if text really changes (reduces flash in controls) if (Nnewlen > _countof (szold) | |::getwindowtext (Hwndctrl, Szold, _countof (szold))! = Nnewlen | | LSTRCMP (Szold, lpsznew)! = 0) {//Change It::setwindowtext (Hwndctrl, lpsznew);}}

MFC Tenth Day

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.