如何在复选框中获取支票和间隙的大小?

Mar*_*ram 14 c++ windows checkbox winapi

我有一个我想要准确测量的复选框,所以我可以正确地在对话框上定位控件.我可以轻松地测量控件上文本的大小 - 但我不知道计算复选框大小的"官方"方式以及文本之前(或之后)的间隙.

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)

我在我的代码中使用它,到目前为止没有问题...


Ian*_*oyd 7

简短回答:

在此输入图像描述

长版

从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)

在此输入图像描述

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