How to get size of check and gap in check box?

前端 未结 7 1438
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 06:46

I have a check box that I want to accurately measure so I can position controls on a dialog correctly. I can easily measure the size of the text on the control - but I don\'

7条回答
  •  失恋的感觉
    2020-12-15 06:59

    Sorry for resurrecting this old thread. I recently found myself wondering about the exact same question. Currently, none of the answers above give a result consistent with Windows 10 for different fonts and font sizes, especially in high-DPI environments.

    Instead, it seems that the correct result is obtained by

    SIZE szCheckBox;
    GetThemePartSize(hTheme, hDC, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rcBackgroundContent, TS_TRUE, &szCheckBox);
    

    for the size of the checkbox itself. And

    SIZE szZeroCharacter;
    GetTextExtentPoint32(hDC, L"0", 1, &szZeroCharacter);
    int iGapWidth = szZeroCharacter.cx / 2;
    

    for the width of the gap. After trying a lot of different methods inspired by the posts above, I found L"0" in the dissembly of comctl32.dll. And while it looks like a joke to me (not necessarily a good one), I suspect it's a holdover from the old days when this might have been a good enough approximation of 2DLU.

    Disclaimer: While I tested the result with various fonts and different sizes on Windows 10, I have not attempted to verify that it also holds on any other (older) version of the operating system.

提交回复
热议问题