数据库开发之窗体编程
作者:李强
在很多管理信息系统的窗体都有一些共同点:可以通过框架菜单打开各种类型的视图窗口,而这些视图窗口具有MDI的一些特点,可以在框架窗体中最小化最大化还原等,并且点击菜单只能产生一个窗口(象SDI)。这种形式的窗口设计给人与一种条理感。于是我用VC6.0尝试创建这种窗口,现将整个过程为大家写下来,这里应含有MFC的一些内幕技术。
一、新建AppWizad(exe)工程,名为mdisdi,基于MDI的文档视模式(其余默认)。
二、在CApp子类中添加如下代码:
public:
CMultiDocTemplate* pDoctemp1;
CMultiDocTemplate* pDoctemp2;
三、修改InitInstance()中将如下代码:
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_MDISDITYPE,
RUNTIME_CLASS(CMdisdiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CMdisdiView));
AddDocTemplate(pDocTemplate);
改为:
//CMultiDocTemplate* pDocTemplate;//删除
pDoctemp1 = new CMultiDocTemplate(
IDR_MDISDITYPE,
RUNTIME_CLASS(CMdisdiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CMdisdiView));
AddDocTemplate(pDoctemp1);
四、加入新类CDoc2,基类为CDocument(利用菜单Insert-New Class…让其自动生成即可);加入新框架类CChildFrame2,基类为CMDIChildWnd;
加入新视类,这里新加CView2基类为CFormView类(Insert-New Form…),注意在添加对话框中将Document选为CDoc2;
五、同(三)将如下代码:
CMultiDocTemplate* pNewDocTemplate = new CMultiDocTemplate(
IDR_VIEW2_TMPL,
RUNTIME_CLASS(CDoc2), // document class
RUNTIME_CLASS(CMDIChildWnd), // frame class
RUNTIME_CLASS(CView2)); // view class
AddDocTemplate(pNewDocTemplate);
改为:
pDoctemp2 = new CMultiDocTemplate(
IDR_VIEW2_TMPL,
RUNTIME_CLASS(CDoc2), // document class
RUNTIME_CLASS(CChildFrame2), // frame class
RUNTIME_CLASS(CView2)); // view class
AddDocTemplate(pDoctemp2);
并在mdisdi.cpp头部添加:
#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加入:
#include "mdisdiDoc.h"
#include "Doc2.h"
并在类中添加:
public:
CMdisdiDoc * pDoc1;
CDoc2 * pDoc2;
在MainFrm.cpp增加代码如下:
void CMainFrame::OnMenuitem32771()
{
// TODO: Add your command handler code here
if(pDoc1==NULL)
{
CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp();
pDoc1=(CMdisdiDoc *)
pmdisdiapp->pDoctemp1->OpenDocumentFile(NULL);
}
else
{
POSITION pos;
pos=pDoc1->GetFirstViewPosition();
CView * pView;
pView=pDoc1->GetNextView(pos);
pView->GetParentFrame()->ActivateFrame();
}
}
void CMainFrame::OnMenuitem32772()
{
if(pDoc2==NULL)
{
CMdisdiApp * pmdisdiapp =(CMdisdiApp *)AfxGetApp();
pDoc2=(CDoc2 *)
pmdisdiapp->pDoctemp2->OpenDocumentFile(NULL);
}
else
{
POSITION pos;
pos=pDoc2->GetFirstViewPosition();
CView * pView;
pView=pDoc2->GetNextView(pos);
pView->GetParentFrame()->ActivateFrame();
}
}
九、在MainFrm.cpp添加如下代码:
CMainFrame::CMainFrame()
{
pDoc1=NULL;
pDoc2=NULL;
}
在ChildFrm.cpp中添加:
#include "MainFrm.h"
和如下代码:
CChildFrame::~CChildFrame()
{
CMainFrame * pmainwnd=
(CMainFrame *)AfxGetMainWnd();
pmainwnd->pDoc1=NULL;
}
在ChildFrame2.cpp中添加:
#include "MainFrm.h"
和如下代码:
CChildFrame2::~CChildFrame2()
{
CMainFrame * pmainwnd=
(CMainFrame *)AfxGetMainWnd();
pmainwnd->pDoc2=NULL;
}
最后将InitInstance()中如下几行注释掉:
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
作用是去掉新建选择视类对话框;