C++ 如何获取对话框的处理程序(HWND)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21643016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get handler(HWND) for dialog box
提问by user3126297
Hi I have created a dialog box and it woks.
嗨,我创建了一个对话框,它可以工作。
My question is: how do you retreive the handle for it?
我的问题是:你如何检索它的句柄?
Also, if you get the handle, how would you change the static text control text inside it?
另外,如果您获得了句柄,您将如何更改其中的静态文本控件文本?
class CStatisticsDlg : public CDialogEx
{
public:
CStatisticsDlg();
// Dialog Data
enum { IDD = IDD_STATISTICS };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
};
CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}
void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
回答by Adam Rosenfield
Assuming you're using MFC (as indicated by the tag), then presumably you have a CDialog
class instance. CDialog
is a subclass of CWnd
, so you can retrieve the window handle by one of 3 ways:
假设您正在使用 MFC(如标记所示),那么您可能有一个CDialog
类实例。 CDialog
是 的子类CWnd
,因此您可以通过以下三种方式之一检索窗口句柄:
- Directly accessing its
m_hWnd
member - Casting it to an
HWND
withoperator HWND()
- Calling
GetSafeHwnd()
on it
- 直接访问其
m_hWnd
成员 - 将其转换为
HWND
withoperator HWND()
- 呼唤
GetSafeHwnd()
它
回答by Michael Haephrati
Here is how to do it. First create a member function to the main application class. Then use the following code (Assuming the class name is CGenericApp, and your Dialog class is CGenericDlg.
这是如何做到的。首先为主应用程序类创建一个成员函数。然后使用以下代码(假设类名是CGenericApp,你的 Dialog 类是CGenericDlg。
CWnd* CGenericApp::GetDlg()
{
return m_pMainWnd;
}
Then when you want to get a handler to the main Dialog box, use:
然后,当您想获得主对话框的处理程序时,请使用:
CGenericApp* app = (CGenericApp*)AfxGetApp();
CGenericDlg* pDlg = (CGenericDlg*)(app->GetDlg());
HWND win = pDlg->GetSafeHwnd();
'win' will hold the HWND you are looking for.
'win' 将保存您正在寻找的 HWND。