This example is composed of two files:
This example is composed of two files:
//============================================================================= //DIALOG: WINDOW - Copyright © 2000,2005 Ken Fitlike //============================================================================= //API functions used: CreateWindowEx,DefWindowProc,DispatchMessage,GetMessage, //GetSystemMetrics,LoadImage,MessageBox,PostQuitMessage,RegisterClassEx, //ShowWindow,UpdateWindow,TranslateMessage,WinMain. //============================================================================= //Demonstrates the creation of a window based on a unique window class //derived from a dialog box. //============================================================================= #include <windows.h> //include all the basics #include <tchar.h> //string and other mapping macros #include <string> //define an unicode string type alias typedef std::basic_string<TCHAR> ustring; //============================================================================= //message processing function declarations LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); void OnCommand(const HWND,int,int,const HWND); //non-message function declarations inline int ErrMsg(const ustring&); //============================================================================= int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd) { ustring classname=_T("DLG_STRING_RESOURCE_NAME"); WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class' wcx.cbSize = sizeof(WNDCLASSEX); wcx.lpfnWndProc = WndProc; //wnd Procedure pointer wcx.cbWndExtra = DLGWINDOWEXTRA; wcx.hInstance = hInst; //app instance //use 'LoadImage' to load wnd class icon and cursor as it supersedes the //obsolete functions 'LoadIcon' and 'LoadCursor', although these functions will //still work. Because the icon and cursor are loaded from system resources ie //they are shared, it is not necessary to free the image resources with either //'DestroyIcon' or 'DestroyCursor'. wcx.hIcon = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION, IMAGE_ICON,0,0,LR_SHARED)); wcx.hCursor = reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW, IMAGE_CURSOR,0,0,LR_SHARED)); wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1); wcx.lpszClassName = classname.c_str(); //the window 'class' (not c++ class) has to be registered with the system //before windows of that 'class' can be created if (!RegisterClassEx(&wcx)) { ErrMsg(_T("Failed to register wnd class")); return -1; } HWND hwnd=CreateDialog(hInst,classname.c_str(),0,0); if (!hwnd) { ErrMsg(_T("Failed to create wnd")); return -1; } ShowWindow(hwnd,nCmd); UpdateWindow(hwnd); //start message loop - windows applications are 'event driven' waiting on user, //application or system signals to determine what action, if any, to take. Note //that an error may cause GetMessage to return a negative value so, ideally, //this result should be tested for and appropriate action taken to deal with //it(the approach taken here is to simply quit the application). MSG msg; while (GetMessage(&msg,0,0,0)>0) { //make sure that default, system dialog box behaviour occurs if (!IsWindow(hwnd) || !IsDialogMessage(hwnd,&msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return static_cast<int>(msg.wParam); } //============================================================================= LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_COMMAND: { OnCommand(hwnd,LOWORD(wParam),HIWORD(wParam), reinterpret_cast<HWND>(lParam)); return 0; } case WM_CREATE: { //Because it's a window based on a dialog resource but NOT a dialog //box it receives a WM_CREATE message and NOT a WM_INITDIALOG message. return 0; } case WM_DESTROY: PostQuitMessage(0); //signal end of application return 0; default: //let system deal with msg return DefDlgProc(hwnd,uMsg,wParam,lParam); } } //============================================================================= void OnCommand(const HWND hwnd,int id,int notifycode,const HWND hCntrl) { //handles WM_COMMAND message of the modal dialogbox switch (id) { case IDOK: //RETURN key pressed or 'ok' button selected case IDCANCEL: //ESC key pressed or 'cancel' button selected DestroyWindow(hwnd); } } //============================================================================= inline int ErrMsg(const ustring& s) { return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION); } //=============================================================================
//============================================================================= //WINDOW DIALOG - Copyright (c) 2000,2005 Ken Fitlike //resource script // //Note that there is no resource define for the dialog - its identifier is a //string which is the same as that used for the CLASS resource definition //statement within the DIALOGEX resource definition statement. //============================================================================= #if !defined __BORLANDC__ #include <afxres.h> #endif //============================================================================= //Resource languages: Codes for languages and sub-languages are declared in //winnt.h eg. for US english replace SUBLANG_ENGLISH_UK with SUBLANG_ENGLISH_US //eg. for FRENCH replace LANG_ENGLISH with LANG_FRENCH and then replace //SUBLANG_ENGLISH_UK with either SUBLANG_FRENCH,SUBLANG_FRENCH_BELGIAN, //SUBLANG_FRENCH_CANADIAN,SUBLANG_FRENCH_SWISS,SUBLANG_FRENCH_LUXEMBOURG, //SUBLANG_FRENCH_MONACO depending on which national or regional variation of //the language corresponds best with your requirements. // LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK //============================================================================= //Dialog //============================================================================= DLG_STRING_RESOURCE_NAME DIALOGEX 0, 0, 252, 149 STYLE DS_CENTER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME CAPTION "Dialog Box: Window" CLASS "DLG_STRING_RESOURCE_NAME" FONT 8, "MS Sans Serif" { DEFPUSHBUTTON "OK",IDOK,22,62,50,14 PUSHBUTTON "Cancel",IDCANCEL,111,63,50,14 } //=============================================================================