MFC狀態欄的編程--動態時間,進度欄和鼠標位置顯示

1.狀態欄動態時間與進度欄的顯示

1.現在資源視圖的String Table中創建兩個個ID資源,我們給他ID號爲:

2.在CMainFrame中找到 static UINT indicators 這個指示器數組在其中加入 ID_TIME

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_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 here
	CRect rect;		
	m_wndStatusBar.GetItemRect(2,&rect);//獲取狀態欄中進度欄的窗口位置
	if(!myCtrl.m_hWnd)//控制進度欄的創建爲一次,因爲在窗口刷新中會調用OnPaint()重畫
		myCtrl.Create(PBS_SMOOTH | WS_CHILD | WS_VISIBLE ,rect,&m_wndStatusBar,123);//創建進度欄資源位置爲rect處,父窗口爲狀態欄
	else
		myCtrl.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 default

CTime 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 default
	CString str;
	str.Format("x=%d,y=%d",point.x,point.y);
	((CMainFrame*)GetParent())->SetMessageText(str);
	CView::OnMouseMove(nFlags, point);
}



第一次寫博,如有不周之處,敬請諒解!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章