如何获得对话框中控件的宽度 - 高度

时间:2014-10-13 13:05:48

标签: winapi get height width controls

我创建了一个带有一些控件的对话框 -

IDD_DIALOG_EFFECTS DIALOGEX 0, 0, 168, 49
STYLE DS_SETFONT | WS_CHILD
FONT 8, "MS Sans Serif", 400, 0, 0x1
BEGIN
    ---           ---
    ---           ---
    CTEXT           "",3,200,120,60,60, WS_VISIBLE
END

在标题中 - 文件: const int16 kItem = 3;

现在,当我试图获得控件的位置和尺寸时,它并不准确。

// Retrieving the location and dimension of the control
RECT    wRect_proxy;
GetWindowRect(GetDlgItem(hDlg, kItem), &wRect_proxy);
ScreenToClient (hDlg, (LPPOINT)&wRect_proxy);
ScreenToClient (hDlg, (LPPOINT)&(wRect_proxy.right));

// Output of the control as location and position that I am getting is:
wRect_proxy.left:   300     (Expected: 200)
wRect_proxy.top:    195     (Expected: 120)
wRect_proxy.right:  390     (Expected: 60)
wRect_proxy.bottom: 293     (Expected: 60)

我需要计算控件的宽度 - 高度。寻求帮助......

2 个答案:

答案 0 :(得分:5)

你收到的是控制的高度!

RC文件使用Dialog Base Units。 创建对话框时,使用特定字体查找多个像素为1 DLU。

内部MapDalogRect用于将RC文件中的值转换为最终的像素数。

在CRect(0,0,4,8)上使用MapDialogrect可以得到1 DLU的基值。

现在将x宽度乘以4并除以刚刚计算的“宽度”基本单位。对于y高度乘以8并除以 “高度”。

这可以通过MulDiv轻松完成。

答案 1 :(得分:0)

谢谢Ton ......:)

根据您的指导和建议,该代码段将为:

// Summarizing the code-snippet.
RECT    wRect;
GetWindowRect(GetDlgItem(hDlg, kDProxyItem), &wRect);
ScreenToClient (hDlg, (LPPOINT)&wRect);
ScreenToClient (hDlg, (LPPOINT)&(wRect.right));

RECT pixel_rect;
pixel_rect.left = 0;
pixel_rect.top = 0;
pixel_rect.right = 4;
pixel_rect.bottom = 8;
bool b_check = MapDialogRect(hDlg, &pixel_rect);

LONG base_pix_width = pixel_rect.right;
LONG base_pix_height = pixel_rect.bottom;

// Calculating acctual X,Y coordinates with Width - Height of the Proxy Rectangle
RECT proxy_acc_dim;
proxy_acc_dim.left   = (wRect.left * 4 / base_pix_width);
//or we can do the same by: MulDiv(wRect.left, 4, base_pix_width);

proxy_acc_dim.right  = (wRect.right * 4 / base_pix_width) - proxy_acc_dim.left; 
//or we can do the same by: MulDiv(wRect.right, 4,  base_pix_width);

proxy_acc_dim.top    = (wRect.top * 8 / base_pix_height);
//or we can do the same by: MulDiv(wRect.top, 8, base_pix_height);

proxy_acc_dim.bottom = (wRect.bottom * 8 / base_pix_height) - proxy_acc_dim.top;
//or we can do the same by: MulDiv(wRect.bottom, 8, base_pix_height);

proxy_acc_dim.left      = proxy_rect.left;
proxy_acc_dim.top       = proxy_rect.top;
proxy_acc_dim.right     = proxy_rect.right - proxy_rect.left;
proxy_acc_dim.bottom    = proxy_rect.bottom - proxy_rect.top;

工作正常。希望这对其他人有帮助......