ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:26:04 24 4
gpt4 key购买 nike

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

#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/dialog 中,只需继承 DateTime 选择器:

SetWindowSubclass(hwndOfYourDateTimePicker, DTP, 0, 0);

新问题:

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

最佳答案

我在父窗口中捕获 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;
}

关于c++ - 启用视觉样式的日期和时间选择器 -> 更改标题的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28511802/

24 4 0
文章推荐: c++ - 二维 vector ( vector 下标超出范围)
文章推荐: swift - 为什么 applyImpulse 在 swift spritekit 中不起作用?
文章推荐: swift - 弹出导航堆栈中的特定 View Controller ,而不会看到中间的其他 VC
文章推荐: ios - UICollectionView 单元格选择的奇怪行为
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com