相信不少人進行資料庫編程都有這樣的問題,如何設定一個登陸框,通過登陸框來進入單文檔視圖。我看到很多資料庫編程方面的書,都是對話方塊之間的相互切換。而在對話方塊中添加菜單不少人都不太熟悉(當然這是可以辦到的)。我在想:為何不能在對話方塊中彈出單文檔,這樣添加菜單等工作就方便多了。為此我幾經探索,終於實現了如何從一個對話方塊彈出單文檔視圖。
下面我以一個登陸對話方塊為例來說明如何從一個對話方塊彈出單文檔視圖。
首先建立一個對話方塊資源,如:
熟悉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次登陸失敗後就應限制這台電腦在一定時間內不能登陸等,還有比如如何驗證多個使用者名稱進行登陸等等。
原文出處:http://ladymaidu.iteye.com/blog/1263336