数据库开发之窗体编程

来源:互联网 发布:信息流广告数据分析 编辑:程序博客网 时间:2024/06/07 01:42
转自:http://www.vckbase.com/index.php/wv/788

在很多管理信息系统的窗体都有一些共同点:可以通过框架菜单打开各种类型的视图窗口,而这些视图窗口具有MDI的一些特点,可以在框架窗体中最小化最大化还原等,并且点击菜单只能产生一个窗口(象SDI)。这种形式的窗口设计给人与一种条理感。于是我用VC6.0尝试创建这种窗口,现将整个过程为大家写下来,这里应含有MFC的一些内幕技术。

一、新建AppWizad(exe)工程,名为mdisdi,基于MDI的文档视模式(其余默认)。

二、在CApp子类中添加如下代码:

1.  public:
2.  CMultiDocTemplate* pDoctemp1;
3.  CMultiDocTemplate* pDoctemp2;

三、修改InitInstance()中将如下代码:

1.  CMultiDocTemplate* pDocTemplate;
2.  pDocTemplate =new CMultiDocTemplate(
3.       IDR_MDISDITYPE,
4.       RUNTIME_CLASS(CMdisdiDoc),
5.       RUNTIME_CLASS(CChildFrame),// custom MDI child frame
6.       RUNTIME_CLASS(CMdisdiView));
7.   AddDocTemplate(pDocTemplate);

改为:

1.   //CMultiDocTemplate* pDocTemplate;//删除
2.       pDoctemp1 =new CMultiDocTemplate(
3.       IDR_MDISDITYPE,
4.       RUNTIME_CLASS(CMdisdiDoc),
5.       RUNTIME_CLASS(CChildFrame),// custom MDI child frame
6.       RUNTIME_CLASS(CMdisdiView));
7.     AddDocTemplate(pDoctemp1);

四、加入新类CDoc2,基类为CDocument(利用菜单Insert-New Class…让其自动生成即可);加入新框架类CChildFrame2,基类为CMDIChildWnd;

加入新视类,这里新加CView2基类为CFormView类(Insert-New Form…),注意在添加对话框中将Document选为CDoc2;

五、同(三)将如下代码:

1.  CMultiDocTemplate* pNewDocTemplate =new CMultiDocTemplate(
2.               IDR_VIEW2_TMPL, 
3.       RUNTIME_CLASS(CDoc2),// document class
4.       RUNTIME_CLASS(CMDIChildWnd),// frame class
5.       RUNTIME_CLASS(CView2));// view class
6.  
7.     AddDocTemplate(pNewDocTemplate);

改为:

1.  pDoctemp2 =new CMultiDocTemplate(
2.            IDR_VIEW2_TMPL, 
3.        RUNTIME_CLASS(CDoc2),// document class 
4.          RUNTIME_CLASS(CChildFrame2),// frame class 
5.            RUNTIME_CLASS(CView2));// view class 
6.      AddDocTemplate(pDoctemp2);

并在mdisdi.cpp头部添加:

1.#include "Doc2.h"

六、修改IDR_MAINFRAME菜单,新添一菜单“功能”,其子菜单为窗口1和窗口2.用ctrl+c复制,ctrl+v粘贴,产生二个新菜单IDR_MAINFRAME1和IDR_MAINFRAME2,再将其更名为IDR_MDISDITYPE,IDR_VIEW2_TMPL. 七、为IDR_MAINFRAME菜单新添菜单增加消息应射函数OnMenuitem32771()和OnMenuitem32772() 八、在MainFrm.h加入:

1.  #include"mdisdiDoc.h"
2.  #include"Doc2.h"

并在类中添加:

1.  public:
2.  CMdisdiDoc * pDoc1;
3.  CDoc2 * pDoc2;

在MainFrm.cpp增加代码如下:

01.  voidCMainFrame::OnMenuitem32771() 
02.  {
03.  // TODO: Add your command handler code here
04.        if(pDoc1==NULL)
05.      {
06.        CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp();
07.        pDoc1=(CMdisdiDoc *)
08.        pmdisdiapp->pDoctemp1->OpenDocumentFile(NULL);
09.       }
10.        else
11.      {
12.        POSITION pos;
13.        pos=pDoc1->GetFirstViewPosition();
14.        CView * pView;
15.        pView=pDoc1->GetNextView(pos);
16.        pView->GetParentFrame()->ActivateFrame();
17.      }
18.   }
19.  
20. voidCMainFrame::OnMenuitem32772() 
21.  {
22.        if(pDoc2==NULL)
23.      
24.        CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp();
25.        pDoc2=(CDoc2 *)
26.        pmdisdiapp->pDoctemp2->OpenDocumentFile(NULL);
27.      }
28.       else
29.     {
30.        POSITION pos;
31.        pos=pDoc2->GetFirstViewPosition();
32.        CView * pView;
33.        pView=pDoc2->GetNextView(pos);
34.        pView->GetParentFrame()->ActivateFrame();
35.     }
36.  }

九、在MainFrm.cpp添加如下代码:

1. CMainFrame::CMainFrame()
2.  {
3.  
4.      pDoc1=NULL;
5.      pDoc2=NULL;
6.  }

在ChildFrm.cpp中添加:

1.  #include"MainFrm.h"

和如下代码:

1. CChildFrame::~CChildFrame()
2.  {
3.  CMainFrame * pmainwnd=
4.            (CMainFrame *)AfxGetMainWnd();
5.  pmainwnd->pDoc1=NULL;
6.  }

在ChildFrame2.cpp中添加:

1.  #include"MainFrm.h"

和如下代码:

1. CChildFrame2::~CChildFrame2()
2.  {
3.  CMainFrame * pmainwnd=
4.            (CMainFrame *)AfxGetMainWnd();
5.  pmainwnd->pDoc2=NULL;
6.  }

最后将InitInstance()中如下几行注释掉:

1.  CCommandLineInfo cmdInfo;
2.  ParseCommandLine(cmdInfo);
3.  
4.  // Dispatch commands specified on the command line
5.  if(!ProcessShellCommand(cmdInfo))
6.  returnFALSE;

作用是去掉新建选择视类对话框;



原创粉丝点击