如何從一個對話框彈出單文檔視圖
????????????????????????????????????????????????????????????????????????????????????????????? 朱金燦????????
相信不少人進行數據庫編程都有這樣的問題,如何設置一個登陸框,通過登陸框來進入單文檔視圖。我看到很多數據庫編程方面的書,都是對話框之間的相互切換。而在對話框中添加菜單不少人都不太熟悉(當然這是可以辦到的)。我在想:為何不能在對話框中彈出單文檔,這樣添加菜單等工作就方便多了。為此我幾經探索,終于實現了如何從一個對話框彈出單文檔視圖。
?
下面我以一個登陸對話框為例來說明如何從一個對話框彈出單文檔視圖。
首先新建一個對話框資源,如下圖:
?
熟悉MFC編程的朋友都知道初始化程序實例是由InitInstance函數完成的。因此彈出這個對話框的代碼也是放在這個函數里的。
代碼如下:
?
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(TestDlg.DoModal()==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次登陸失敗后就應限制這臺電腦在一定時間內不能登陸等,還有比如如何驗證多個用戶名進行登陸等等。