Retrieves a pointer to the specified control or child window in a dialog box or other window.
2025-11-02
Written by: xiaobin

Given a handle to any one of these objects, you can find the MFC object that wraps the handle by calling the static method FromHandle. For example, given an HWND called hWnd, the following line will return a pointer to the CWnd that wraps hWnd:
CWnd::FromHandle(hWnd)
If hWnd does not have a specific wrapper object, a temporary CWnd is created to wrap hWnd. This makes it possible to obtain a valid C++ object from any handle.
After you have a wrapper object, you can retrieve its handle from a public member variable of the wrapper class. In the case of a CWnd, m_hWnd contains the HWND for that object.
CWnd* GetDlgItem(int nID) const;
void GetDlgItem(
int nID,
HWND* phWnd) const;
A pointer to the given control or child window. If no control with the integer ID given by the nID parameter exists, the value is NULL.
The returned pointer may be temporary and shouldn’t be stored for later use.
The pointer returned is usually cast to the type of control identified by nID.
// uses GetDlgItem to return a pointer to a user interface control
CEdit *pBoxOne;
pBoxOne = (CEdit*)GetDlgItem(IDC_MYEDIT);
GotoDlgCtrl(pBoxOne);