024.讲MFC_窗口指针

窗口指针
通过HWND获得CWnd指针        //如何通过窗口句柄获得窗口指针
获得应用程序主窗口的指针      //如何获得应用程序主窗口的指针
一、建立名为dialogPoint的mfc工程,添加两个button 
双击button1
进入dialogPointDlg.cpp
添加
void CdialogPointDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HWND hWnd = GetSafeHwnd(); //获取当前窗口句柄
CWnd *pWnd = CWnd::FromHandle(hWnd); //通过句柄得到指针
CString s;
s.Format(_T("pWnd = 0x%X\nthis = 0x%X"),pWnd,this);
AfxMessageBox(s);
}

同理双击button2
添加
void CdialogPointDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CdialogPointApp *pApp = (CdialogPointApp *)AfxGetApp();//应用程序指针
CWnd *pMainWnd = pApp->m_pMainWnd;
CString s;
s.Format(_T("pMainWnd = 0x%X\nthis = 0x%X"),pMainWnd,this);
AfxMessageBox(s);
}

效果图

猜你喜欢

转载自www.cnblogs.com/Malphite/p/10828926.html