本文介紹如何建立以及如何插入初始螢幕,在 Visual C++.NET 中或 Visual C++2005 中。 在 Microsoft Visual Studio 6.0 您可能會在使用 Visual C++ 元件庫将 MFC 單文檔界面 (SDI) 應用程式中或多文檔界面 (MDI) 應用程式的初始螢幕。 因為在 Visual C++.NET 中或 Visual C++2005 建立初始螢幕不直接方式使用應用程式向導生成預設 MDI 應用程式或預設 SDI 應用程式,然後中添加從 CDialog (是例如 CSplashDlg) 派生的類。 修改此代碼,以将初始螢幕。
建立并插入初始螢幕的 SDI 中的 MDI 應用程式
建立一個預設 SDI 或 MDI 應用程式
- 啟動 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
- 在 檔案 菜單上指向 建立 ,然後單擊 項目 。
-
單擊 項目類型 下的 Visual C++ 項目 ,然後單擊 模闆 下的 MFC 應用程式 。
請注意 在 Visual 的 Studio 2005 Visual C++ 項目 将被更改為 Visual C++ 。
- MyApp ,該項目命名,然後單擊 确定 。
- 在 MFC 應用程式向導中,單擊 完成 。
建立資源編輯器中的一個對話框
- 在 視圖 菜單中上, 單擊 資源視圖 。
- 在資源視圖中展開 MyApp 、 MyApp.rc ,和右鍵單擊 。
- 單擊 插入 。 情況預設,建立 IDD_DIALOG1。
- 删除 确定 按鈕和 取消 按鈕。 為此,右鍵單擊每個按鈕,,然後單擊 删除 。
- 在屬性窗格在 雜項 ,下輕按兩下 ID 。
- 在 ID 屬性中單擊 IDD_DIALOG1 ,然後鍵入 IDD_SPLASH 。
- 外觀 下, 單擊 标題欄 ,然後單擊 False 下拉清單框中。
- 單擊 邊框 ,然後單擊 Thin 下拉清單框中。
将文本添加到您的初始螢幕
- 從工具箱, 對話框編輯器 頁籤的 靜态文本 控件拖放到對話框中。
- 在屬性窗格中單擊 标題 ,然後鍵入标題的新名稱。
建立新 CDialog 派生類
- 輕按兩下對話框框。 在 MFC 類向導 對話框。
- 在 類名稱 文本框中,鍵入 CSplashDlg ,然後單擊 CDialog 從 基礎類 下拉清單框。
- 單擊 完成 。 兩個檔案 (SplashDlg.cpp) 和 (SplashDlg.h) 添加到您的項目。
向項目中添加函數
- 在 視圖 菜單中上, 單擊 類視圖 。
- 在類視圖窗格中展開 MyAPP ,右鍵單擊 CSplashDlg ,指向 添加 ,然後再單擊 添加函數 。
- 在添加成員函數向導,按照下列步驟添加以下三個函數:
- ShowSplashScreen(CWnd* pParentWnd) : 用于顯示初始螢幕的方法。
- 在 傳回類型 清單中,單擊 void。
- 在 函數名稱 文本框中,鍵入 ShowSplashScreen 。
- 在 參數 文本框中,鍵入 CWnd * pParentWnd 。
- 單擊以選中 靜态 複選框。
- HideSplashScreen() : 方法,用以銷毀初始螢幕。
- 在 傳回類型 清單中,單擊 void。
- 在 函數名稱 文本框中,鍵入 HideSplashScreen 。
- PreTranslateAppMessage(MSG* pMsg) : 若要隐藏初始螢幕,每當收到鍵盤或滑鼠消息所使用的靜态方法。
- 在 傳回類型 清單中,單擊 BOOL 。
- 在 函數名稱 文本框中,鍵入 PreTranslateAppMessage 。
- 在 參數 文本框中,鍵入 MSG * pMsg 。
- 單擊以選中 靜态 複選框。
- ShowSplashScreen(CWnd* pParentWnd) : 用于顯示初始螢幕的方法。
- 若要修改在步驟 3 中建立的三種方法使用以下代碼:
void CSplashDlg::ShowSplashScreen(CWnd* pParentWnd) { // Allocate a new splash screen, and create the window. c_pSplashDlg = new CSplashDlg; if (!c_pSplashDlg->Create(CSplashDlg::IDD, pParentWnd)) delete c_pSplashDlg; else c_pSplashDlg->ShowWindow(SW_SHOW); c_pSplashDlg->UpdateWindow(); c_pSplashDlg->SetTimer(1,2000, NULL); } void CSplashDlg::HideSplashScreen() { // Destroy the window, and update the mainframe. c_pSplashDlg->KillTimer(1); DestroyWindow(); AfxGetMainWnd()->UpdateWindow(); delete c_pSplashDlg; c_pSplashDlg = NULL; } BOOL CSplashDlg::PreTranslateAppMessage(MSG* pMsg) { if (c_pSplashDlg == NULL) return FALSE; // If you receive a keyboard or a mouse message, hide the splash screen. if (c_pSplashDlg->m_hWnd != NULL && pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN || pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_RBUTTONDOWN || pMsg->message == WM_MBUTTONDOWN || pMsg->message == WM_NCLBUTTONDOWN || pMsg->message == WM_NCRBUTTONDOWN || pMsg->message == WM_NCMBUTTONDOWN) { c_pSplashDlg->HideSplashScreen(); return TRUE; // message handled here } return FALSE; // message not handled }
添加 OnTimer 事件
- 在類視圖右鍵單擊 CSplashDlg ,然後單擊 屬性 。
- 在屬性窗格中單擊 郵件 圖示,以檢視郵件。
- 單擊 WM _ TIMER ,然後單擊 <add> OnTimer.
- 使用下面的代碼如下所示修改 OnTimer 方法:
HideSplashScreen();}void CSplashDlg::OnTimer(UINT nIDEvent) { // Destroy the splash screen window. HideSplashScreen(); }
重寫 CSplashDlg 類的 OnInitDialog 方法
- 在 視圖 菜單中上, 單擊 類視圖 。
- 在類視圖右鍵單擊 CSplashDlg ,然後單擊 屬性 。
- 在屬性窗格中單擊 覆寫 圖示以顯示清單的替代方法。
- 單擊 OnInitDialog ,然後單擊 <add> OnInitDialog在下拉清單框中。
- 使用以下代碼以修改 OnInitDialog 覆寫剛建立的方法:
BOOL CSplashDlg::OnInitDialog() { CDialog::OnInitDialog(); CenterWindow(); SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); return TRUE; // return TRUE unless you set the focus to a control }
顯示初始螢幕
要檢視,啟動畫面,請按照下列步驟操作:
- 打開應用程式源代碼檔案 MyApp.cpp,,然後執行以下步驟:
- 在該 InitInstance() 方法的行"pMainFrame > ShowWindow(m_nCmdShow),"之前添加下面的代碼:
請注意 在 SDI 的應用程式添加此代碼,下面的行之前:"m_pMainWnd ShowWindow(SW_SHOW) >"。CSplashDlg::ShowSplashScreen(NULL);
- 重寫 PreTranslateMessage 方法 CMyAppApp 的類。 要這樣做,請按下列步驟操作:
- 在類視圖右鍵單擊 CMyAppApp ,然後單擊 屬性 。
- 在屬性窗格中單擊 覆寫 圖示以顯示清單的替代方法。
- 在 PreTranslateMessage 域中,單擊 <add> PreTranslateMessage在下拉清單框中。
- 使用以下代碼以修改 PreTranslateMessage 覆寫剛建立的方法:
如果 (CSplashDlg::PreTranslateAppMessage (pMsg)) 傳回 TRUE ; 傳回 CWinApp::PreTranslateMessage(pMsg);}BOOL CMyAppApp::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here, and call the base class, or both. if(CSplashDlg::PreTranslateAppMessage (pMsg)) return TRUE; return CWinApp::PreTranslateMessage(pMsg); }
- 在該 InitInstance() 方法的行"pMainFrame > ShowWindow(m_nCmdShow),"之前添加下面的代碼:
- 通過在 SplashDlg.h 檔案添加下面一行聲明 c_pSplashDlg 靜态變量:
static CSplashDlg* c_pSplashDlg;
- 在應用程式源代碼檔案 MyApp.cpp 的開頭添加以下語句:
#include "SplashDlg.h" CSplashDlg* CSplashDlg::c_pSplashDlg; //This is required ( in one of the files ) because the static variable 'c_pSplashDlg' is declared outside this file
- 編譯,然後運作應用程式。 您可能會看到的顯示初始螢幕。
轉載位址:http://hi.baidu.com/%B7%A2%B9%E2%B5%C4%B7%BF%D7%D3/blog/item/fe8c3acaee385d81c81768e8.html