Goz*_*Goz 13
我很确定复选框的宽度等于
int x = GetSystemMetrics( SM_CXMENUCHECK );
int y = GetSystemMetrics( SM_CYMENUCHECK );
Run Code Online (Sandbox Code Playgroud)
然后你可以通过减去以下内容来计算出里面的区域......
int xInner = GetSystemMetrics( SM_CXEDGE );
int yInner = GetSystemMetrics( SM_CYEDGE );
Run Code Online (Sandbox Code Playgroud)
我在我的代码中使用它,到目前为止没有问题...
简短回答:

从MSDN 布局规范:Win32,我们有一个复选框的尺寸规格.
从控件的左边缘到文本的开头是12个对话框单元:

复选框控件高10个对话框单位:
Surfaces and Controls Height (DLUs) Width (DLUs)
===================== ============= ===========
Check box 10 As wide as possible (usually to the margins) to accommodate localization requirements.
Run Code Online (Sandbox Code Playgroud)
首先,我们计算水平和垂直对话框的大小:
const dluCheckBoxInternalSpacing = 12; //12 horizontal dlus
const dluCheckboxHeight = 10; //10 vertical dlus
Size dialogUnits = GetAveCharSize(dc);
Integer checkboxSpacing = MulDiv(dluCheckboxSpacing, dialogUnits.Width, 4);
Integer checkboxHeight = MulDiv(dluCheckboxHeight, dialogUnits.Height, 8);
Run Code Online (Sandbox Code Playgroud)
使用方便的帮助函数:
Size GetAveCharSize(HDC dc)
{
/*
How To Calculate Dialog Base Units with Non-System-Based Font
http://support.microsoft.com/kb/125681
*/
TEXTMETRIC tm;
GetTextMetrics(dc, ref tm);
String buffer = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Size result;
GetTextExtentPoint32(dc, buffer, 52, out result);
result.Width = (result.X/26 + 1) / 2; //div uses trunc rounding; we want arithmetic rounding
result.Height = tm.tmHeight;
return result;
}
Run Code Online (Sandbox Code Playgroud)
现在我们知道checkboxSpacing要添加多少像素(),我们正常计算标签大小:
textRect = Rect(0,0,0,0);
DrawText(dc, Caption, -1, textRect, DT_CALCRECT or DT_LEFT or DT_SINGLELINE);
chkVerification.Width = checkboxSpacing+textRect.Right;
chkVerification.Height = checkboxHeight;
Run Code Online (Sandbox Code Playgroud)

注意:任何代码都会发布到公共领域.无需归属.