Home

Windows Controls: Date Time Picker

   

Introduction

These are the steps to add a date time picker control to an application. This exercise uses Microsoft Visual C++.

 

Practical LearningPractical Learning: Introducting the Date Time Picker

  1. Start Microsoft Visual Studio or Visual C++
  2. On the main menu, click File -> New Project...
  3. In the left list, click Visual C++ Project.
    In the right list, click Win32 Project
  4. Set the Name to DateTimePickerControl
     
    New Project
  5. Click OK
  6. In the first page of the wizard, click Next
  7. In the second page, click Windows Application and click Empty Project
     
    Win32 Application Wizard
  8. Click Finish

Practical LearningPractical Learning: Creating a Resource Header

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click Header File
  3. Set the Name to Resource
  4. Click Add
  5. In the empty document, type: #define IDD_EXERCISE_DLG 1010

Practical LearningPractical Learning: Creating a Resource File

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click Text File
  3. Set the Name to Exercise.rc (make sure you include the extension)
  4. Click Add
  5. In the Solution Explorer, right-click Exercise.rc and click Open With...
  6. In the Open With dialog box, click Source Code (Text) Editor
     
    Open With
  7. Click OK
  8. In the empty document, type the following:
    #include <afxres.h>
    #include "Resource.h"
    
    IDD_EXERCISE_DLG DIALOG 260, 200, 158, 140
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Date Time Picker"
    FONT 8, "MS Shell Dlg"
    BEGIN
    DEFPUSHBUTTON   "OK", IDOK, 100, 120, 50, 14
    END

Practical LearningPractical Learning: Creating the Source Code

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click C++ File (.cpp)
  3. Set the Name to Exercise
  4. Click Add
  5. In the empty document, type the following:
    #include <windows.h>
    #include "Resource.h"
    
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInstGlobal;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	hInstGlobal = hInstance;
    
    	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_EXERCISE_DLG),
    	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
            break;
    	}
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
  6. Execute the application to see the result
     
    Date Time Picker
  7. Close the dialog box and return to your programming environment
 
 
 

Practical LearningPractical Learning: Adding the Common Controls Library

  1. On the main menu, click Project -> Add Existing Item...
  2. Locate the directory where Microsoft Visual C++ libraries are installed. By default, for Microsoft Visual C++ 2010, this should be
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\
  3. Select ComCtl32.Lib
     
    Add Existing Item
  4. Click Add
  5. In the empty document, type: #define IDC_DATETIMEPICKER 2012 and press Enter

Practical LearningPractical Learning: Programmatically Creating the Control

  1. Access the Exercise.cpp file
  2. To programmatically create a date time picker, change the code as follows:
    #include <windows.h>
    #include <commctrl.h>
    #include "Resource.h"
    
    No Change . . .
    
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        INITCOMMONCONTROLSEX icex;
    
        icex.dwSize = sizeof(icex);
        icex.dwICC = ICC_DATE_CLASSES;
    
        InitCommonControlsEx(&icex);
    
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		
    		CreateWindowEx(0,
                           DATETIMEPICK_CLASS,
                           TEXT("DateTime"),
                           WS_BORDER | WS_CHILD | WS_VISIBLE | DTS_SHOWNONE,
                           10,10,220,20,
    		       hWndDlg,
                           NULL,
                           hInstGlobal,
                           NULL);
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
    	}
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
  3. Execute the application to see the result
     
    Date Time Picker
     
    Date Time Picker
  4. Close the dialog box and return to your programming environment

Practical LearningPractical Learning: Manually Creating a Data Time Picker

  1. In the Exercise.cpp file, delete the code in the WM_INITDIALOG event:
    #include <windows.h>
    #include <commctrl.h>
    #include "Resource.h"
    
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInstGlobal;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	hInstGlobal = hInstance;
    
    	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_EXERCISE_DLG),
    	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        INITCOMMONCONTROLSEX icex;
    
        icex.dwSize = sizeof(icex);
        icex.dwICC = ICC_DATE_CLASSES;
    
        InitCommonControlsEx(&icex);
    
        switch(msg)
        {
    	case WM_INITDIALOG:
    	
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------
  2. Access the Exercise.rc file and change it as follows:
    #include <afxres.h>
    #include "Resource.h"
    
    IDD_EXERCISE_DLG DIALOG 260, 200, 158, 140
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Date Time Picker"
    FONT 8, "MS Shell Dlg"
    BEGIN
        CONTROL         "", IDC_DATETIMEPICKER, "SysDateTimePick32",
                        WS_CHILD | WS_TABSTOP, 10, 10, 140, 14
        DEFPUSHBUTTON   "OK", IDOK, 100, 120, 50, 14
    END
  3. Execute the application to see the result
  4. Close the dialog box and return to your programming environment
 
 
   
 

Home  

 

Resource Header:

#define IDD_EXERCISE_DLG   2020
#define IDC_DATETIMEPICKER 2012
 

Resource File:

#include >afxres.h>
#include "Resource.h"

IDD_EXERCISE_DLG DIALOG 260, 200, 158, 140
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Date Time Picker"
FONT 8, "MS Shell Dlg"
BEGIN
    CONTROL         "", IDC_DATETIMEPICKER, "SysDateTimePick32",
                    WS_CHILD | WS_TABSTOP, 10, 10, 140, 14
    DEFPUSHBUTTON   "OK", IDOK, 100, 120, 50, 14
END

Source Code:

#include <windows.h>
#include <commctrl.h>
#include "Resource.h"

//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInstGlobal;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	hInstGlobal = hInstance;

	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_EXERCISE_DLG),
	          hWnd, reinterpret_cast>DLGPROC>(DlgProc));

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	INITCOMMONCONTROLSEX icex;

    icex.dwSize = sizeof(icex);
    icex.dwICC = ICC_DATE_CLASSES;

    InitCommonControlsEx(&icex);

	switch(msg)
	{
	case WM_INITDIALOG:
		
		/*CreateWindowEx(0,
                       DATETIMEPICK_CLASS,
                       TEXT("DateTime"),
                       WS_BORDER|WS_CHILD|WS_VISIBLE|DTS_SHOWNONE,
                       10,10,220,20,
					   hWndDlg,
                       NULL,
                       hInstGlobal,
                       NULL);*/
		return TRUE;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDOK:
			EndDialog(hWndDlg, 0);
			return TRUE;
		}
		break;

	case WM_CLOSE:
        PostQuitMessage(WM_QUIT);
        break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------