gpt4 book ai didi

c++ - Win32 选项卡控件灰色背景

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:22:45 24 4
gpt4 key购买 nike

我正在努力使我在 win32 中的 ui 无处不在。问题是我的选项卡控件的背景不是白色的,所以不是选项卡本身,而是选项卡旁边的部分是灰色的。

Like this

我正在为静态控件处理 WM_CTLCOLORSTATIC,但它似乎不适用于选项卡控件。

case WM_CTLCOLORSTATIC:
{
HDC hEdit = (HDC)w_param;
SetBkMode(hEdit, TRANSPARENT);
SetTextColor(hEdit, RGB(0, 0, 0));
SetBkColor(hEdit, RGB(255, 255, 255));
// Do not return a brush created by CreateSolidBrush(...) because you'll get a memory leak
return (INT_PTR)GetStockObject(WHITE_BRUSH);
}

我希望有一种“简单”的方法可以让我的整个用户界面变白:)

加油

最佳答案

您不能在灰色背景上绘制消息。系统绘制WM_PRINTCLIENT 中的所有内容。然而,有一个很好的技巧!思路来自this post .

I do this (in my WM_PAINT handler):

  1. Create a memory DC to draw into

  2. Send a WM_PRINTCLIENT message to the tab control to get it to draw the tabs into your memory DC

  3. Create a region which mirrors the shape of the tabs

  4. Fill the parts of the memory DC outside this region (RGN_DIFF) with the desired background brush

  5. Blt the result into the DC returned by BeginPaint

  6. Call EndPaint and return, without calling the tab control's own WndProc of course :)

Step 3 is a bit fiddly as you have to know the location and shape of the tabs, but other than that it's a pretty clean solution (see image below the following sample code). You could probably use TransparentBlt to replace the system background colour instead.

我在此解决方案中使用 TransparentBlt:

创建hdcMemTab第 1 步

HBITMAP hBitmap, hBitmapOld ; //keep them till the end of the program
HDC hdcMemTab; //keep it till the end of the program

HDC hdc;
Rect rt;
hdc = CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
hdcMemTab = CreateCompatibleDC(hdc);
GetWindowRect(hwnd_Tab, &rt);

rt.right = rt.right - rt.left;
rt.bottom = rt.bottom - rt.top;
rt.left = 0;
rt.top = 0;

hBitmap = CreateCompatibleBitmap(hdc, rt.right, rt.bottom);
hBitmapOld = SelectObject(hdcMemTab, hBitmap);
DeleteDC(hdc);

在子类选项卡控件的 WM_PAINT 中:

RECT rt, rtTab;
HDC hdc = BeginPaint(hwnd, &ps);

GetWindowRect(hwnd_Tab, &rt);

rt.right = rt.right - rt.left;
rt.bottom = rt.bottom - rt.top;
rt.left = 0;
rt.top = 0;

//step 2
SendMessage(hwnd_Tab, WM_PRINTCLIENT, (WPARAM)hdcMemTab, PRF_CLIENT);

FillRect(hdc, &rt, gBrushWhite); //gBrushWhite has the desired background color

HRGN hRgn = CreateRectRgn(0, 0, 0, 0);

int n_items = TabCtrl_GetItemCount(hwnd_Tab);

//get tabs region, step 3
for(i = 0; i < n_items; i++){
TabCtrl_GetItemRect(hwnd_Tab, i, &rtTab);

HRGN hTabRgn = CreateRectRgn(rtTab.left, rtTab.top, rtTab.right, rt.bottom);

CombineRgn(hRgn, hRgn, hTabRgn, RGN_OR);

DeleteObject(hTabRgn);
}

GetRgnBox(hRgn, &rtTab);

DeleteObject(hRgn);

//step 5
TransparentBlt(hdc, 0, 0, rt.right, rt.bottom, hdcMemTab, 0, 0, rt.right, rt.bottom, RGB(240, 240, 240)); //(240, 240, 240) is the grey color
BitBlt(hdc, rtTab.left, rtTab.top, rtTab.right - 5, rtTab.bottom, hdcMemTab, rtTab.left, rtTab.top, SRCCOPY);

EndPaint(hwnd, &ps);

//step 6
return 0;

关于c++ - Win32 选项卡控件灰色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com