MFC状态栏的编程--动态时间,进度栏和鼠标位置显示

来源:互联网 发布:流星蝴蝶剑轻功 知乎 编辑:程序博客网 时间:2024/05/16 04:25

1.状态栏动态时间与进度栏的显示

1.现在资源视图的String Table中创建两个个ID资源,我们给他ID号为:

2.在CMainFrame中找到 static UINT indicators 这个指示器数组在其中加入 ID_TIME

static UINT indicators[] ={ID_SEPARATOR,           // status line indicatorID_TIME,ID_JINDU,ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};

3.在CMainFrame 中添加进度栏控件

CProgressCtrl myCtrl;. 

4.在CMainFrame中 Add Windows Message Handler     为 WM_PAINT添加句柄响应消息

void CMainFrame::OnPaint() {CPaintDC dc(this); // device context for painting// TODO: Add your message handler code hereCRect rect;m_wndStatusBar.GetItemRect(2,&rect);//获取状态栏中进度栏的窗口位置if(!myCtrl.m_hWnd)//控制进度栏的创建为一次,因为在窗口刷新中会调用OnPaint()重画myCtrl.Create(PBS_SMOOTH | WS_CHILD | WS_VISIBLE ,rect,&m_wndStatusBar,123);//创建进度栏资源位置为rect处,父窗口为状态栏elsemyCtrl.MoveWindow(rect);//窗口发生改变时移动进度栏,确保状态栏中进度条相对位置的稳定myCtrl.SetIt();//画出进度栏中的进度// Do not call CFrameWnd::OnPaint() for painting messages}

5.在 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)  函数中写入 

SetTime(1,1000,NULL);  //启动定时器,间隔时间为1秒


6.在CMainFrame中 Add Windows Message Handler     为 WM_TIMER添加句柄响应消息

7.在OnTimer定时器函数中写入响应函数

void CMainFrame::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultCTime t = CTime::GetCurrentTime();CString strT=t.Format("%H:%M:%S");CClientDC dc(this);CSize sizeStr=dc.GetTextExtent(strT);m_wndStatusBar.SetPaneInfo(1,ID_TIME,SBPS_NORMAL,sizeStr.cx);m_wndStatusBar.SetPaneText(1,strT,TRUE);myCtrl.StepIt();CFrameWnd::OnTimer(nIDEvent);}


2.鼠标位置在状态栏中的显示

1.在view中 Add Windows Message Handler     为 WM_MOUSEMOVE添加句柄响应消息
2.在OnMouseMove函数中写入:
void CLesson9_StyleView::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCString str;str.Format("x=%d,y=%d",point.x,point.y);((CMainFrame*)GetParent())->SetMessageText(str);CView::OnMouseMove(nFlags, point);}



第一次写博,如有不周之处,敬请谅解!


                                             
0 0
原创粉丝点击