Comment créer un contrôle Month Calendar

Cette rubrique montre comment créer dynamiquement un contrôle Month Calendar à l’aide de la fonction CreateWindowEx .

Bon à savoir

Technologies

Prérequis

  • C/C++
  • Windows Programmation de l’interface utilisateur

Instructions

Pour créer un contrôle Month Calendar, utilisez la fonction CreateWindowEx , en spécifiant la _ classe calendrier monthcal comme classe de fenêtre. Vous devez d’abord inscrire la classe de fenêtre en appelant la fonction InitCommonControlsEx , en spécifiant le bit des _ _ classes de date ICC dans la structure InitCommonControlsEx qui l’accompagne.

L’exemple suivant montre comment créer un contrôle Month Calendar dans une boîte de dialogue non modale existante. Notez que les valeurs de taille transmises à CreateWindowEx ne sont que des zéros. Étant donné que la taille minimale requise dépend de la police utilisée par le contrôle, l’exemple utilise la macro calendrier monthcal _ GetMinReqRect pour demander des informations sur la taille, puis redimensionne le contrôle en appelant SetWindowPos. Si vous modifiez par la suite la police avec WM _ SetFont, les dimensions du contrôle ne changeront pas. Vous devez rappeler calendrier monthcal _ GetMinReqRect et redimensionner le contrôle pour l’adapter à la nouvelle police.

// Child window identifier of the month calendar.
#define IDC_MONTHCAL 101

// Symbols used by SetWindowPos function (arbitrary values).
#define LEFT 35
#define TOP  40

// Description:
//   Creates a month calendar control in a dialog box.  
// Parameters:
//   hwndOwner - handle of the owner window.
// Nonlocal variables:
//   MonthCalDlgProc - window procedure of the dialog box that 
//     contains the month calendar.
//   g_hInst - global instance handle.
//
HRESULT CreateMonthCalDialog(HWND hwndOwner)
{
    RECT rc;
    INITCOMMONCONTROLSEX icex;
    HWND hwndDlg = NULL;
    HWND hwndMonthCal = NULL;

    // Return an error code if the owner handle is invalid.
    if (hwndOwner == NULL)
        return E_INVALIDARG;

    // Load the window class.
    icex.dwSize = sizeof(icex);
    icex.dwICC  = ICC_DATE_CLASSES;
    InitCommonControlsEx(&icex);

    // Create a modeless dialog box to hold the control.
    hwndDlg = CreateDialog(g_hInst,
                    MAKEINTRESOURCE(IDD_DATE_PICKER),
                    hwndOwner,
                    MonthCalDlgProc);
   
    // Return if creating the dialog box failed. 
    if (hwndDlg == NULL)
        return HRESULT_FROM_WIN32(GetLastError()); 
                        
    // Create the month calendar.
    hwndMonthCal  = CreateWindowEx(0,
                    MONTHCAL_CLASS,
                    L"",
                    WS_BORDER | WS_CHILD | WS_VISIBLE | MCS_DAYSTATE,
                    0,0,0,0, // resize it later
                    hwndDlg,
                    (HMENU) IDC_MONTHCAL,
                    g_hInst,
                    NULL);

    // Return if creating the month calendar failed. 
    if (hwndMonthCal == NULL)
        return HRESULT_FROM_WIN32(GetLastError()); 
                     
    // Get the size required to show an entire month.
    MonthCal_GetMinReqRect(hwndMonthCal, &rc);

    // Resize the control now that the size values have been obtained.
    SetWindowPos(hwndMonthCal, NULL, LEFT, TOP, 
        rc.right, rc.bottom, SWP_NOZORDER);

    // Set the calendar to the annual view.
    MonthCal_SetCurrentView(hwndMonthCal, MCMV_YEAR);

    // Make the window visible.
    ShowWindow(hwndDlg, SW_SHOW);

    return S_OK;
}

Référence du contrôle Month Calendar

À propos des contrôles Month Calendar

Utilisation des contrôles Month Calendar