user3126297
user3126297

Reputation: 169

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()

Upvotes: 5

Views: 30189

Answers (2)

Michael Haephrati
Michael Haephrati

Reputation: 4225

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.

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.

Upvotes: 0

Adam Rosenfield
Adam Rosenfield

Reputation: 400334

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:

Upvotes: 11

Related Questions