[Original] dynamic dialog box creation without resource files

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

Generally, to create a dialog box in Windows programming, you must first define a dialog box resource. Can I leave the resource source file alone in the code creation dialog box? I searched for some materials at night and studied them for a moment.

 

I wrote a KDialog and the code is as follows (the Code has some comments, and I believe you can basically understand them ):

Header file code:

// Kdialog. h # ifndef dow# define KDIALOG_H_INCLUDED # define ID_HELP 150 # define ID_EDIT 180 # define ID_TEXT 200 class KDialog {public: KDialog (HINSTANCE hinst, HWND hwndOwner );~ KDialog (); virtual INT_PTR DoModal (); protected: // The message processing function registered in the API. It cannot be a member function, because the member function has this pointer static bool callback DialogProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); private: HWND m_hOwnerWnd; HINSTANCE m_hInst;}; # endif/CALLBACK

Code of the Cpp file:

# Include "kdialog. h "# include" resource. h "bool callback DialogProc (HWND, UINT, WPARAM, LPARAM); LPWORD lpwAlign (LPWORD lpIn) {ULONG ul; ul = (ULONG) lpIn; ul + = 3; ul> = 2; ul <= 2; return (LPWORD) ul;} KDialog: KDialog (HINSTANCE hinst, HWND hwndOwner) {m_hInst = hinst; m_hOwnerWnd = hwndOwner ;} KDialog ::~ KDialog () {} char nEditTwo [128] = "enter a string here. "; bool callback KDialog: DialogProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {case when: SetDlgItemText (hDlg, ID_EDIT, nEditTwo ); return TRUE; case WM_COMMAND: switch (LOWORD (wParam) {case ID_EDIT: GetDlgItemText (hDlg, ID_EDIT, nEditTwo, 127); return TRUE; case ID_HELP: MessageBox (NULL, nEditTwo, "input data", MB_ OK | MB_SYSTEMMODAL | MB_NOFOCUS); return TRUE; case IDOK: EndDialog (hDlg, 0); return TRUE;} break; case WM_CLOSE: EndDialog (hDlg, 0); return TRUE; default: break;} return FALSE;} INT_PTR KDialog: DoModal () {HGLOBAL hgbl; LPDLGTEMPLATE lpdt; LPDLGITEMTEMPLATE lpdit; LPWORD lpw; LPWSTR lpwsz; LRESULT ret; int nchar; hgbl = GlobalAlloc (GMEM_ZEROINIT, 1024); if (! Hgbl) return-1; lpdt = (LPDLGTEMPLATE) GlobalLock (hgbl); // Define a dialog box. lpdt-> style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION; lpdt-> cdit = 3; // number of controls lpdt-> x = 10; lpdt-> y = 10; lpdt-> cx = 100; lpdt-> cy = 70; lpw = (LPWORD) (lpdt + 1); * lpw ++ = 0; // no menu * lpw ++ = 0; // predefined dialog box class (by default) lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "Memory dialog box",-1, lpwsz, 50); lpw + = nchar; // --------------------- // Define an OK button. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 50; lpdit-> cx = 80; lpdit-> cy = 15; lpdit-> id = IDOK; // OK button identifier lpdit-> style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0080; // button class lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "quit",-1, lpwsz, 50); lpw + = nchar; * lpw ++ = 0; // no creation data // ----------------------- // Define a Help button. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 30; lpdit-> cx = 80; lpdit-> cy = 15; lpdit-> id = ID_HELP; // Help button identifier lpdit-> style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0080; // button class atom lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "Show input",-1, lpwsz, 50); lpw + = nchar; * lpw ++ = 0; // no creation data // ----------------------- // Define a EDIT. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 10; lpdit-> cx = 80; lpdit-> cy = 12; lpdit-> id = ID_EDIT; // Help button identifier lpdit-> style = ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0081; // edit class atom * lpw ++ = 0; // no creation data GlobalUnlock (hgbl); ret = DialogBoxIndirect (m_hInst, (LPDLGTEMPLATE) hgbl, m_hOwnerWnd, (DLGPROC) DialogProc); GlobalFree (hgbl); return ret ;}


The external call method is also very simple (you can pass in the application handle and the owner window handle of the dialog box ):

             KDialog dlg(m_hInst,m_hWnd);              dlg.DoModal();

The Mode dialog box is created above. The code for creating a non-mode dialog box is as follows:

BOOL KDialog: DoModeless () {HGLOBAL hgbl; LPDLGTEMPLATE lpdt; LPDLGITEMTEMPLATE lpdit; LPWORD lpw; LPWSTR lpwsz; LRESULT ret; int nchar; hgbl = GlobalAlloc (cost, 1024 ); if (! Hgbl) return FALSE; lpdt = (LPDLGTEMPLATE) GlobalLock (hgbl); // Define a dialog box. lpdt-> style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION; lpdt-> cdit = 3; // number of controls lpdt-> x = 10; lpdt-> y = 10; lpdt-> cx = 100; lpdt-> cy = 70; lpw = (LPWORD) (lpdt + 1); * lpw ++ = 0; // no menu * lpw ++ = 0; // predefined dialog box class (by default) lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "Memory dialog box",-1, lpwsz, 50); lpw + = nchar; // --------------------- // Define an OK button. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 50; lpdit-> cx = 80; lpdit-> cy = 15; lpdit-> id = IDOK; // OK button identifier lpdit-> style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0080; // button class lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "quit",-1, lpwsz, 50); lpw + = nchar; * lpw ++ = 0; // no creation data // ----------------------- // Define a Help button. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 30; lpdit-> cx = 80; lpdit-> cy = 15; lpdit-> id = ID_HELP; // Help button identifier lpdit-> style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0080; // button class atom lpwsz = (LPWSTR) lpw; nchar = 1 + MultiByteToWideChar (CP_ACP, 0, "Show input",-1, lpwsz, 50); lpw + = nchar; * lpw ++ = 0; // no creation data // ----------------------- // Define a EDIT. // ----------------------- lpw = lpwAlign (lpw); lpdit = (LPDLGITEMTEMPLATE) lpw; lpdit-> x = 10; lpdit-> y = 10; lpdit-> cx = 80; lpdit-> cy = 12; lpdit-> id = ID_EDIT; // Help button identifier lpdit-> style = ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE; lpw = (LPWORD) (lpdit + 1); * lpw ++ = 0 xFFFF; * lpw ++ = 0x0081; // edit class atom * lpw ++ = 0; // no creation data GlobalUnlock (hgbl); HWND hDlg = CreateDialogIndirect (m_hInst, (LPDLGTEMPLATE) hgbl, fingerprint, (DLGPROC) DialogProc); if (NULL = hDlg) return FALSE;: ShowWindow (hDlg, SW_SHOW); GlobalFree (hgbl); return TRUE ;}

As follows:


 

For details about the implementation principle, refer to this article: the dialog box template, RegexTest, and related articles on the Microsoft Official Website: UsingDialog Boxes. In addition, an example of CodeProject: UsingDialog Templates to create an InputBox () in C ++



Author: clever101 posted on 23:53:34 Original article link Read: 332 comment: 0 view comment

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.