【VC编程技巧】窗口☞3.5对单文档或者多文档程序制作启动画面

(一)概要:

文章描写叙述了如何通过Visual C++ 2012或者Visual C++ .NET,为单文档或者多文档程序制作启动画面。在Microsoft Visual Studio 6.0中对于单文档程序(SDI)我们能够非常方便利用微软提供的组件Visual C++ Component (Splash Screen)。由于在Microsoft Visual Studio 6.0以后的版本号或者Visual C++ .NET没有提供这个组件,我们能够通过自己定义对话框来实现Splash Screen功能。



(二)对于单文档或者多文档程序制作启动画面

1.启动Microsoft Visual Studio 2012创建默认的单文档程序;(非常easy,不须要多说)

2.在project的资源编辑器中。创建一个对话框(启动画面)

   改动对话框的属性:

              对话框ID:IDD_SPLASH

             对话框Title Bar:False

             对话框BorderThin

3.加入图片控件picture control在启动画面

   改动控件属性:

            控件type:bitmap

4.创建启动画面加入关联类CSplashDlg,

   类CSplashDlg的基类:CDialog

5.对于类CSplashDlg加入例如以下函数(依据Microsoft Visual Studio 6.0中Splash Screen控件自己主动生成):

// CSplashDlg メッセージ ハンドラー

//说明:静态函数,用于显示启动画面( splash screen )
//參数:pParentWnd 父窗体指针
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);
}

//说明:用于销毁启动画面( splash screen )
//參数:无
void CSplashDlg::HideSplashScreen(void)
{
	// Destroy the window, and update the mainframe.
	c_pSplashDlg->KillTimer(1);
	DestroyWindow();
	AfxGetMainWnd()->UpdateWindow();
	delete c_pSplashDlg;
	c_pSplashDlg = NULL;
}

//说明:静态函数,用于键盘或鼠标消息触发时,退出启动画面( splash screen )
//參数:pMsg MFC标准消息
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
}

//说明:用于控制启动画面( splash screen )持续的时间
//參数:MFC标准Event
void CSplashDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: ここにメッセージ ハンドラー コードを追加するか、既定の処理を呼び出します。
	
	// Destroy the splash screen window.
	HideSplashScreen();
	
	//CDialogEx::OnTimer(nIDEvent);
}


BOOL CSplashDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  ここに初期化を追加してください	
	//设置启动画面的图片
	SetImage(_T("1.png"), IDC_PIC);
	//启动画面居中
	CenterWindow();
	//启动画面置定
	SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0,
      SWP_NOMOVE|SWP_NOSIZE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 例外 : OCX プロパティ ページは必ず FALSE を返します。

} //说明:将图片显示在指定PictureControl上 //參数:pszFileName 图片名(全路径/相对路径) // uID PictureControl控件ID VOID CSplashDlg::SetImage(LPCTSTR pszFileName, UINT uID) { CStatic* pWnd = (CStatic*)GetDlgItem(uID); CImage image; image.Load(pszFileName); HBITMAP hBmp = image.Detach(); CRect rect; GetClientRect(&rect); pWnd->SetBitmap(hBmp); pWnd->SetWindowPos(NULL, rect.left, rect.top, rect.right, rect.bottom, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER); return VOID(); }



6.通过调用上面类CSplashDlg的成员函数。控制启动画面。

   在单文档框架显示之前显示启动画面

// CSplashSDIApp 初期化

BOOL CSplashSDIApp::InitInstance()
{
	...
	
	//加入启动画面
	CSplashDlg::ShowSplashScreen(NULL);

	// メイン ウィンドウが初期化されたので、表示と更新を行います。

m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; }


   截取主框架消息(当有鼠标或者键盘消息时,启动画面退出)

BOOL CSplashSDIApp::PreTranslateMessage(MSG* pMsg)
{
	// TODO: ここに特定なコードを追加するか、もしくは基本クラスを呼び出してください。

if(CSplashDlg::PreTranslateAppMessage (pMsg)) return TRUE; return CWinAppEx::PreTranslateMessage(pMsg); }



(三)执行效果图:


原文地址:https://www.cnblogs.com/wzjhoutai/p/6943439.html