다음을 통해 공유


월 달력 컨트롤을 만드는 방법

이 항목에서는 CreateWindowEx 함수를 사용하여 동적으로 월 달력 컨트롤을 만드는 방법을 보여 줍니다.

알아야 하는 작업

기술

필수 구성 요소

  • C/C++
  • Windows 사용자 인터페이스 프로그래밍

지침

월 달력 컨트롤을 만들려면 CreateWindowEx 함수를 사용하여 MONTHCAL_CLASS를 창 클래스로 지정합니다. 먼저 함께 제공되는 INITCOMMONCONTROLSEX 구조체에서 ICC_DATE_CLASSES 비트를 지정함으로써 InitCommonControlsEx 함수를 호출하여 창 클래스를 등록해야 합니다.

다음 예에서는 기존 모덜리스 대화 상자에서 월 달력 컨트롤을 만드는 방법을 보여 줍니다. CreateWindowEx에 전달된 크기 값은 모두 0입니다. 필요한 최소 크기는 컨트롤이 사용하는 글꼴에 따라 다르므로 이 예에서는 MonthCal_GetMinReqRect 매크로를 사용하여 크기 정보를 요청한 다음 SetWindowPos를 호출하여 컨트롤 크기를 조정합니다. 이후에 WM_SETFONT를 사용하여 글꼴을 변경해도 컨트롤의 크기는 변경되지 않습니다. MonthCal_GetMinReqRect를 다시 호출하고 새 글꼴에 맞게 컨트롤 크기를 조정해야 합니다.

// 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;
}

월 달력 컨트롤 참조

월 달력 컨트롤 정보

월 달력 컨트롤 사용