How to get size and location of a control placed on a dialog in MFC?(如何获取放置在 MFC 对话框中的控件的大小和位置?)
问题描述
我有指向带有函数的控件的指针
I've got the pointer to the control with function
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 对话框中的控件的大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取放置在 MFC 对话框中的控件的大小和位置?
基础教程推荐
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
