tags:

views:

1221

answers:

3

How do I create a button control (with CreateWindow of a BUTTON window class) that has a standard system-wide size (especially height) that's consistent with the rest of Windows applications? I should of course take DPI into account and probably other settings.

Remark: Using USE_CW_DEFAULT for width and height results in a 0, 0 size button, so that's not a solution.

+6  A: 

This is what MSDN has to say: Design Specifications and Guidelines - Visual Design: Layout.

The default size of a button is 50x14 DLUs, which can be calculated to pixels using the examples shown for GetDialogBaseUnits.

The MapDialogRect function seems to do the calculation for you.

Timbo
MapDialogRect doesn't help if your window is not a dialog unfortunately, but calculating by hand works very well.
macbirdie
+2  A: 

In the perfect, hassle-free world...

To create a standard size button we would have to do this:

LONG units = GetDialogBaseUnits();
m_hButton = CreateWindow(TEXT("BUTTON"), TEXT("Close"), 
                 WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
                 0, 0, MulDiv(LOWORD(units), 50, 4), MulDiv(HIWORD(units), 14, 8),
                 hwnd, NULL, hInst, NULL);

where 50 and 14 are respective DLU dimensions, 4 and 8 are horizontal and vertical dialog template units respectively, based on GetDialogBaseUnits() function documentation remarks.


Nothing's perfect

BUT as Anders pointed out, those metrics are based on the system font. If your window uses a shell dialog font or simply anything not making your eyes bleed, you're pretty much on your own.

To get your own "dialog" base units, you have to retrieve current text metrics with GetTextMetrics() and use character height and average width (tmHeight and tmAveCharWidth of the TEXTMETRIC struct respectively) and translate them with MulDiv by your own, unless you are in a dialog, then MapDialogRect() will do all the job for you.

Note that tmAveCharWidth only approximates the actual average character width so it's recommended to use a GetTextExtentPoint32() function on an alphabetic character set instead.

See:


Simpler alternative

If buttons are the only control you want to resize automatically, you can also use BCM_GETIDEALSIZE message Button_GetIdealSize() macro (Windows XP and up only) to retrieve optimal width and height that fits anything the button contains, though it looks pretty ugly without any margins applied around the button's text.

macbirdie
In an even more perfect world, GUI layout in 2009 wouldn't involve pixel coordinates at all. :) Dynamic layout managers are a better solution in the majority of cases, imo.
unwind
Amen to that! :) Although even in this pixel-ridden Win32 world, you've got some helpful functions for that, so you never have to deal with pixel coordinates explicitly.
macbirdie
yup, `BCM_GETIDEALSIZE` gives you shooty dims
Janusz Lenar
+1  A: 

@macbirdie: you should NOT use GetDialogBaseUnits(), it is based on the default system font (Ugly bitmap font). You should use MapDialogRect()

Anders
As I mentioned in a previous comment, MapDialogRect doesn't work for non-dialog windows, so I guess I have to try the GetTextExtentPoint32 and GetTextMetrics functions as suggested.
macbirdie