MFC框架互操作机制记录

来源:互联网 发布:arm c语言编程实例 编辑:程序博客网 时间:2024/05/21 09:04

呵呵,今天终于找到这方面的介绍了。相信许多mfc的小白都觉得mfc不如winform好用,不如winform直接,消息映射太复杂不好掌握。看会这个互操作介绍时候你会觉得mfc很可爱,甚至会爱上他的消息映射机制,当然你要先理解指针的含义。
来自这里写链接内容
这里的介绍很是重要。谢谢他!

//这个是在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);}

我这里就摘抄注释一下扩散一下,感谢原创博主:
1、获取应用程序指针
CMyApp* pApp=(CMyApp*)AfxGetApp();

2、获取主框架指针
CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针
CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
或者
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
//获得这些指针后你就可以在其他类中通过消息机制调用另一个类中的函数。
3、获取菜单指针
CMenu* pMenu = AfxGetMainWnd()->GetMenu();

4、获取工具栏、状态栏指针
主框架中可以直接使用m_wndToolBar、m_wndStatusBar
其他:

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

5、获取控件指针
先用 GetDlgItem() 再转换,如:

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

6、通过框架获取文档、视图指针

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、文档、视图

从视图获取文档指针:
CYourDoc* pDoc = GetDocument();

从文档获取视图指针:
利用成员函数 GetFirstViewPosition() 和 GetNextView() 遍历
virtual POSITION GetFirstViewPosition() const;
virtual CView* GetNextView(POSITION& rPosition) const;

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

MDI:

定义函数
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;
}

使用如下:
CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

8、文档模版、文档

从文档获取文档模版指针:
CDocTemplate* GetDocTemplate() const;

从文档模版获取文档指针:
viaual POSITION GetFirstDocPosition( ) const = 0;
visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

9、获取分割视图中各个视图的指针

主框架中定义:CSplitterWnd m_wndSplitter;

定义两个View类:CView1、CView2

框架类中重载:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)
{
VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成两行一列
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;
}

获取分割视图指针
CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);
CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

10、通过鼠标获得子窗口指针

CWnd* ChildWindowFromPoint(POINT point) const;
CWnd* ChildWindowFromPoint(POINT point,UINT nFlags) const;
用于确定包含指定点的子窗口
如果指定点在客户区之外,函数返回NULL;
如果指定点在客户区内,但是不属于任何一个子窗口,函数返回该CWnd的指针;
如果有多个子窗口包含指定点,则返回第一个子窗口的指针。
还要注意的是,该函数返回的是一个伪窗口指针,不能将它保存起来供以后使用。
对于第二个参数nFlags有几个含义:
CWP_ALL file://不忽略任何子窗口
CWP_SKIPNIVSIBLE file://忽略不可见子窗口
CWP_SKIPDISABLED file://忽略禁止的子窗口
CWP_SKIPRANSPARENT file://忽略透明子窗口

0 0
原创粉丝点击