启用了视觉样式的日期和时间选择器 - >更改标题的背景颜色

时间:2015-02-14 02:32:33

标签: c++ c winapi datepicker

我为我的应用程序启用了视觉样式:

#pragma comment( linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
    language='*'\"")

我可以使用以下方法在编辑控件中设置日期字体:

HFONT hf = (HFONT)SendMessage(hwndDateTimePicker, WM_GETFONT, (WPARAM)0, (LPARAM)0);
if (hf != NULL)
{
    LOGFONT lf = { 0 };

    GetObject(hf, sizeof(LOGFONT), &lf);

    lf.lfWeight = FW_BOLD;
    lf.lfUnderline = TRUE;

    hf = CreateFontIndirect(&lf);

    SendMessage(GetDlgItem(hDlg, IDC_DATETIMEPICKER1), WM_SETFONT
        (WPARAM)hf, (LPARAM)TRUE);
}

但是,尝试使用GetDCSetTextColor(例如)不起作用。

下图显示了我的目标(颜色变为黄色)。

enter image description here

我试过继承控件,但是tjat失败了。这是代码,以防您发现错误:

LRESULT CALLBACK Decimalni(HWND hwnd, UINT message, 
    WPARAM wParam, LPARAM lParam, 
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (message)
    {
    case WM_CTLCOLOREDIT:
        return (LRESULT)((HBRUSH)GetStockObject(GRAY_BRUSH));
    case WM_NCDESTROY:
        ::RemoveWindowSubclass(hwnd, Decimalni, 0);
        return DefSubclassProc(hwnd, message, wParam, lParam);
        break;
    }
    return ::DefSubclassProc(hwnd, message, wParam, lParam);
}


// in WM_INITDIALOG
SetWindowSubclass(hwndDTP, Decimalni, 0, 0 );

问题:

我的问题是否有解决方法?

也许有办法获得编辑控件的句柄并将其子类化,例如?只是一个想法......

编辑:

这是我能做的最好的事情:

// subclass procedure for date time picker
LRESULT CALLBACK DTP(HWND hwnd, UINT message, 
    WPARAM wParam, LPARAM lParam,
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (message)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps = { 0 };

        HDC hdc = BeginPaint(hwnd, &ps);

        RECT rcClient = { 0 };
        GetClientRect(hwnd, &rcClient);
        // Fill client area with desired brush, I used light gray as an example
        FillRect(hdc, &rcClient, (HBRUSH)GetStockObject(LTGRAY_BRUSH));

        BITMAPINFO bmi;
        // Create a memory DC
        HDC memDC = CreateCompatibleDC(hdc);

        // Create a DIB header for parent
        memset(&bmi, 0, sizeof(bmi));
        bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmi.bmiHeader.biWidth = rcClient.right - rcClient.left;
        bmi.bmiHeader.biHeight = rcClient.bottom - rcClient.top;
        bmi.bmiHeader.biBitCount = 24;
        bmi.bmiHeader.biPlanes = 1;
        bmi.bmiHeader.biCompression = BI_RGB;

        // Create a DIB bitmap
        HBITMAP tempBmp = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, 0, 0, 0);

        // Select tempBmp onto DC to force size and DIB change on the DC
        HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, tempBmp);

        // Okay get datetime picker to draw on our memory DC
        DefSubclassProc(hwnd, WM_PRINTCLIENT, (WPARAM)memDC, (LPARAM)(PRF_CLIENT));

        // Transfer the memory DC onto datetime picker DC excluding default white color
        TransparentBlt(hdc, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, 
            memDC, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, GetSysColor(COLOR_WINDOW));

        // cleanup
        SelectObject(memDC, oldBmp);
        DeleteObject(tempBmp);
        DeleteDC(memDC);

        EndPaint(hwnd, &ps);
    }
        return 0L;
    case WM_NCDESTROY:
        ::RemoveWindowSubclass(hwnd, DTP, 0);
        return DefSubclassProc(hwnd, message, wParam, lParam);
    }
    return ::DefSubclassProc(hwnd, message, wParam, lParam);
}

在主windiw /对话框中,只是将DateTime选择器子类化:

SetWindowSubclass(hwndOfYourDateTimePicker, DTP, 0, 0);

新问题:

  • 您会注意到下拉按钮在视觉上并不吸引人。有没有办法纠正这个?
  • ClearType字体可能会提供一些工件,有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:1)

我在父窗口中捕获WM_CTLCOLORxxx的实验结果

代码如下。没有任何参数,它只会打印出WM_CTLCOLORxxx条消息。通过参数,它会尝试更改控件颜色和文本背景颜色(为了简单起见,但基于它们的不同之处)。

Windows 7,没有视觉样式/公共控件5 :没有WM_CTLCOLORxx消息;颜色没有变化
Windows 7,视觉样式/公共控件6 :多条WM_CTLCOLORSTATIC条消息,但设置颜色似乎无效 wine :一条WM_CTLCOLREDIT条消息,已应用返回的画笔,但文字背景颜色不是

这都是32位二进制文​​件;我怀疑使用64位二进制文​​件会改变任何东西(至少,我希望不会)。

因此仅检查父窗口中的WM_CTLCOLORxxx消息将无效。这个答案没有回答这个问题,但肯定有更多实验的空间(也许更多的子类化测试?)。

我在Windows 7上查看了Spy ++中的视觉样式/ Common Controls 6设置,但我没有看到日期时间选择器的任何子项。

希望现在有所帮助。

// 17 february 2015
#define UNICODE
#define _UNICODE
#define STRICT
#define STRICT_TYPED_ITEMIDS
#define CINTERFACE
#define COBJMACROS
// get Windows version right; right now Windows XP
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#define _WIN32_WINDOWS 0x0501       /* according to Microsoft's winperf.h */
#define _WIN32_IE 0x0600            /* according to Microsoft's sdkddkver.h */
#define NTDDI_VERSION 0x05010000    /* according to Microsoft's sdkddkver.h */
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>

HWND dtp;
BOOL returnColors = FALSE;

LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
#define CTLCOLOR(which) \
    case which: \
        printf("%s %p\n", #which, (HWND) lParam); \
        if (returnColors) { \
            SetBkColor((HDC) wParam, GetSysColor(COLOR_ACTIVECAPTION)); \
            return (LRESULT) GetSysColorBrush(COLOR_GRADIENTACTIVECAPTION); \
        } \
        break; /* fall through to DefWindowProc() */
    CTLCOLOR(WM_CTLCOLORMSGBOX)
    CTLCOLOR(WM_CTLCOLOREDIT)
    CTLCOLOR(WM_CTLCOLORLISTBOX)
    CTLCOLOR(WM_CTLCOLORBTN)
    CTLCOLOR(WM_CTLCOLORDLG)
    CTLCOLOR(WM_CTLCOLORSCROLLBAR)
    CTLCOLOR(WM_CTLCOLORSTATIC)
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int main(int argc, char *argv[])
{
    INITCOMMONCONTROLSEX icc;
    WNDCLASSW wc;
    HWND mainwin;
    MSG msg;

    returnColors = argc > 1;

    ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX));
    icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
    icc.dwICC = ICC_DATE_CLASSES;
    if (InitCommonControlsEx(&icc) == 0) {
        fprintf(stderr, "InitCommonControlsEx() failed: %I32u\n", GetLastError());
        return 1;
    }

    ZeroMemory(&wc, sizeof (WNDCLASSW));
    wc.lpszClassName = L"mainwin";
    wc.lpfnWndProc = wndproc;
    wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    wc.hInstance = GetModuleHandle(NULL);
    if (RegisterClassW(&wc) == 0) {
        fprintf(stderr, "RegisterClassW() failed: %I32u\n", GetLastError());
        return 1;
    }

    mainwin = CreateWindowExW(0,
        L"mainwin", L"Main Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        320, 240,
        NULL, NULL, GetModuleHandle(NULL), NULL);
    if (mainwin == NULL) {
        fprintf(stderr, "create main window failed: %I32u", GetLastError());
        return 1;
    }

    dtp = CreateWindowExW(0,
        DATETIMEPICK_CLASSW, L"",
        DTS_LONGDATEFORMAT | WS_CHILD | WS_VISIBLE,
        20, 20, 200, 180,
        mainwin, NULL, GetModuleHandle(NULL), NULL);
    if (dtp == NULL) {
        fprintf(stderr, "create date-time picker failed: %I32u\n", GetLastError());
        return 1;
    }
    printf("dtp %p\n", dtp);

    ShowWindow(mainwin, SW_SHOWDEFAULT);
    UpdateWindow(mainwin);

    while (GetMessageW(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}