jeudi 1 avril 2021

MFC CheckBox - retrieve accurate square size

The problem has been discussed here, but people settled for an inaccurate solution. I'm using a CButton class with a BS_AUTOCHECKBOX flag. Is there a precise way to determine the size of the square with a black border (which holds the checkmark) on Windows/MFC? And also the gap between this square and text?

enter image description here

I realized, the gap can be calculated like this (scale factor depends on the screen settings - 100%dpi, scale_factor==1; 150%dpi, scale_factor==1.5, etc)

std::ceil(3 * scale_factor)

I need my solution to work correctly despite the screen resolution/DPI settings. Windows creates this square 13x13px on 100% DPI, 16x16px on 120% DPI, but 20x20px on both 150% and 175% DPI! Therefore, it's impossible to just multiply 13 with the scale_factor.

This code

int x = GetSystemMetrics(SM_CXMENUCHECK);
int y = GetSystemMetrics(SM_CYMENUCHECK);

returns 15px, and if I subtract

int xInner = GetSystemMetrics( SM_CXEDGE );
int yInner = GetSystemMetrics( SM_CYEDGE );

I get my 13px on 100% dpi, but the result is not correct on different screen DPI settings (because the SM_CXMENUCHECK doesn't represent the standard CButton checkbox).

I'd like to avoid custom drawing. Is there any way to do this with pixel-precision (if not, what's the best possible solution you came up with)? Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire