MFC 获取对话框的指针

对于MFC的对话框,要对其操作必须先获取到它的对象指针,下面是对对话框的对象指针的获取方式的解读
1.获取主窗口的对话框的对象指针

CWnd* pWnd = AfxGetApp()->GetMainWnd();

2.获取指定对话框的对象指针

CWnd* pWnd = FindWindow(NULL, _T("对话框的名称"));

3.ID , 指针, 句柄三者的转化

ID--HANDLE--HWND三者之间的互相转换 
id->句柄-----------hWnd = ::GetDlgItem(hParentWnd,id); 
id->指针-----------CWnd::GetDlgItem(); 
句柄->id-----------id = GetWindowLong(hWnd,GWL_ID); 
句柄->指针--------CWnd *pWnd=CWnd::FromHandle(hWnd); 
指针->ID----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID); 
GetDlgCtrlID(); 
指针->句柄--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;
HICON->ID--------HICON hIcon = AfxGetApp()->LoadIcon(nIconID);
HICON hIcon = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nIconID));

附注:获得窗体句柄三种方法

1.HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName)
2. HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter,LPCTSTR lpClassName, LPCTSTR lpWindowName)
3.HWND WindowFromPoint(POINT& Point)//获得当前鼠标光标位置的窗体HWND

你可能感兴趣的:(MFC 获取对话框的指针)