MFC单文档如何打开文本文件并将其内容显示在窗口里?

MFC单文档如何打开文本文件并将其内容显示在窗口里?,第1张

函数: BOOL CTT1Doc::OnOpenDocument(LPCTSTR lpszPathName) 中打开。 其中, CTT1Doc是用户的文档类名。

这个函数不是系统自动生成的, 可以通过建立继承函数: virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); 来实现。

亦可以参见自动建立:

首先新建一个对话框资源,初始化程序实例是由InitInstance函数完成的。因此d出这个对话框的代码也是放在这个函数里的。
代码如下:
BOOL CDlgTestApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates Document templates
// serve as the connection between documents, frame windows and views
CLogsys TestDlg;
if(TestDlgDoModal()==IDOK) // 单击Ok后就开始初始化程序实例
{
CSingleDocTemplate pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDlgTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDlgTestView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
else // 假如单击了CANCEL按钮就直接退出
return FALSE;
}
当然不是单击OK就可以进入单文档视图,在单击OK后还要进行检查用户名和密码。因此要在对话框的OnOK函数里添加相应的处理代码。
void CLogsys::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE); // 获取输入数据
if(m_strUser=="Admin"&&m_strPwd=="1234")
{
CDialog::OnOK(); // 假如用户名和密码正确,就关闭对话框
}
/假如用户名或密码错误,且还未超出登陆次数,就进行提示/
if((m_strUser!="Admin"||m_strPwd!="1234")&&(m_Time<3)) //假如密码和用户名正确
{
AfxMessageBox("用户名或密码不正确");
m_Time++;
}
/假如超出登陆次数,提示并退出系统/
if(m_Time>2)
{
AfxMessageBox("登陆错误次数超过3次");
PostQuitMessage(0);
}
}
当然在实际中功能还应进行扩充,比如3次登陆失败后就应限制这台电脑在一定时间内不能登陆等,还有比如如何验证多个用户名进行登陆等等。

我想说的是,MFC处理MDI是很严格的,有一整套处理逻辑,但就楼主的OnTestTest1(),就知道楼主对于MDI是相当陌生的,这种处理就是闭门造车。
推荐一本相当不错的书籍:VC++技术内幕,现在已经是第五版了。楼主系统研究一下,书中对SDI,MDI的介绍。就会知道SDI, MDI原来是这样的。和楼主想的完全不是一回事。
该书虽然很厚,但其实对于SDI和MDI也仅仅是起到了穿针引线的作用,并不是百科全书,研究的时候还需要打开MSDN对一些类,对一些函数的说明,也算作是对VC++技术内幕解释的一个补充说明。
如果是手上的工作的话,那没办法了,肯定是要尽快先解决这个项目。系统学习只能慢慢来。如果楼主只是自己在尝试代码的话,强烈建议,不要再做下去了。MDI有很多内容,它是一个网状结构,往往改造一个默认功能,需要在好几个重载函数里进行加工。不是像楼主想的那样,加个菜单处理函数,模拟菜单单击,给FileNew发送个WM_COMMAND。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13400613.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存