MFC framework interoperability mechanism record

Haha, I finally found an introduction in this area today. I believe that many mfc novices feel that mfc is not as easy to use as winform, and it is not as direct as winform, and the message mapping is too complicated to master. When you read this introduction to interoperability, you will think MFC is very cute, and even fall in love with his message mapping mechanism. Of course, you must first understand the meaning of pointers.
From here to write the linked content
presented here is very important. Thank him!

//这个是在view类窗口中添加鼠标移动事件消息,通过这个事件在状态栏显示鼠标的x,y
//同时将这个字符输出到mfc默认生成的输出窗口;
void CmfcktView::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值

    CString str;
    str.Format("X=%d,Y=%d",point.x,point.y);//获取鼠标点的x,y
    //获取父框架指针并使用SetMessageText(str)设置状态栏的信息
    ((CFrameWnd*)GetParent())->SetMessageText(str); 

   //获取应用程序的CMainFrame* pMainFrame指针然后就在view类中操作了CMainFrame类中的内容,CMainFrame中有涵盖了许多框架窗口的成员,所以获得了操作这些类的权利,这样是不是好复杂又好灵活!mfc开始喜欢你了!
    CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd); 
    pMainFrame->Addmesg(str);

    CView::OnMouseMove(nFlags, point);

}

I will excerpt and comment here and spread it out, thanks to the original blogger:
1. Get the application pointer
CMyApp* pApp=(CMyApp*)AfxGetApp();

2. Get
the public member variable m_pMainWnd in the main frame pointer CWinApp is the pointer of the main frame
CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
or
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
// After obtaining these pointers, you can call functions in another class through the message mechanism in other classes.
3. Get the menu pointer
CMenu* pMenu = AfxGetMainWnd()->GetMenu();

4, obtaining a toolbar, status bar pointer
in the main frame can be used directly m_wndToolBar, m_wndStatusBar
Other:

CToolBar* pToolBar = (CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

5, the pointer gets control
to use the GetDlgItem () and then converted, such as:

CButton* pButton = (CButton*)GetDlgItem(IDC_MYBUTTON);

6. Get the document and view pointer through the frame

SDI:
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();
CYourView* pView = (CYourView*)pMainFrame->GetActiveView();

MDI:
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();
CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument();
CYourView* pView = (CYourView*)pChildFrame->GetActiveView();

7, documents, views

Get the document pointer from the view:
CYourDoc* pDoc = GetDocument();

Get the view pointer from the document:
Use the member functions GetFirstViewPosition() and GetNextView() to traverse the
virtual POSITION GetFirstViewPosition() const;
virtual CView* GetNextView(POSITION& rPosition) const;

SDI:
CYourView* pView;
POSITION pos = GetFirstViewPosition();
pView = GetNextView(pos);

MDI:

Define function
CView* CYourDoc::GetView(CRuntimeClass* pClass)

{

CView* pView;
POSITION pos=GetFirstViewPosition();
while(pos!=NULL)
{
pView=GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
}
if(!pView->IsKindOf(pClass))
{
AfxMessageBox(“Connt Locate the View.”);
return NULL;
}
return pView;
}

Use as follows:
CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

8. Document templates and documents

Get the document template pointer from the document:
CDocTemplate* GetDocTemplate() const;

Obtain the document pointer from the document template:
viaual POSITION GetFirstDocPosition() const = 0;
visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

9. Get the pointers of each view in the split view

Defined in the main frame: CSplitterWnd m_wndSplitter;

Define two View classes: CView1, CView2

Overload in the frame class:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)
{
VERIFY(m_splitter.CreateStatic(this,2,1)); //Split into two rows and one column
VERIFY(m_splitter.CreateView(0,0, RUNTIME_CLASS(CView1),CSize(100,100),pContext));
VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext));
return TRUE;
}

Get the split view pointer
CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);
CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

10. Get the child window pointer through the mouse

* ChildWindowFromPoint the CWnd (the POINT Point) const;
the CWnd * ChildWindowFromPoint (the POINT Point, UINT nFlags) const;
a sub-window contains the specified point to determine
if the specified point is outside the client area, the function returns NULL;
if the specified point in the client area , But does not belong to any child window, the function returns the pointer of the CWnd;
if there are multiple child windows containing the specified point, it returns the pointer of the first child window.
Also note that this function returns a pseudo-window pointer, which cannot be saved for later use.
The second parameter nFlags has several meanings:
CWP_ALL file://Do not ignore any child windows
CWP_SKIPNIVSIBLE file://Ignore invisible child windows
CWP_SKIPDISABLED file://Ignore prohibited child windows
CWP_SKIPRANSPARENT file://Ignore transparent child windows

Guess you like

Origin blog.csdn.net/sun19890716/article/details/51151541