如何获取控件放置在MFC对话框中的大小和位置? [英] How to get size and location of a control placed on a dialog in MFC?

查看:466
本文介绍了如何获取控件放置在MFC对话框中的大小和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有指向控件的指针

CWnd* CWnd::GetDlgItem(int ITEM_ID)

所以我有 CWnd * 到控制,
,但根本找不到 CWnd 类中的任何方法,将
检索给定控件的大小和位置。
任何帮助?

so i've got CWnd* pointer which points to the control, but simply can't find any method within CWnd class that will retrieve the size and location of a given control. Any help?

推荐答案

CRect rect;
CWnd *pWnd = pDlg->GetDlgItem(YOUR_CONTROL_ID);
pWnd->GetWindowRect(&rect);
pDlg->ScreenToClient(&rect); //optional step - see below

//position:  rect.left, rect.top
//size: rect.Width(), rect.Height()

GetWindowRect 给出控件的屏幕坐标。 pDlg-> ScreenToClient 会将它们转换为相对于对话框的客户区域,这通常是您需要的。

GetWindowRect gives the screen coordinates of the control. pDlg->ScreenToClient will then convert them be relative to the dialog's client area, which is usually what you need.

注意: pDlg 上面是对话框。如果你是对话框类的成员函数,只需删除 pDlg->

Note: pDlg above is the dialog. If you're in a member function of the dialog class, just remove the pDlg->.

这篇关于如何获取控件放置在MFC对话框中的大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆