This example is composed of three files:
This example is composed of three files:
//============================================================================= //USER CONTROLS (RESOURCES) - Copyright © 2000,2005 Ken Fitlike //============================================================================= //API functions used: DialogBox,EndDialog,GetDlgItem,LoadImage,MAKEINTRESOURCE, //MessageBox,PostMessage,SendMessage,SetWindowText,WinMain. //============================================================================= //This demonstrates the creation of user controls on a modal dialog box. //============================================================================= #include <windows.h> //include all the basics #include <tchar.h> //string and other mapping macros #include <string> #include "resources.h" //define an unicode string type alias typedef std::basic_string<TCHAR> ustring; //============================================================================= //message processing function declarations INT_PTR CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM); void OnCommand(const HWND,int,int,const HWND); INT_PTR OnInitDlg(const HWND,LPARAM); //non-message function declarations inline int ErrMsg(const ustring&); //============================================================================= int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int) { INT_PTR success=DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),0,DlgProc); if (success==-1) { ErrMsg(_T("DialogBox failed.")); } return 0; } //============================================================================= INT_PTR CALLBACK DlgProc(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_INITDIALOG: { return OnInitDlg(hwnd,lParam); } default: return FALSE; //let system deal with msg } } //============================================================================= 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 EndDialog(hwnd,id); } } //============================================================================= INT_PTR OnInitDlg(const HWND hwnd,LPARAM lParam) { //set the small icon for the dialog. SendMessage(hwnd,WM_SETICON,ICON_SMALL, reinterpret_cast<LPARAM>(LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0,0,LR_DEFAULTCOLOR))); //add a string to the combobox control SendMessage(GetDlgItem(hwnd,IDC_COMBOBOX), CB_ADDSTRING,0, reinterpret_cast<LPARAM>(_T("Drop-down combobox"))); //add a string to the edit control SetWindowText(GetDlgItem(hwnd,IDC_EDITTEXT),_T("Edit Control")); //add a string to the listbox control SendMessage(GetDlgItem(hwnd,IDC_LISTBOX), LB_ADDSTRING,0, reinterpret_cast<LPARAM>(_T("Listbox control"))); //ensure focus rectangle is properly drawn around control with focus PostMessage(hwnd,WM_KEYDOWN,VK_TAB,0); return TRUE; } //============================================================================= inline int ErrMsg(const ustring& s) { return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION); } //=============================================================================
//============================================================================= //Resource script constants for User Controls example. //============================================================================= #define IDD_DIALOG1 200 #define IDI_ICON1 301 #define IDC_RADIO1 302 #define IDC_3STATE 303 #define IDC_AUTOCHECK 304 #define IDC_AUTORADIOBUTTON 305 #define IDC_CHECKBOX 306 #define IDC_COMBOBOX 307 #define IDC_CTEXT 308 #define IDC_EDITTEXT 309 #define IDC_GROUPBOX 310 #define IDC_ICONCNTRL 311 #define IDC_LISTBOX 312 #define IDC_LTEXT 313 #define IDC_RTEXT 314 #define IDC_SCROLLBAR 315 #define IDC_STATE3 316
//============================================================================= //USER CONTROLS - Copyright (c) 2000,2005 Ken Fitlike //resource script // //The controls created use specific resource definition statements which //provide default definitions for that control type. By contrast, the CONTROL //resource definition statement, which can be used to create any control, //requires more parameters to create a particular control since those same //default values must be explicitly defined. // //Borland's bcc5.5 resource compiler (brc32.exe) requires four arguments //specifying the dimensions for the ICON resource definition statement. Other //resource compilers only require the first two. //============================================================================= #if !defined __BORLANDC__ #include <afxres.h> #endif #include "resources.h" //============================================================================= //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 //============================================================================= IDD_DIALOG1 DIALOGEX 0, 0, 400, 300 STYLE DS_CENTER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME CAPTION "Resources: User Controls" FONT 8, "MS Sans Serif" { DEFPUSHBUTTON "OK",IDOK,35,260,50,14 PUSHBUTTON "Cancel",IDCANCEL,330,260,50,14 RADIOBUTTON "RADIO 1",IDC_RADIO1,30,14,100,16 AUTO3STATE "AUTO3STATE",IDC_3STATE,30,30,100,16 AUTOCHECKBOX "AUTOCHECKBOX",IDC_AUTOCHECK,30,50,100,16 AUTORADIOBUTTON "AUTORADIOBUTTON",IDC_AUTORADIOBUTTON,30,70,100,16 CHECKBOX "CHECKBOX",IDC_CHECKBOX,30,90,100,16 COMBOBOX IDC_COMBOBOX,30,120,100,90,CBS_DROPDOWN CTEXT "CTEXT",IDC_CTEXT,30,140,100,16 EDITTEXT IDC_EDITTEXT,30,160,100,16 GROUPBOX "GROUPBOX",IDC_GROUPBOX,4,4,392,292 ICON "IDI_ICON",IDC_ICONCNTRL,30,180,0,0 LISTBOX IDC_LISTBOX,30,200,100,60 LTEXT "LTEXT",IDC_LTEXT,220,10,100,16 RTEXT "RTEXT",IDC_RTEXT,220,50,100,16 SCROLLBAR IDC_SCROLLBAR,220,70,100,16 STATE3 "STATE3",IDC_STATE3,220,90,100,16 } //============================================================================= //Images (icons) //============================================================================= //MinGW: can't use same id for icon in dlgbox static(ICON) cntrl as app icon. IDI_ICON ICON "icon1.ico" //used for ICON control IDI_ICON1 ICON "icon1.ico" //used as icon for dialogbox //=============================================================================