MFC進度條(CProgressCtrl) 重繪
先看一張圖:
新建一個 繼承自 CProgressCtrl 的類,然后添加 OnPaint 消息處理函數,此函數代碼如下:
void CNewProgress::OnPaint() { CPaintDC dc(this); CBrush BackgroundBrush; BackgroundBrush.CreateSolidBrush(RGB(255,0,0)); CBrush ForeBrush; ForeBrush.CreateSolidBrush(RGB(100,255,0)); CRect r; this->GetClientRect(r); double With=r.Width(); int min,max; this->GetRange(min,max); int pos= this->GetPos(); double unit=(double)r.Width()/(max-min); dc.FillRect(r,&BackgroundBrush); r.right=pos*unit; dc.FillRect(r,&ForeBrush); }
這樣就實現了 CProgressCtrl 重繪。
如果你想在 進度條中 添加 用鼠標左鍵單擊,然后到指定的位置,這一功能,就要再添加對OnLButtonDown 消息的處理。代碼如下:
void CNewProgress::OnLButtonDown(UINT nFlags, CPoint point) { CRect r; this->GetClientRect(r); double With=r.Width(); int min,max; this->GetRange(min,max); double unit=(double)(max-min)/r.Width(); // int pos= point.x*unit; this->SetPos(pos); CProgressCtrl::OnLButtonDown(nFlags, point); }