头文件:
1 //{{AFX_MSG(CMainFrame)
2 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
3 afx_msg void OnTimer(UINT nIDEvent);
4 afx_msg void OnPaint();
5 afx_msg void OnProgress();//消息
6 //}}AFX_MSG
源文件:
1 //{{AFX_MSG_MAP(CMainFrame)
2 ON_WM_CREATE()
3 ON_WM_TIMER()
4 ON_WM_PAINT()
5 ON_MESSAGE(UM_PROGRESS,OnProgress) //消息
6 //}}AFX_MSG_MAP
3、 在源文件中添加消息响应函数
1 void CMainFrame::OnProgress()
2 {
3 CRect rect;
4 m_wndStatusBar.GetItemRect(5,&rect);
5 m_cpc.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH,rect,&m_wndStatusBar,123);
6 m_cpc.SetPos(50);
7 }
4、 在源文件的OnCreate函数中添加PostMessage(UM_PROGRESS);
这样便可以在状态栏动态创建一个进度条。这里有个一SendMessage函数和PostMessage函数的区别说明:
1、 SendMessage:直接把消息发送给消息响应函数,等消息响应函数处理完之后再返回。
2、 PostMessage:把消息发送到消息队列,然后立即返回。
由于OnProgress函数需要在OnCreate执行完之后再响应,因此此处采用PostMessage函数来发送该消息。
创建进度条完成之后,显示正常,但是如果此时拉动窗口,改变大小,会发现进度条的位置不正确了,为了保证进度条的位置正确,需要添加OnPaint函数。
01 void CMainFrame::OnPaint()
02 {
03 CPaintDC dc(this); // device context for painting
04 // TODO: Add your message handler code here
05 CRect rect;
06 m_wndStatusBar.GetItemRect(5,&rect);
07 if(!m_cpc.m_hWnd)
08 {
09 m_cpc.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH,rect,&m_wndStatusBar,123);
10 }
11 else
12 {
13 m_cpc.MoveWindow(rect);
14 }
15 m_cpc.SetPos(50);
16 // Do not call CFrameWnd::OnPaint() for painting messages
17 }
因此WM_PAINT消息是自动执行的,这样就不需要在OnCreate函数中再添加PostMessage函数来发送消息了。
完成进度条的创建之后,可以再OnTimer函数中添加代码,实现进度条的动态效果,代码就不贴上来了,都是调用相关函数来实现。
9.6在状态栏上显示鼠标
在View类上添加WM_MOUEOVER函数:
1 void CStyleView::OnMouseMove(UINT nFlags, CPoint point)
2 {
3 // TODO: Add your message handler code here and/or call default
4 CString str;
5 str.Format("x=%d y=%d",point.x,point.y);
6 ((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);
7 CView::OnMouseMove(nFlags, point);
8 }
这之前需要在View类中引入Frame类的头文件,并将Frame类的m_wndStatusBar设置为public.
责任编辑:小草