MFC创建一个程序启动画面

版权声明:转载请注明出处,谢谢 https://blog.csdn.net/roccreed/article/details/7473758


在自己写程序的时候,我们可以为我们自己的程序添加一个类似于WORD,VS之类的初始化界面。

具体方法如下:

	CString str=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
		AfxGetApp()->LoadCursor(IDC_WAIT),(HBRUSH)(COLOR_WINDOW+1),NULL);

	CSplashWnd wnd;
	//用注册好的类来创建窗口
	wnd.CreateEx(WS_EX_CLIENTEDGE,str,_T(""),WS_POPUP,CRect(0,0,0,0),NULL,NULL);
	//Call this member function to change the size, position, 
	//and Z-order of child, pop-up, and top-level windows.
	wnd.SetWindowPos(NULL,100,100,wnd.m_bitmap.bmWidth,wnd.m_bitmap.bmHeight,SWP_NOZORDER);
	//This method centers a window relative to its parent.
	//If the pop-up window is not owned, it is centered relative to the screen.
	wnd.CenterWindow();
	//Sets the visibility state of the window. 
	wnd.ShowWindow(SW_SHOW);
	//Send a WM_PAINT msg
	wnd.UpdateWindow();
	::Sleep(3000);
	wnd.DestroyWindow();

CSplashWnd是我们自己编写的CWnd派生类,下面贴出CSplashWnd的相关代码。

在其头文件中,声明了:

	CBitmap m_CBitmap;
	BITMAP  m_bitmap;

用以处理初始化界面的位图。

在PreCreteWindow函数中,在窗口创建前,修改其style

	if(!CWnd::PreCreateWindow(cs))
		return FALSE;
	cs.style=WS_POPUP;
然后,在OnCreate中添加
	m_CBitmap.LoadBitmap(IDB_BITMAP1);
	//把m_CBitmap的图形信息填充到BITMAP结构的m_bitmap中
	m_CBitmap.GetObject(sizeof(BITMAP),&m_bitmap);

m_bitmap填充了IDB_BITMAP1的图形信息,其中的宽度高度等信息后面会用到

然后我们在OnPaint中添加下面代码,就大功告成了

扫描二维码关注公众号,回复: 3610414 查看本文章

	CPaintDC dc(this); 
	CDC dcMem;
	CBitmap *pBmp;
	CFont font;
	font.CreatePointFont(150,_T("Times New Roman"));
	dcMem.CreateCompatibleDC(&dc);
	//把位图选入到dcMem中。
	pBmp=(CBitmap *)dcMem.SelectObject(&m_CBitmap);

	dc.BitBlt(0,0,m_bitmap.bmWidth,m_bitmap.bmHeight,&dcMem,0,0,SRCCOPY);
	dc.SelectObject(&font);
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(RGB(255,255,0));
	dc.TextOut(50,m_bitmap.bmHeight-60,CString("初始化..."));
	//?????
	dcMem.SelectObject(pBmp);

最后一行代码,本人不明白有什么意义,还请路过的大神指点。

另外我是VC初学者,文中错漏之处,也请告诉我。



猜你喜欢

转载自blog.csdn.net/roccreed/article/details/7473758