Win32 DateTime_SetMonthCalColor seems not work

Viewed 169

I want to change the background color of a datetimepicker contorl's calender. According windows SDK ,there's a marco DateTime_SetMonthCalColor.

DateTime_SetMonthCalColor(hwnd,MSC_BACKGROUND,RGB(0,120,250));

But not work. I do this on windows control spy,send the same message DTM_SETMCCOLOR , no effect either. So what's wrong ?

1 Answers

MCSC_BACKGROUND sets the background color of the month calendar control itself, which only shows when the dropdown is resized to more than the default single month. The background of individual months is set with MCSC_MONTHBK instead.

enter image description here

The month calendar control gets created each time the dropdown is opened. In order to resize it, the parent window of the date-time picker control must process the DTN_DROPDOWN notification, like in the minimal example below.

HWND CreateDTC(HWND hWnd)
{
    INITCOMMONCONTROLSEX icex = { sizeof(icex), ICC_DATE_CLASSES };
    InitCommonControlsEx(&icex);

    return CreateWindowExW(0, DATETIMEPICK_CLASS, L"DateTime",
                           WS_BORDER | WS_CHILD | WS_VISIBLE | DTS_SHOWNONE,
                           10, 10, 150, 25, hWnd, nullptr, hInst, nullptr);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND hDTC = nullptr;

    switch(msg)
    {
    case WM_CREATE:
        hDTC = CreateDTC(hWnd);
        DateTime_SetMonthCalColor(hDTC, MCSC_BACKGROUND, RGB(0, 120, 250)); // blue
        DateTime_SetMonthCalColor(hDTC, MCSC_MONTHBK, RGB(120, 250, 0));    // green
        break;

    case WM_NOTIFY:
        switch(((LPNMHDR)lParam)->code)
        {
        case DTN_DROPDOWN:
            HWND hMC = DateTime_GetMonthCal(hDTC);
            RECT rc;
            MonthCal_GetMinReqRect(hMC, &rc);
            MoveWindow(hMC, 0, 0, (5 * rc.right) / 2, (3 * rc.bottom) / 2, FALSE);
            break;
        }
        break;
    //...
    }
    return 0;
}
Related