mfc为对话框添加启动画面

新建CwzdSplash类

CwzdSplash.h

 1 #pragma once  
 2 class CWzdSplash : public CWnd  
 3 {  
 4     DECLARE_DYNAMIC(CWzdSplash)  
 5 public:  
 6     CWzdSplash();  
 7     virtual ~CWzdSplash();  
 8 protected:  
 9     DECLARE_MESSAGE_MAP()  
10 public:  
11     CBitmap m_bitmap;  
12 //  void Create(void);  
13     void Create(UINT nBitmapID);  
14     afx_msg void OnPaint();  
15     afx_msg void OnTimer(UINT_PTR nIDEvent);  
16 
17 }; 

CwzdSplash.cpp

 1 // WzdSplash.cpp : 实现文件  
 2 //  
 3 #include "stdafx.h"  
 4 #include "CwzdSplash.h"  
 5 // CWzdSplash  
 6 IMPLEMENT_DYNAMIC(CWzdSplash, CWnd)  
 7 CWzdSplash::CWzdSplash()  
 8 {  
 9 }  
10 CWzdSplash::~CWzdSplash()  
11 {  
12 }  
13 BEGIN_MESSAGE_MAP(CWzdSplash, CWnd)  
14     ON_WM_PAINT()  
15     ON_WM_TIMER()  
16 END_MESSAGE_MAP()  
17 // CWzdSplash 消息处理程序 
18 void CWzdSplash::Create(UINT nBitmapID)  
19 {  
20     m_bitmap.LoadBitmap(nBitmapID);  
21     BITMAP bitmap;  
22     m_bitmap.GetBitmap(&bitmap);  
23     CreateEx(0,  
24         AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),  
25         NULL, WS_POPUP | WS_VISIBLE, 0, 0, bitmap.bmWidth, bitmap.bmHeight, NULL , NULL);  
26 }  
27 void CWzdSplash::OnPaint()  
28 {  
29     // TODO: 在此处添加消息处理程序代码  
30     // 不为绘图消息调用 CWnd::OnPaint()  
31     CPaintDC dc(this); // device context forpainting  
32     BITMAP bitmap;  
33     m_bitmap.GetBitmap(&bitmap);  
34     CDC dcComp;  
35     dcComp.CreateCompatibleDC(&dc);  
36     dcComp.SelectObject(&m_bitmap);  
37     // draw bitmap  
38     dc.BitBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,&dcComp,0,0,SRCCOPY);  
39 }  
40 void CWzdSplash::OnTimer(UINT_PTR nIDEvent)  
41 {  
42     // TODO: 在此添加消息处理程序代码和/或调用默认值  
43     //CWnd::OnTimer(nIDEvent);  
44     DestroyWindow(); //销毁初始画面窗口  
45 }  

在主窗口初始化处添加代码

1 //添加启动画面
2     CWzdSplash wndSplash;
3     wndSplash.Create(IDB_SPLASH);
4     wndSplash.CenterWindow();
5     wndSplash.UpdateWindow();
6     Sleep(3000);
7     wndSplash.DestroyWindow();

添加bmp图片,命名为IDB_SPALSH

运行:

猜你喜欢

转载自www.cnblogs.com/qiwu1314/p/9116557.html