[英]How to get handler(HWND) for dialog box
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()
Assuming you're using MFC (as indicated by the tag), then presumably you have a CDialog
class instance.假设您正在使用 MFC(如标记所示),那么您大概有一个CDialog
类实例。 CDialog
is a subclass of CWnd
, so you can retrieve the window handle by one of 3 ways: CDialog
是CWnd
的子类,因此您可以通过以下三种方式之一检索窗口句柄:
m_hWnd
member直接访问其m_hWnd
成员HWND
with operator HWND()
使用operator HWND()
将其转换为HWND
GetSafeHwnd()
on it在其上调用GetSafeHwnd()
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.