NLScotty
NLScotty

Reputation: 96

Win32 Tab Control grey background

I am trying to make make my ui in win32 everywhere white. The problem is that my tab control's background is not white , so the not the tab itself, but the party next to the tabs is grey.

Like this

I am handling WM_CTLCOLORSTATIC for the static controls, but it does not seems to work for the tab control.

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

I hope there is an 'easy' way to make my entire ui white :)

Grz

Upvotes: 4

Views: 2716

Answers (2)

Gabriel
Gabriel

Reputation: 21

Sorry about my english. What I do is return 0 in WM_PRINTCLIENT message without doing anything, I mean, preventing WM_PRINTCLIENT from calling DefWindowProc. This causes the tabcontrol header to have the same background color as its parent window. The same goes for the TrackBar.

I only have it tested on windows 10, I would like to know if it works for win 7 too.

Upvotes: 1

You can not capture a message to draw on the grey background. The system draws everything in the WM_PRINTCLIENT. There is however a nice hack! The idea is from 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.

I am using TransparentBlt in this solution:

create hdcMemTab, step 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);

In WM_PAINT of the subclassed tab control:

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;

Upvotes: 1

Related Questions