淡入淡出启动画面的制作

http://down.51cto.com/data/133335

http://blog.csdn.net/libenqing/article/details/5961991

CSplashWnd.h

#pragma once
#include "afxwin.h"


// CSplashWnd

class CSplashWnd : public CWnd
{
	DECLARE_DYNAMIC(CSplashWnd)

public:
	CSplashWnd(UINT nBitmapID);
	virtual ~CSplashWnd();

protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnClose();
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	CBitmap m_bitmap;
	BITMAP bitmap;
};


CSplashWnd.cpp
// SplashWnd.cpp : 实现文件
//

#include "stdafx.h"
#include "SecDistribute.h"
#include "SplashWnd.h"


// CSplashWnd

IMPLEMENT_DYNAMIC(CSplashWnd, CWnd)

CSplashWnd::CSplashWnd(UINT nBitmapID)
{
	m_bitmap.LoadBitmap(nBitmapID);  
	//BITMAP bitmap;  
	m_bitmap.GetBitmap(&bitmap); 
	LPCTSTR lpszWdnClass = AfxRegisterWndClass(NULL);//注册窗口
	CreateEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,lpszWdnClass,NULL,
		WS_POPUP,CW_USEDEFAULT,CW_USEDEFAULT,bitmap.bmWidth,bitmap.bmHeight,NULL,NULL,NULL);//创建窗口

}

CSplashWnd::~CSplashWnd()
{
}


BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()



// CSplashWnd 消息处理程序




int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CenterWindow();//启动画面居中
	SetTimer(1,300,NULL);//启动画面显示时间为3秒
	::AnimateWindow(GetSafeHwnd(),1200,AW_BLEND); //窗口渐显,实现淡入,时间1.2秒
	return 0;
}


void CSplashWnd::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	::AnimateWindow(GetSafeHwnd(),800,AW_BLEND | AW_HIDE);//窗口渐隐
	CWnd::OnClose();
}


void CSplashWnd::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	KillTimer(1);		  //销毁定时器
	PostMessage(WM_CLOSE); //关闭启动画面
	CWnd::OnTimer(nIDEvent);
}


BOOL CSplashWnd::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	HINSTANCE hApp = ::GetModuleHandle(NULL);//得到应用程序的模块句柄
	HBITMAP hbmp = ::LoadBitmap(hApp,MAKEINTRESOURCE(IDB_BITMAPLOGO));//加载程序中的位图资源
	HDC hmdc = ::CreateCompatibleDC(pDC->GetSafeHdc());// 创建兼容DC作为内存DC
	HBITMAP hbmpOld = (HBITMAP) ::SelectObject(hmdc,hbmp);//将位图资源选入内存
	::BitBlt(pDC->GetSafeHdc(),0,0,bitmap.bmWidth,bitmap.bmHeight,hmdc,0,0,SRCCOPY);//将内存dc的内容拷贝到设备dc上显示
	::SelectObject(hmdc,hbmpOld); //清理内存 防止内存泄漏
	::DeleteObject(hmdc);
	::DeleteDC(hmdc);
	return CWnd::OnEraseBkgnd(pDC);
}

在app里面的InitInstance函数或者MainFrame中的OnCreate函数中添加调用代码

pSplash最好是添加到app或者MainFrame中的成员变量CSplashWnd *pSplash;  记得添加#include "SplashWnd.h"头文件 否则会识别不了的 app或者MainFrame中的头文件中添加

还有要是写在MainFrame中的话pSplash->ShowWindow(m_nCmdShow);要改成pSplash->ShowWindow(theApp.m_nCmdShow);

app的析构函数要手动添加,MainFrame中直接就有

if(pSplash != NULL)delete pSplash; 防止内存泄漏



查了一下ontimer消息说明,当SetTimer(1,0,NULL);最后一个参数的时候回调函数就是直接调用OnTimer函数。。。

UINT SetTimer( 
UINT nIDEvent, 
UINT nElapse, 
void (CALLBACK EXPORT* lpfnTimer)( 
HWND, UINT, UINT, DWORD) ); 
Parameters
nIDEvent
Specifies a nonzero timer identifier.
nElapse
Specifies the time-out value, in milliseconds.
lpfnTimer
Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application message queue and handled by the CWnd object.
Return Value

The timer identifier of the new timer if the function is successful. An application passes this value to theCWnd::KillTimer method to kill the timer. Nonzero if it is successful; otherwise, it is zero.

Example

This example uses CWnd::SetTimer, CWnd::OnTimer, and CWnd::KillTimer to handle WM_TIMER messages. A timer is set up to send a WM_TIMER message to the main frame window every 2 seconds inOnStartTimer. OnStopTimer will stop the timer by callingCWnd::KillTimer. OnTimer was set up through ClassWizard to handle WM_TIMER messages for the main frame window. In this example, the PC speaker will beep every 2 seconds.

void CMainFrame::OnStartTimer() 
{
  m_nTimer = SetTimer(1, 2000, 0);
}

void CMainFrame::OnStopTimer() 
{
  KillTimer(m_nTimer);  
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
  MessageBeep(0xFFFFFFFF);  // Beep

  // Call base class handler.
  CMDIFrameWnd::OnTimer(nIDEvent);
}

所以这里设置timer很鸡肋的感觉。。

直接这样写更好些

	CenterWindow();//启动画面居中
	//SetTimer(1,0,NULL);//启动画面显示时间为3秒
	::AnimateWindow(GetSafeHwnd(),1200,AW_BLEND); //窗口渐显,实现淡入,时间1.2秒
	PostMessage(WM_CLOSE); //关闭启动画面
OnTimer中的自己之前添加的代码也可以去掉了


	//======================启动画面======================

	pSplash = new CSplashWnd(IDB_BITMAPLOGO);
	pSplash->ShowWindow(m_nCmdShow);
	Sleep(1000);
	//====================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值