码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-07-12 18:52:53      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   strong   os   

(一)概要:

文章描述了怎样通过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创建默认的单文档程序;(很简单,不需要多说)

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

   修改对话框的属性:

              对话框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);
}


(三)运行效果图:

bubuko.com,布布扣

【VC编程技巧】窗体?3.5对单文档或者多文档程序制作启动画面,布布扣,bubuko.com

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

标签:des   style   blog   http   strong   os   

原文地址:http://blog.csdn.net/chen_jint/article/details/37660121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!