用*this可以获得HWND,经调试得知,运行到的是操作符()函数.等价于:
CWND wnd;
HWND hWnd = (wnd);
AFXWIN_INLINE CWnd::operator HWND() const
{ return this == NULL ? NULL : m_hWnd; }
_AFXWIN_INLINE BOOL CWnd::operator==(const CWnd& wnd) const
{ return ((HWND) wnd) == m_hWnd; }
_AFXWIN_INLINE BOOL CWnd::operator!=(const CWnd& wnd) const
{ return ((HWND) wnd) != m_hWnd; }
_AFXWIN_INLINE HWND CWnd::GetSafeHwnd() const
{ return this == NULL ? NULL : m_hWnd; }
1.CWnd
对于基于CWnd的类,如对话框
比如CXXXDialog
在对话框内部那就是
(CWnd*)this ;
如果定义的
CXXXDialog dlg ;
就是 (CWnd*)&dlg ;
如果是对话框的控件如IDC_EDIT1
CWnd *pEdit = GetDlgItem( IDC_EDIT1 ) ;
2.HWND
例如:
在CFormView或者CDialog内部:
HWND hWnd = GetSafeHwnd();
否则:
CFormView *pView = ......;
HWND hWndFormView = pView->GetSafeHwnd();
CDialog *pDlg = ......
HWND hWndDlg = pDlg->GetSafeHwnd();