孙鑫VC++深入详解:Lesson9 Part3---模拟动画图标


让应用程序标题栏的图标按照时间轮换

思路:

1) SetClassLong函数在窗口建成后调用图标文件(事先准备好3个图标文件),在OnCreate中处理

2) 在OnCreate中放置一个SetTimer

3)处理WM_TIIMER消息即OnTime函数


知识点:

用三种方法获取应用程序实例hInstance句柄

1) AfxGetInstanceHandle()

2)theApp.m_hInstance

3)AfxGetApp()->m_hInstance

资源ID转化为资源标识字符串 MAKEINTRESOURCE,这是个宏,WIN32编程中常用.

注意:theApp是在类CStyleApp文件中定义的全局变量CStyleApp theApp,在CMainFrame中要用它,必须先申明

theApp来自外部,即: extern CStyleApp theApp


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	
//------先加载图标到数字m_hIcon[], 用SetClassLong()调用,修改标题图标,放置一个定时器

	m_hIcon[0] =LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));

	extern CStyleApp theApp; // 要用theApp这个全局的应用程序对象,需要申明下它是CStyleApp中定义过的.
	m_hIcon[1] =LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));
	
	m_hIcon[2] =LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));

    SetClassLong(this->m_hWnd,GCL_HICON,(LONG)m_hIcon[0]);//把m_hIcon[0]设置为窗口创建后的默认图标

	SetTimer(1,1000,NULL); //设置一个定时器,定时产生WM_TIMER消息
   

	return 0;
}


// 响应定时器SetTimer(1,1000,NULL)的WM_TIMER消息函数
void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	static int index =1;
	SetClassLong(this->m_hWnd,GCL_HICON,(LONG)m_hIcon[index]);	
	index++;if(index==3)index=0; // 或者取3的取模运算:index=++index%3;

	CFrameWnd::OnTimer(nIDEvent);
}








你可能感兴趣的:(孙鑫VC++深入详解:Lesson9 Part3---模拟动画图标)