user667967
user667967

Reputation: 648

Groupbox Font Issue with WinAPI

I have problems with creating a simple Group-Box-Control via CreateWindowEx. The font-size/-style of its caption just doesn’t look right.

I have created a simple Windows Dialog (containing group-boxes, buttons…) with the Visual Studio - Resource Manager. When I load that dialog with DialogBox(…) everything looks normal but when I create another group-box-control on that same dialog via CreateWindowEx(…) the caption of the new control has a different font-size/-style.

With Microsoft Spy++ I was able to see the dwExStyle and dwStyle values of the other groub-boxes, but even when I use the same values in CreateWindowEx I still get a different look.

Here is the code I use to create the new group-box:

HWND hGroup1 = GetDlgItem(_hWnd, IDC_GROUPBOX1);
HWND hGroup2 = CreateWindowEx(            
    WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_NOPARENTNOTIFY,
    L"Button",
    L"Hallo",
    WS_CHILDWINDOW | WS_VISIBLE | BS_GROUPBOX,
    20, 20, 250, 250,
    hGroup1,
    nullptr,
    _hInstance,
    nullptr);

Here is a screen capture of the dialog:

http://imageshack.us/photo/my-images/856/groupboxfontissue.png/

Please let me know where I went wrong and what I can do to fix it.

[EDIT-1]

In regards to Jonathan Potter and Superman, as you suggested I set the font-handle of the new group-box to the same as for the other controls.

HFONT hFont1 = (HFONT)SendMessage(hGroup1, WM_GETFONT, 0, 0); 
HFONT hFont2 = (HFONT)SendMessage(hGroup2, WM_GETFONT, 0, 0);
HFONT hFont3 = (HFONT)SendMessage(_hWnd, WM_GETFONT, 0, 0);
SendMessage(hGroup2, WM_SETFONT, (WPARAM)hFont1, TRUE);

hFont2 = (HFONT)SendMessage(hGroup2, WM_GETFONT, 0, 0);

At the end of this code, I can see that all controls and the dialog window have the same font-handle but only the controls which were created with the Resource Manager have the correct font (which is the system font).

Is there anything else I can do???

[EDIT-2]

I cannot believe it… it works now! Thank you all very much for your help!

I just had to set the hWndParent value in CreateWindowEx(…) to the dialog handle and then use WM_GETFONT and WM_SETFONT to copy the right font.

I wish you all a nice weekend.

Upvotes: 1

Views: 1114

Answers (2)

Superman
Superman

Reputation: 3081

When you place a control in a dialog using the resource editor, the font set to the dialog, which is the parent of the control will be used for it by default.

If you're creating a control dynamically, the system font will be used instead of the font of the dialog.

To get the same font of the dialog for a control that you create dynamically, set the font of the dialog to the control in the WM_INITDIALOG handler.

In the code snippet below, replace m_hWnd with the handle of the parent dialog.

HFONT font = (HFONT)::SendMessage(m_hWnd, WM_GETFONT, 0, 0);
::SendMessage(hGroup2, WM_SETFONT, (WPARAM)font, TRUE);

Upvotes: 1

Jonathan Potter
Jonathan Potter

Reputation: 37152

Controls you create manually (via CreateWindowEx) do not get their font set automatically, and will default to the "system font" (which is what you see in your screenshot). Instead, you need to set the control's font once it has been created. For example,

SendMessage(hGroup2, WM_SETFONT, (WPARAM)SendMessage(hGroup1, WM_GETFONT, 0, 0), TRUE);

Upvotes: 3

Related Questions