Determine checkbox square size [MFC]

itom 21 Reputation points
2021-04-01T20:11:27.673+00:00

Hi,

is there any way to determine the exact size of the black-bordered square of the checkbox (CButton with BS_AUTOCHECKBOX style) in MFC? I'm talking only about the rectangle with the black border that contains an optional checkmark. Also, I'd like to determine the width of the gap between this square and the checkbox's text.

83804-image.png

I tried everything mentioned here, but none of the answers gave me the exact result (Windows draws the 13x13px square on 100%, 16x16px on 125% dpi, 20x20px on 150%, ALSO 20x20px on 175% dpi (???), etc.). It seems that the real size doesn't seem to grow proportionally to the DPI, therefore I cannot come up with any formula. I'd also like to avoid a custom-drawn checkbox.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

Accepted answer
  1. Song Zhu - MSFT 906 Reputation points
    2021-04-02T06:31:39.103+00:00

    Yes, you can use GetThemePartSize function .

    some code:

    HTHEME hTheme = ::OpenThemeData(hwnd, L"button");
    SIZE szCheckBox;
    HRESULT hr = GetThemePartSize(hTheme, NULL, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, NULL, TS_TRUE, &szCheckBox); 
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Castorix31 81,721 Reputation points
    2021-04-02T05:09:02.177+00:00

    On which OS ?

    On Windows 10 1909, I get 13*13 for the default check box size (with the usual methods : GetThemePartSize when themed or size of OBM_CHECKBOXES when not themed) and 22-23 at 175%, which is = 13*175% = 22.75

    A screencopy of a (themed) CheckBox at 175% :
    83875-checkbox-175.jpg

    1 person found this answer helpful.