Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinAPI Tab Bar Rendering

Tags:

winapi

I'm writing a C-based WinAPI program that has a tab control in the client area of the main window. This tab control works great, except there appears to be some kind of rendering issue with the tabs. The titles of tabs are rendered in bold, unaliased fonts, and therefore waste a lot of screen real estate: Here's what tabs look like in essentially every other application:

I use this code to set up my tab control:

RECT rcClient, rcTool, rcTab;
TCHAR tabTitleTmp[256]; // Temp string buffer

HWND hTool = GetDlgItem(hWnd, IDC_MAIN_TOOL);
GetWindowRect(hTool, &rcTool);
int iToolHeight = rcTool.bottom - rcTool.top;

// Get parent's client rect
GetClientRect(hWnd, &rcClient); 

// Create tab control
HWND hwndTab = CreateWindowEx(NULL, WC_TABCONTROL, NULL, WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 
     0, iToolHeight, rcClient.right, rcClient.bottom - iToolHeight, hWnd, (HMENU) IDC_MAIN_TAB,
     hInst, NULL);

// Create tab items
TCITEM tie; 
tie.mask = TCIF_TEXT | TCIF_IMAGE; 
tie.iImage = -1; 
tie.pszText = tabTitleTmp; 

// Set up tabs
for(int i = 0; i < 8; i++) {
    LoadString(hInst, IDC_TAB_GENERAL + i, tabTitleTmp, sizeof(tabTitleTmp) / sizeof(tabTitleTmp[0]));
    TabCtrl_InsertItem(hwndTab, i, &tie);
}

Does anyone know the solution to this problem? I haven't yet found it on Google, and I'm beginning to think this might just be a bug in WinAPI itself. Thanks for any responses!

Edit: I call InitCommonControlsEx() at the beginning of my program before creating any controls, so the common control classes are registered.

like image 284
Tristan Avatar asked Sep 28 '13 23:09

Tristan


1 Answers

You need to set the font on the tab control using SendMessage() with the WM_SETFONT message. You can use GetStockObject(DEFAULT_GUI_FONT) to get the default GUI font, You can use SystemParametersInfo() to get the default font, or you can set a different font using CreateFont().

NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, NULL);
HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);
SendMessage(hwndTab, WM_SETFONT, (WPARAM)hFont, true);
like image 166
Drew Chapin Avatar answered Oct 13 '22 01:10

Drew Chapin