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 对话框中的控件的大小和位置?


基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07