Into the world of Windows Programming-----dialog box

Source: Internet
Author: User

1 Categories of dialog boxes
2 Basic ways to use dialog boxes
3 dialog box Resources
4 Using the modal dialog box

int DialogBox (hinstance hinstance, LPCTSTR lptemplate, HWND hwndparent, Dlgproc lpdialogfunc);


5 Use of modeless dialog boxes
5.1 Adding a dialog box resource
5.2 Defining window processing functions

BOOL CALLBACK DialogProc (HWND Hwnddlg,//Handle to dialog boxUINT umsg,//messageWPARAM WPARAM,//First message parameterLPARAM LPARAM);//Second message parameter

5.3 Creating a dialog box

HWND createdialog (hinstance hinstance,//Application Instance handleLPCTSTR Lptemplate,//Resource ID of the dialog boxHWND hWndParent,//parent WindowDlgproc Lpdialogfunc);//window processing functions for dialog boxes

The return value is the window handle of the created dialog box.
5.4 Display dialog box
ShowWindow
5.5 Closing the dialog box
EndDialog
5.6 Comparison with modal dialog box
Create a function: modal dialog box DialogBox
modeless dialog Box Createdialog
Program Execution Mode:
Modal dialogs block to prevent input from other windows
modeless dialog box will let the main program, do not affect the input of other windows

  the message in the 6 dialog box         
   
    wm_initdialog message, use similar to wm_create message
    can initialize the data after the dialog box is created.
    All child controls (buttons, and so on) on the dialog resource template
    creation is complete, Sent to the dialog window again.  
      LParam-parameters that are included with the creation

  7 dialog and normal window contrast
      Normal window: createwindow/ex
       dialog box: dialogbox/createdialog
    7.2 message
      Normal window: will receive WM_CREATE
       dialog box:   wm_initdialog
    7.3 Closed by:
      Normal window: Destorywindow et
       dialog box: EndDialog

Windialog.cpp: Defines the entry point for the application. #include "stdafx.h" #include "windialog.h" #define max_loadstring 100//global variable: hinstance hinst;//current instance Tchar sztitle[max_ loadstring];//title bar Text tchar szwindowclass[max_loadstring];//main window class name//forward declaration of functions contained in this code module: Atommyregisterclass (hinstance HINSTANCE); Boolinitinstance (HINSTANCE, int.); LRESULT Callbackwndproc (hwnd, UINT, WPARAM, LPARAM); Int_ptr Callbackabout (hwnd, UINT, WPARAM, LPARAM); INT apientry _                     Twinmain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpCmdLine, int ncmdshow) {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine); TODO: Place the code here. MSG msg; Haccel hacceltable;//Initialization of global string LOADSTRING (HInstance, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_windialog, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization: if (! InitInstance (HINSTANCE, nCmdShow)) {return FALSE;} hacceltable = Loadaccelerators (hinstance, Makeintresource (Idc_windialoG);//main message loop: while (GetMessage (&msg, NULL, 0, 0)) {if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg);D ispatchmessage (&msg);}} return (int) Msg.wparam;} Function: MyRegisterClass ()////Purpose: Registers the window class. Note:////This function and its usage are only required if you want//This code is compatible with the WIN32 system that was added to the "registerclassex"//function in Windows 95. It is important to call this function,//so that the application can get the associated//"well-formed" small icon. ATOM MyRegisterClass (hinstance hinstance) {wndclassex wcex;wcex.cbsize = sizeof (wndclassex); wcex.style= CS_HREDRAW | cs_vredraw;wcex.lpfnwndproc= wndproc;wcex.cbclsextra= 0;wcex.cbwndextra= 0;wcex.hinstance= hInstance;wcex.hIcon= LoadIcon (HInstance, Makeintresource (Idi_windialog)); wcex.hcursor= loadcursor (NULL, idc_arrow); wcex.hbrBackground= (Hbrush) (color_window+1); wcex.lpszmenuname= makeintresource (Idc_windialog); wcex.lpszclassname= SzWindowClass; Wcex.hiconsm= LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); return RegisterClassEx (&wcex);} Function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the masterWindow////Note:////in this function, we save the instance handle in the global variable and//create and display the main program window.   BOOL InitInstance (hinstance hinstance, int ncmdshow) {HWND hwnd; HInst = hinstance; Store instance handles in global variables hWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, 0, Cw_usedefault,   0, NULL, NULL, HINSTANCE, NULL);   if (!hwnd) {return FALSE;   } ShowWindow (HWnd, ncmdshow);   UpdateWindow (HWND); return TRUE;} BOOL CALLBACK settingdlgproc_1 (HWND hwnddlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {switch (message) {case Wm_creat        E://The MessageBox will not occur (NULL, "wm_create", "Windlg", MB_OK); Break;case wm_initdialog:/* is similar to Wm_create message, you can initialize the data after the dialog box is completed * Different: Wm_initdialog will be sent to the dialog window after all the space in the dialog resource class has been created *        WParam: The currently accepted input Focus child control window handle * LParam: The parameter that accompanies the creation * * MESSAGEBOX (NULL, "Wm_initdialog", "Windlg", MB_OK); Break;case Wm_syscommand:switch (wParam) {case Sc_close:enddialog (hwnddlg,1); Break;default://enddialog (hwndDlg,0); break;} Break;default:break;} Default dialog box handler functionReturn Defdlgproc (HWNDDLG, message, WParam, LParam); return 0;} Functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Handles the message of the main window. wm_command-processing Application Menu//wm_paint-Draw main window//wm_destroy-send exit message and return////lresult CALLBACK WndProc (HWND hwnd, UINT message , WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent; Paintstruct PS; HDC hdc;        HWND hdlg;switch (message) {case WM_CREATE://will occur in the MessageBox (NULL, "wm_create", "Windlg", MB_OK); Break;case Wm_command:wmid = LoWord (wParam); wmevent = HiWord (wParam); int ret;//Analysis Menu selection: switch (wmid) {case id_32771:/ /modal dialog box will block, disallow input of other windows ret = DialogBox (Hinst,makeintresource (id_setting), hwnd,settingdlgproc_1); if (ret! = 2) { MessageBox (NULL, "de-config", "Windialog", MB_OK);} Else{messagebox (NULL, "OK Configuration", "Windialog", MB_OK);} Break;case id_32772://modeless dialog box will let the main program, do not affect the input of other windows Hdlg = Createdialog (Hinst,makeintresource (id_setting), HWnd, SETTINGDLGPROC_1);//Display dialog box ShowWindow (hdlg,sw_show); Break;case Idm_about:dialogbox (HInst, Makeintresource (IDD_ AboutBox), hWnd, about); bReak;case Idm_exit:destroywindow (HWND); Break;default:return DefWindowProc (hwnd, message, WParam, LParam);} Break;case WM_PAINT:HDC = BeginPaint (hWnd, &ps);//TODO: Add any drawing code here ... EndPaint (hwnd, &PS); Break;case wm_destroy:postquitmessage (0); Break;default:return DefWindowProc (hwnd, message, WParam, LParam);} return 0;} The message handler for the About box. INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {case Wm_initdialog:return (INT_PTR) true;case wm_command:if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL) {EndDialog (hdlg, LoWord (WParam)); return (INT_PTR) TRUE;} break;} Return (INT_PTR) FALSE;}


Into the world of Windows Programming-----dialog box

Related Article

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.