Win32选项卡控制灰色背景

时间:2015-01-22 04:21:54

标签: c++ winapi background

我试图让我的ui在win32到处都是白色的。问题是我的标签控件的背景不是白色,所以不是标签本身,而是标签旁边的一方是灰色的。

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

我希望有一个简单的'让我整个ui变白的方法:)

GRZ

2 个答案:

答案 0 :(得分:0)

您无法捕捉要在灰色背景上绘制的消息。系统会在WM_PRINTCLIENT中绘制所有内容。然而,有一个很好的黑客!这个想法来自this post

  

我这样做(在我的WM_PAINT处理程序中):

     
      
  1. 创建内存DC以进入

  2.   
  3. 向选项卡控件发送WM_PRINTCLIENT消息,以便将选项卡绘制到内存中

  4.   
  5. 创建一个镜像选项卡形状的区域

  6.   
  7. 使用所需的背景笔刷填充此区域外的内存DC部分(RGN_DIFF)

  8.   
  9. 将结果Blt到BeginPaint返回的DC

  10.   
  11. 调用EndPaint并返回,不用调用选项卡控件自带的WndProc:)

  12.         

    步骤3有点繁琐,因为你必须知道标签的位置和形状,但其他   而不是它是一个非常干净的解决方案(见下面的示例代码图片)。您   可能会使用TransparentBlt替换系统背景颜色。

我在此解决方案中使用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;

答案 1 :(得分:0)

抱歉我的英语。我做的是在WM_PRINTCLIENT消息中返回0而不做任何事情,我的意思是,阻止WM_PRINTCLIENT调用DefWindowProc。这会导致tabcontrol标题与其父窗口具有相同的背景颜色。 TrackBar也是如此。

我只在Windows 10上测试过,我想知道它是否适用于win 7。