Groupbox Font Issue with WinAPI - c++

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.

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

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

Related

Win32 Button doesn't properly display in a Direct2D Window

I have a Direct2D program, and my goal is to have a Win32 button rendered with the Direct2D window.
I was able to compile the program fine, but the problem is that the button only shows when I click on it. Once I click outside of the button, it just goes invisible. It's even invisible when I open the application. What I expect is for the button to be visible all the time. I'm not necessarily worried about if it renders above or below the Direct2D renderer.
When I click on it:
When I click outside the button (just blank):
Here is my render function:
create_drs(); // Creates device resources
m_pRenderTarget->BeginDraw();
m_pRenderTarget->Clear(D2D1::ColorF(bg.r / 255, bg.g / 255, bg.b / 255, 1));
HWND hwndButton = CreateWindow(
L"BUTTON",
L"a button",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
0,
0,
150,
150,
m_hwnd,
NULL,
(HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE),
NULL);
m_pRenderTarget->EndDraw();
I've tried taking out the Clear function, but it's still invisible when the application opens, but stays visible forever once I click on it. I also tried looking online, but couldn't find a sufficient solution for this specific problem.

Draw Resource Icon on Button

I would like to draw my program's icon on an "owner-drawn" button. This could be either an icon in the resource file, or the generic Windows icon. But, even after endless searching, I have not been able to find the code for this. I have come across bits and pieces of answers. But, no complete explanations.
Sorry that I have no code to post. I am totally lost on this one. Either standard Win API or GDI+ will work for me.
When you create the button, add BS_ICON style, then get the handle to your icon, you can use LoadImage.
Finally send BM_SETIMAGE message with the handle to your icon.
Here is the code sample:
HWND button1 = CreateWindow(TEXT("Button"), TEXT("OK"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_ICON,
50, 50, 50, 50, hwnd, (HMENU)1001, hInstance, NULL);
HICON hIcon = (HICON)LoadImage( // returns a HANDLE so we have to cast to HICON
NULL, // hInstance must be NULL when loading from a file
L"iconname.ico", // the icon file name
IMAGE_ICON, // specifies that the file is an icon
0, // width of the image (we'll specify default later on)
0, // height of the image
LR_LOADFROMFILE | // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE | // default metrics based on the type (IMAGE_ICON, 32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
SendMessage(button1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);

Changing the font size of a static textbox in a Windows GUI

how do you change the font size of a static text box in a Windows GUI application written in C++?
HWND hText = CreateWindowW(L"EDIT", L"enter some text", WS_VISIBLE | WS_CHILD | ES_RIGHT, 100, 100, 100, 50, hWnd, NULL, NULL, NULL);
do i have to make another Window message
As #RbMm said that, use CreateFont and WM_SETFONT can achieve this. And the official documents also have corresponding introduction.
Changing the Font Used by an Edit Control.
An application can change the font that an edit control uses by
sending the WM_SETFONT message. Most applications do this while
processing the WM_INITDIALOG message. Changing the font does not
change the size of the edit control; applications that send the
WM_SETFONT message may have to retrieve the font metrics for the text and recalculate the size of the edit control. For more
information about fonts and font metrics, see Fonts and Text.
The least code:
LOGFONT logfont;
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.lfHeight = -20;
HFONT hFont = CreateFontIndirect(&logfont);
SendMessage(hText, WM_SETFONT, (WPARAM)hFont, TRUE);

MFC - Minimize main MDI window only

I have main MDI window and have custom CWnd derived window which I create dynamically run-time. I want to keep that window on the screen even when main MDI window is minimized but I dont want to have top-most window. I have tried use WS_EX_CONTROLPARENT | WS_EX_APPWINDOW styles, set parent to NULL and set owner to GetDesktopWindow() but nothing works.
Any ideas how I should do that?
When window is minimized, it takes down with it all of its child and owned windows.
This code creates a regular (not topmost) window which is not hidden when the main frame is minimized:
HWND hWnd = ::CreateWindow(L"button", L"test", WS_CAPTION|WS_VISIBLE,
100, 100, 200, 200, GetDesktopWindow(), 0, 0, 0);

List Control SetFocus Redraw Error on a Tab Control

Environment: Visual Studio 2008, Visual Studio Feature Pack, MFC Dialog App, Windows XP, New Common Controls.
I'm having a problem with a list control that happens to be on a tab control.
To reproduce the problem simply create a dialog based app. Place a tab control on that dialog, then put a list control onto that tab control. You don't need to add any code to the project. Just build and run. Set the focus to the list view and then either minimize the dialog or bring another window in front of it.
Now bring the dialog back to the foreground, the list will not draw itself correctly.
One thing I have tried is handle the set focus event for the list control, but left it with an empty method body, ie...
void CMyListControl::OnSetFocus(CWnd* window)
{
// Default();
}
Then the redraw problem goes away, however now you can not select items within the list. Uncommenting the call to Default makes the problem come back.
If I move the list off of the tab the problem goes away. If I set the focus to another control on the tab or dialog, the problem goes away. This is a weird one.
In fact, if you watch closely you can see the list drawing itself and then being obscured by the tab control.
I know it's late but I had them same problem today. You need to set ListView's parent to Tab control.
hWndTab = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TABCONTROL, NULL,
WS_CHILD | WS_TABSTOP | WS_VISIBLE,
0, 0, 0, 0, hWnd, (HMENU) IDC_TAB, hInstance, NULL);
hWndList = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, NULL,
WS_CHILD | WS_TABSTOP | WS_VISIBLE | LVS_REPORT,
0, 0, 0, 0, hWndTab, (HMENU) IDC_LIST, hInstance, NULL);
Note parent window handler for hWndList: hWndTab. Or you can use SetParent.