win32: How to calculate control sizes for a consistent look across windows versions / themes? win32: How to calculate control sizes for a consistent look across windows versions / themes? windows windows

win32: How to calculate control sizes for a consistent look across windows versions / themes?


This is only a partial answer I'm adding here for reference:

Indeed, using the DEFAULT_GUI_FONT is wrong according to this blog entry by Raymond Chen. So, no need to trust to do "the right thing".

The Design Specifications indicate that Edit Controls should be the same height as Buttons (14 DLU). To convert these to pixel sizes, the Dialog Base Units (DBU) are needed, and while GetDialogBaseUnits() only returns them for the system font, there are MSDN articles describing how to calculate them for other fonts.

1 vertical DBU corresponds to 8 DLU, so an Edit control will be 6 DLU higher than the text it contains. This doesn't look so nice, because the Edit control doesn't center the text vertically but instead aligns it at the top. avoids this by calculating a smaller size for an Edit control. The drawback is that an Edit control will not align nicely next to a Button.

I found a kind of "hacky" solution to that problem by shrinking the client area of the Edit control in an overridden window proc. The following code compares the results (and contains controls using the system font for completeness):

#include <stdlib.h>#include <string.h>#include <windows.h>#include <commctrl.h>typedef struct PaddedControl{    WNDPROC baseWndProc;    int vshrink;} PaddedControl;static HINSTANCE instance;static HWND mainWindow;static HWND buttonSF;static HWND textBoxSF;static HWND buttonMF;static HWND textBoxMF;static HWND buttonMFC;static HWND textBoxMFC;static PaddedControl textBoxMFCPadded;#define WC_mainWindow L"W32CtlTestDemo"static NONCLIENTMETRICSW ncm;static HFONT messageFont;static TEXTMETRICW messageFontMetrics;static int controlHeightSF;static int controlHeightMF;static int buttonWidthSF;static int buttonWidthMF;/* hack to enable visual styles without relying on manifest * found at http://stackoverflow.com/a/10444161 * modified for unicode-only code */static int enableVisualStyles(void){    wchar_t dir[MAX_PATH];    ULONG_PTR ulpActivationCookie = 0;    ACTCTXW actCtx =    {        sizeof(actCtx),        ACTCTX_FLAG_RESOURCE_NAME_VALID            | ACTCTX_FLAG_SET_PROCESS_DEFAULT            | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,        L"shell32.dll", 0, 0, dir, (LPWSTR)124,        0, 0    };    UINT cch = GetSystemDirectoryW(dir, sizeof(dir) / sizeof(*dir));    if (cch >= sizeof(dir) / sizeof(*dir)) { return 0; }    dir[cch] = L'\0';    ActivateActCtx(CreateActCtxW(&actCtx), &ulpActivationCookie);    return (int) ulpActivationCookie;}static void init(void){    INITCOMMONCONTROLSEX icx;    icx.dwSize = sizeof(INITCOMMONCONTROLSEX);    icx.dwICC = ICC_WIN95_CLASSES;    InitCommonControlsEx(&icx);    ncm.cbSize = sizeof(ncm);    SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);    messageFont = CreateFontIndirectW(&ncm.lfStatusFont);    LONG sysDbu = GetDialogBaseUnits();    HDC dc = GetDC(0);    SelectObject(dc, (HGDIOBJ) messageFont);    GetTextMetricsW(dc, &messageFontMetrics);    SIZE sampleSize;    GetTextExtentExPointW(dc,            L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",            52, 0, 0, 0, &sampleSize);    ReleaseDC(0, dc);    controlHeightSF = MulDiv(HIWORD(sysDbu), 14, 8);    controlHeightMF = MulDiv(messageFontMetrics.tmHeight, 14, 8);    buttonWidthSF = MulDiv(LOWORD(sysDbu), 50, 4);    buttonWidthMF = MulDiv(sampleSize.cx, 50, 4 * 52);    instance = GetModuleHandleW(0);}static LRESULT CALLBACK paddedControlProc(        HWND w, UINT msg, WPARAM wp, LPARAM lp){    PaddedControl *self = (PaddedControl *)GetPropW(w, L"paddedControl");    WNDCLASSEXW wc;    switch (msg)    {    case WM_ERASEBKGND:        wc.cbSize = sizeof(wc);        GetClassInfoExW(0, L"Edit", &wc);        RECT cr;        GetClientRect(w, &cr);        cr.top -= self->vshrink;        cr.bottom += self->vshrink;        HDC dc = GetDC(w);        FillRect(dc, &cr, wc.hbrBackground);        ReleaseDC(w, dc);        return 1;    case WM_NCCALCSIZE:        if (!wp) break;        LRESULT result = CallWindowProcW(self->baseWndProc, w, msg, wp, lp);        NCCALCSIZE_PARAMS *p = (NCCALCSIZE_PARAMS *)lp;        int height = p->rgrc[0].bottom - p->rgrc[0].top;        self->vshrink = 0;        if (height > messageFontMetrics.tmHeight + 3)        {            self->vshrink = (height - messageFontMetrics.tmHeight - 3) / 2;            p->rgrc[0].top += self->vshrink;            p->rgrc[0].bottom -= self->vshrink;        }        return result;    }    return CallWindowProcW(self->baseWndProc, w, msg, wp, lp);}static LRESULT CALLBACK wproc(HWND w, UINT msg, WPARAM wp, LPARAM lp){    switch (msg)    {    case WM_CREATE:        buttonSF = CreateWindowExW(0, L"Button", L"sysfont",                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,                4, 4, buttonWidthSF, controlHeightSF,                w, 0, instance, 0);        buttonMF = CreateWindowExW(0, L"Button", L"msgfont",                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,                4, 8 + controlHeightSF, buttonWidthMF, controlHeightMF,                w, 0, instance, 0);        SendMessageW(buttonMF, WM_SETFONT, (WPARAM)messageFont, 0);        buttonMFC = CreateWindowExW(0, L"Button", L"msgfont adj",                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,                4, 12 + controlHeightSF + controlHeightMF,                buttonWidthMF, controlHeightMF,                w, 0, instance, 0);        SendMessageW(buttonMFC, WM_SETFONT, (WPARAM)messageFont, 0);        textBoxSF = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", L"abcdefgh",                WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,                8 + buttonWidthSF, 4, 100, controlHeightSF,                w, 0, instance, 0);        textBoxMF = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", L"abcdefgh",                WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,                8 + buttonWidthMF, 8 + controlHeightSF,                100, controlHeightMF,                w, 0, instance, 0);        SendMessageW(textBoxMF, WM_SETFONT, (WPARAM)messageFont, 0);        textBoxMFC = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", L"abcdefgh",                WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,                8 + buttonWidthMF, 12 + controlHeightSF + controlHeightMF,                100, controlHeightMF,                w, 0, instance, 0);        memset(&textBoxMFCPadded, 0, sizeof(PaddedControl));        textBoxMFCPadded.baseWndProc = (WNDPROC)SetWindowLongPtr(                textBoxMFC, GWLP_WNDPROC, (LONG_PTR)paddedControlProc);        SetPropW(textBoxMFC, L"paddedControl", &textBoxMFCPadded);        SetWindowPos(textBoxMFC, 0, 0, 0, 0, 0,                SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);        SendMessageW(textBoxMFC, WM_SETFONT, (WPARAM)messageFont, 0);        break;    case WM_DESTROY:        PostQuitMessage(0);        break;    }    return DefWindowProcW(w, msg, wp, lp);}int main(int argc, char **argv){    if (argc < 2 || strcmp(argv[1], "-s"))    {        enableVisualStyles();    }    init();    WNDCLASSEXW wc;    memset(&wc, 0, sizeof(wc));    wc.cbSize = sizeof(wc);    wc.hInstance = instance;    wc.lpszClassName = WC_mainWindow;    wc.lpfnWndProc = wproc;    wc.hbrBackground = (HBRUSH) COLOR_WINDOW;    wc.hCursor = LoadCursorA(0, IDC_ARROW);    RegisterClassExW(&wc);    mainWindow = CreateWindowExW(0, WC_mainWindow, L"fontdemo",            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 180,            0, 0, instance, 0);    ShowWindow(mainWindow, SW_SHOWNORMAL);    MSG msg;    while (GetMessageW(&msg, 0, 0, 0) > 0)    {        TranslateMessage(&msg);        DispatchMessageW(&msg);    }    return (int)msg.wParam;}

The last row of controls using this hack is the best I could achieve so far:

screenshots in win10 and win7

As you can see, a problem that still persists is that the heights of Button and Edit controls look different with the visual styles theme of Windows 10. So I'd still be happy to see a better answer to this question.