Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368049
  • 博文数量: 715
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:46
文章分类

全部博文(715)

文章存档

2011年(1)

2008年(714)

我的朋友

分类:

2008-10-13 16:39:19

如何给基于对话框的应用加启动画面


作者/



    很多基于对话框的程序都有一个启动画面,那么如何为自己的对话框程序也加一个这样的画面呢?本文将一步一步教你实现每一个细节。其实VC++开发环境中有一个现成的 Splash Screen 组件,用这个组件很容易实现基于框架类(也就是CMainFrame)的程序的启动画面,那么如果是对话框程序,没有框架类怎么办呢?所以这个组件功能不是想象的那么强。要实现基于对话框应用程序的启动画面必须另辟蹊径。本文将定制一个C++类:CSplashWnd,用这个类可以增强原来的 Splash Screen 组件功能。它不但可以在用于具有CMainFrame的程序,同时也可以用于基于对话框应用的程序。
CSplashWnd的使用方法如下:

思路

需要改写三个函数:

      CDialog::OnInitDialog() 
     CWinApp::InitInstance()
      CWinApp::PreTranslateMessage(MSG* pMsg)      
    如果你用应用程序向导(AppWizard)创建工程,那么它会自动在CWinApp.h和CWinApp.cpp文件中产生 OnInitDialog 和 InitInstance 的声明和实现的默认代码,但是与CWinApp::PreTranslateMessage(MSG* pMsg)有关的处理必须自己添加到CWinApp派生类中。

具体步骤:

第一步:
在CDialog::OnInitDialog()方法末尾添加下列代码:
 	// 创建并显示启动画面
	CSplashWnd::ShowSplashScreen(3000, IDB_SPLASH24, this);     
ShowSplashScreen函数的第一个参数是超时时间,以毫秒计算,表示启动画面持续显示的时间;第二个参数是位图图像的资源标示符,表示启动画面显示的图像。最后一个参数是父窗口,此参数可以为NULL。

第二步:
在CWinApp::InitInstance()方法的开始处添加如下代码:
	// Enable the splash screen component based on the command line info.
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);      
这几行代码的作用是让程序能解析和处理命令行信息。

第三步:
用类向导(ClassWizard)改写 CWinApp::PreTranslateMessage(MSG* pMsg)方法,并添加如下代码:
     BOOL CDialogsplApp::PreTranslateMessage(MSG* pMsg) 
     {
       	     // Route messages to the splash screen while it is visible
             if (CSplashWnd::PreTranslateAppMessage(pMsg)) {
    	    	return TRUE;
             }
             return CWinApp::PreTranslateMessage(pMsg);
     }
编译运行程序。
--------------------next---------------------

当调用Sleep(3000)时...
鼠标与键盘就不起作用了...
( lxa520 发表于 2008-6-10 18:01:00)
 
int CSerProDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
CSplashWnd::ShowSplashScreen(this);
Sleep(3000);//时间与创建SPLASH SCREEN的时间相同就可以,这样,当SplashWindow显示时,主窗口就不会显示了。
} ( feige7887 发表于 2007-5-21 8:50:00)
 
第一步:
应该CDialog::OnCreate()方法开始处添加下列代码, 这样SplashWindow的显示才没有延迟吧 ( parine 发表于 2005-7-1 12:38:00)
 
有一个问题,当SplashWindow显示时,主窗口已经出来了 ( guoyun_he 发表于 2005-6-19 21:13:00)
 
有一个问题,当SplashWindow显示时,主窗口已经出来了,这有点影响美观。 ( Robinson 发表于 2005-3-27 0:49:00)
 
GetSystemWindowsDirectory函数应改为:
GetSystemDirectory ( zxg625 发表于 2003-1-16 16:06:00)
 
你的 OS 系统 是 Win98 还是 XP? ( iamsoHappy 发表于 2003-1-16 11:41:00)
 
GetSystemWindowsDirectory需要最新的Win32SDK的头文件*.H库文件*.LIB,但我只有头文件最新*.H,没有最新的库*.LIB,编译时出错:tatLink.obj : error LNK2001: unresolved external symbol __imp__GetSystemWindowsDirectoryA@8
Debug/DlgSplashScr.exe : fatal error LNK1120: 1 unresolved externals
你能给我一份最新的库文件LIB ( liron 发表于 2003-1-16 9:06:00)
 
.......................................................

--------------------next---------------------

阅读(148) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~