As any body know how to create a dialog box in Win32, but lot of them are struggling how to place controls on that dialog. We don’t have specific idea how to do that when we are new to Win32. Here I am showing sample code to place a tab control on dialog.
Some of the steps that may be used for creating this application.
- Create a new Empty Win32 application in VC++. The sample name can be SampleTab.
- Click on Menu –> Add To Project –> New –> C++ source file and name the file as SampleTab.cpp
- Copy and paste the code below.
#include <windows.h> #include "resource.h" #include "commctrl.h" BOOL CALLBACK LoginDlgProc(HWND, UINT, WPARAM, LPARAM); int PASCAL WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, int) { int nRet = DialogBoxParam(hInst, MAKEINTRESOURCE (IDD_DIALOG1), NULL, LoginDlgProc, 1); return 0; } BOOL CALLBACK LoginDlgProc(HWND hWnd, UINT uMsg, WPARAM wp, LPARAM lp) { //Initialize the common controls library. //This will enable the program to use Tab control INITCOMMONCONTROLSEX InitCtrlEx; InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX); InitCtrlEx.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&InitCtrlEx); switch(uMsg) { case WM_INITDIALOG: { TCITEM tci1,tci2; tci1.mask = TCIF_TEXT; tci1.pszText = "Pane!"; tci1.cchTextMax = strlen("Pane1") + 1; tci1.iImage = -1; tci1.lParam = 0; tci2.mask = TCIF_TEXT; tci2.pszText = "Pane2"; tci2.cchTextMax = strlen("Pane2") + 1; tci2.iImage = -1; tci2.lParam = 0; TabCtrl_InsertItem(GetDlgItem(hWnd , IDC_TAB1), 0, & tci1); TabCtrl_InsertItem(GetDlgItem(hWnd , IDC_TAB1), 1, & tci2); return TRUE; break; } default: return FALSE; } }
- Add a new dialog by clicking Menu –> Insert –> Resource –> Dialog. This is the dialog editor provided by VC++.
- Keep the Dialog resource id as IDD_DIALOG1.
- Place a tab control with the Resource ID as IDC_TAB1.
- Save the resource file and resource.h file in the project folders.
- Add the resource files to the project by using Menu –> Project –> Add to Project –> Files.
- This program will need the comctl32.lib for initializing the common controls like tab control. So add comctl32.lib to Project Settings –> Link Tab –> Object/Library modules.
- Build and run the project.