Use dialogbox to create a Windows program

Source: Internet
Author: User

Use dialogbox to create a Windows program

Generally, Win32 programs compiled using the SDK (use winmain and windproc for message processing ),
The generated EXE files are relatively small, but there will also be deficiencies, that is, the created window makes the layout of the interface controls more troublesome.
To solve this problem, you can use the resource Method to Solve the Problem of window layout, that is, create a window and menu in the resource file,
Call the diaglogbox function to create a Windows window and process window messages in dlgproc.

The function declaration is as follows:
Int dialogbox (
Hinstance,
Lptstr lptemplate,
Hwnd hwndparent,
Dlgproc lpdialogfunc
);
Hinstance: Resource handle
Lptstr lptemplate: Long pointer to the window template to be created. You can use makeintresource to obtain the long pointer through the window ID.
Hwnd hwndparent: parent window handle
Dlgproc lpdialogfunc: window message processing function

The callback function of window message processing is defined as follows:
Bool callback dialogproc (
Hwnd hwnddlg,
Uint umsg,
Wparam,
Lparam
);
Here, if the return value is true, the message has been processed. If the return value is false, the message has not been processed properly,
For default processing.

When dialogbox is called, a window in the resource is created. After the window is created, the message loop function in the window is displayed,
The dialogbox function is returned only when the enddialog () function is called.

Note: When Using shcreatemenubar, if the created menu is in the resource file, you must set the shmenubarinfo
Dwflags of is shcmbf_hmenu!

 

 

 # Include <windows. h> <br/> # include <aygshell. h> <br/> # include "resource. H "<br/> ////////////////////////////////// //////////////////////////////////////// <br/> # pragma comment (Lib, "aygshell. lib ") <br/> //////////////////////////////////// /// // <br /> hwndg_hwnd; <br/> hinstanceg_hinst; <br/> //////////////////////////////////// /// // <br/> Bool callback dialogproc (hwnd hdlg, uint umsg, <br/> wparam, lparam); <br/> bool initwindow (const hwnd hdlg, uint ntoolbarid ); <br/> //////////////////////////////////// /// // <br /> # define wnd_title_t ("resdialog ") <br/> //////////////////////////////////// /// // <br /> ////////////////////////////////////// ////////////////// /// // <Br/> ////////////////// //////////////////////////////////////// ////////////// <br/> int apientry winmain (hinstance, <br/> hinstance hprevinstance, <br/> lpwstr lpcmdline, <br/> int nshowcmd) <br/>{< br/> int nresult; <br/> handle hmutex; </P> <p> hmutex = createevent (null, false, false, _ T ("_ program_only_one_instance _"); <br/> If (hmutex! = NULL) <br/>{< br/> If (getlasterror () = error_already_exists) <br/>{< br/> hwnd; <br/> hwnd = findwindow (null, wnd_title); <br/> If (hwnd! = NULL) <br/>{< br/> setforegroundwindow (hwnd) (ulong) hwnd | 0x00000001 )); <br/>}< br/> else <br/>{< br/> g_hinst = hinstance; <br/> nresult = dialogbox (g_hinst, makeintresource (idd_dialog1), <br/> null, (dlgproc) dialogproc); <br/> nresult = getlasterror (); <br/>}</P> <p> closehandle (hmutex); <br/>}</P> <p> return (nresult> = 0 ); <br/>}< br/> /////////////////////////////// ////////////////////////////// //////////< Br/> bool callback dialogproc (hwnd hdlg, uint umsg, <br/> wparam, lparam) <br/>{< br/> bool freturn = true; </P> <p> switch (umsg) <br/>{< br/> case wm_initdialog: <br/> If (! Initwindow (hdlg, idr_menu1) <br/> enddialog (hdlg,-1); // exit cause error <br/> break; <br/> case wm_command: <br/> switch (wparam) <br/> {<br/> case idm_quit: <br/> enddialog (hdlg, idok); <br/> break; <br/>}< br/> break; <br/> case wm_destroy: <br/> break; <br/> default: <br/> freturn = false; <br/>}< br/> return freturn; <br/>}< br/> /////////////////////////////// //////////////////////////////////////// /// <br/> bool initwindow (const hwnd hdlg, uint ntoolbarid) <br/>{< br/> // specify that the dialog box shoshould stretch full screen <br/> shinitdlginfo Shidi; <br/> zeromemory (& Shidi, sizeof (Shidi); <br/> Shidi. dwmask = shidim_flags; <br/> Shidi. dwflags = shidif_sizedlgfullscreen; <br/> Shidi. hdlg = hdlg; </P> <p> // set up soft keys menu <br/> shmenubarinfo MBI; <br/> zeromemory (& MBI, sizeof (shmenubarinfo )); <br/> MBI. cbsize = sizeof (shmenubarinfo); <br/> MBI. hwndparent = hdlg; <br/> MBI. dwflags = shcmbf_hmenu; <br/> MBI. ntoolbarid = ntoolbarid; <br/> MBI. hinstres = g_hinst; </P> <p> // if we cocould not initialize the dialog box, return an error <br/> If (false = (shinitdialog (& Shidi) & shcreatemenubar (& MBI) <br/>{< br/> return false; <br/>}</P> <p> // set the title bar <br/> setwindowtext (hdlg, wnd_title); </P> <p> return true; <br/>}</P> <p>

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.