vs2008 添加启动画面(基于对话框)

来源:互联网 发布:游族网络待遇怎么样 编辑:程序博客网 时间:2024/06/05 18:39

1. 手动添加CSplashWnd类

SplashWnd.h

[cpp] view plaincopy
  1. #ifndef _SPLASH_HEADER_   
  2. #define _SPLASH_HEADER_  
  3.   
  4. #pragma once  
  5. //#include "afxwin.h"  
  6.   
  7. // CSplashWnd  
  8.   
  9. class CSplashWnd : public CWnd  
  10. {  
  11.     //DECLARE_DYNAMIC(CSplashWnd)  
  12.   
  13. public:  
  14.     CSplashWnd();  
  15.     virtual ~CSplashWnd();  
  16.     virtual void PostNcDestroy();  
  17.   
  18. public:  
  19.     CBitmap m_bitmap;  
  20.   
  21. protected:  
  22.     static BOOL c_bShowSplashWnd;  
  23.     static CSplashWnd* c_pSplashWnd;  
  24.   
  25. public:  
  26.     static void EnableSplashScreen(BOOL bEnable = TRUE);  
  27.     static void ShowSplashScreen(CWnd* pParentWnd = NULL);  
  28.     static BOOL PreTranslateAppMessage(MSG* pMsg);  
  29.   
  30. protected:  
  31.     BOOL Create(CWnd* pParentWnd = NULL);  
  32.     void HideSplashScreen();  
  33.     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);  
  34.     afx_msg void OnPaint();  
  35.     afx_msg void OnTimer(UINT nIDEvent);  
  36.   
  37. protected:  
  38.     DECLARE_MESSAGE_MAP()  
  39. };  
  40. #endif   

SplashWnd.cpp

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include "resource.h"  
  3. #include "SplashWnd.h"  
  4.   
  5.   
  6. // CSplashWnd  
  7.   
  8. //IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)  
  9.   
  10. BOOL CSplashWnd::c_bShowSplashWnd = FALSE;  
  11. CSplashWnd* CSplashWnd::c_pSplashWnd;  
  12.   
  13. CSplashWnd::CSplashWnd()  
  14. {  
  15.   
  16. }  
  17.   
  18. CSplashWnd::~CSplashWnd()  
  19. {  
  20.     // Clear the static window pointer.  
  21.     ASSERT(c_pSplashWnd == this);  
  22.     c_pSplashWnd = NULL;  
  23. }  
  24.   
  25.   
  26. BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)  
  27.     ON_WM_CREATE()  
  28.     ON_WM_PAINT()  
  29.     ON_WM_TIMER()  
  30. END_MESSAGE_MAP()  
  31.   
  32.   
  33. void CSplashWnd::EnableSplashScreen(BOOL bEnable )  
  34. {  
  35.     c_bShowSplashWnd = bEnable;  
  36. }  
  37.   
  38. void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd )  
  39. {  
  40.     if (!c_bShowSplashWnd || c_pSplashWnd != NULL)  
  41.         return;  
  42.     // Allocate a new splash screen, and create the window.  
  43.     c_pSplashWnd = new CSplashWnd;  
  44.     if (!c_pSplashWnd->Create(pParentWnd))  
  45.         delete c_pSplashWnd;  
  46.     else  
  47.         c_pSplashWnd->UpdateWindow();  
  48. }  
  49.   
  50. BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)  
  51. {  
  52.     if (c_pSplashWnd == NULL)  
  53.         return FALSE;  
  54.   
  55.     // If we get a keyboard or mouse message, hide the splash screen.  
  56.     if (pMsg->message == WM_KEYDOWN ||  
  57.         pMsg->message == WM_SYSKEYDOWN ||  
  58.         pMsg->message == WM_LBUTTONDOWN ||  
  59.         pMsg->message == WM_RBUTTONDOWN ||  
  60.         pMsg->message == WM_MBUTTONDOWN ||  
  61.         pMsg->message == WM_NCLBUTTONDOWN ||  
  62.         pMsg->message == WM_NCRBUTTONDOWN ||  
  63.         pMsg->message == WM_NCMBUTTONDOWN)  
  64.     {  
  65.         c_pSplashWnd->HideSplashScreen();  
  66.         return TRUE; // message handled here  
  67.     }  
  68.   
  69.     return FALSE; // message not handled  
  70. }  
  71.   
  72. BOOL CSplashWnd::Create(CWnd* pParentWnd )  
  73. {  
  74.     if (!m_bitmap.LoadBitmap(IDB_SPLASH)) //<span style="font-family: Arial, Helvetica, sans-serif;">IDB_SPLASH 为要启动的画面</span>  
  75.   
  76.         return FALSE;  
  77.   
  78.     BITMAP bm;  
  79.     m_bitmap.GetBitmap(&bm);  
  80.   
  81.     return CreateEx(0,  
  82.         AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),  
  83.         NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);  
  84. }  
  85.   
  86.   
  87. void CSplashWnd::HideSplashScreen()  
  88. {  
  89.     // Destroy the window, and update the mainframe.  
  90.     DestroyWindow();  
  91.     AfxGetMainWnd()->UpdateWindow();  
  92. }  
  93.   
  94. void CSplashWnd::PostNcDestroy()  
  95. {  
  96.     // Free the C++ class.  
  97.     delete this;  
  98. }  
  99.   
  100. int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)  
  101. {  
  102.     if (CWnd::OnCreate(lpCreateStruct) == -1)  
  103.         return -1;  
  104.   
  105.     // Center the window.  
  106.     CenterWindow();  
  107.   
  108.     // Set a timer to destroy the splash screen.  
  109.     SetTimer(1, 1000, NULL);  
  110.   
  111.     return 0;  
  112. }  
  113.   
  114. void CSplashWnd::OnPaint()  
  115. {  
  116.     CPaintDC dc(this);  
  117.   
  118.     CDC dcImage;  
  119.     if (!dcImage.CreateCompatibleDC(&dc))  
  120.         return;  
  121.   
  122.     BITMAP bm;  
  123.     m_bitmap.GetBitmap(&bm);  
  124.   
  125.     // Paint the image.  
  126.     CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);  
  127.     dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);  
  128.     dcImage.SelectObject(pOldBitmap);  
  129. }  
  130.   
  131. void CSplashWnd::OnTimer(UINT nIDEvent)  
  132. {  
  133.     // Destroy the splash screen window.  
  134.     HideSplashScreen();  
  135. }   

2. 为CSplashWnd类 的Oncreate 函数添加如下代码 //WM_CREATE

[cpp] view plaincopy
  1. CenterWindow();  
  2.   
  3. // Set a timer to destroy the splash screen.  
  4. SetTimer(1, 1000, NULL);//1000 表画面停止时间  

3. 为CSplashWnd类 的OnTimer函数添加如下代码//WM_TIMER

[cpp] view plaincopy
  1. HideSplashScreen();  

4. 为CNetTesterDlg的 Oncreate函数添加如下代码//WM_CREATE

[cpp] view plaincopy
  1.     CenterWindow();  
  2.     CSplashWnd::ShowSplashScreen(this);  
  3.   
  4.     if (CDialog::OnCreate(lpCreateStruct) == -1)  
  5.         return -1;  
  6.   
  7.     // TODO:  在此添加您专用的创建代码  
  8.     
  9. //  this->MoveWindow(0,0,0,0);    
  10. //  this->SetTimer(1,2000,NULL);//注意这个2000一点要等于步骤1中的2000   
  11.     Sleep(1000);//使得  画面停止约 1s后 启动主画面,<span style="color:#ff0000;">貌似可以用定时器,希望大虾指教</span>。 1000 要与SetTimer中1000 同步,使画面链接流畅  
  12.     return 0;  

5. 完毕

0 0
原创粉丝点击