Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default button size?

Tags:

winapi

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.

like image 294
macbirdie Avatar asked Sep 12 '08 10:09

macbirdie


2 Answers

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.

like image 123
Timbo Avatar answered Oct 22 '22 16:10

Timbo


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:

  • How to calculate dialog box units based on the current font in Visual C++
  • How To Calculate Dialog Base Units with Non-System-Based Font

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.

like image 22
macbirdie Avatar answered Oct 22 '22 16:10

macbirdie