如何從一個對話方塊彈出單文檔視圖

來源:互聯網
上載者:User

相信不少人進行資料庫編程都有這樣的問題,如何設定一個登陸框,通過登陸框來進入單文檔視圖。我看到很多資料庫編程方面的書,都是對話方塊之間的相互切換。而在對話方塊中添加菜單不少人都不太熟悉(當然這是可以辦到的)。我在想:為何不能在對話方塊中彈出單文檔,這樣添加菜單等工作就方便多了。為此我幾經探索,終於實現了如何從一個對話方塊彈出單文檔視圖。

下面我以一個登陸對話方塊為例來說明如何從一個對話方塊彈出單文檔視圖。

首先建立一個對話方塊資源,如:

熟悉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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.