|
如何通过代码获得应用程序主窗口的指针
在MDI或者SDI应用程序中获得了主框架指针就比较容易获得其他窗口指针或者其他有用信息,如果可以在任何地方都能轻松获得它的话将是非常有意义的.
通过分析CWinApp类,发现在InitInstance函数中已经将程序的主窗口指针赋值为主框架窗口的指针.而在程序的任何地方都可以通过函数AfxGetApp()获得唯一的应用程序指针,代码如下:
CWnd pMainWnd=AfxGetApp()->m_pMainWnd; CMainFrame*
pMainFrame=(CMainFrame*)pMainWnd;
CRect rc; CWnd*
pParent=AfxGetApp()->GetMainWnd(); pParent->GetWindowRect(&rc);
主窗口的
指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现。 AfxGetMainWnd()
->ShowWindow(SW_SHOWMAXMIZED) //使程序最大化.
例子如下: CRect
rc; CWnd*
pParent=AfxGetApp()->GetMainWnd(); pParent->GetWindowRect(&rc); pParent->MoveWindow(rc.left,rc.top,rc.Width()+1,rc.Height()+1,TRUE);
|