?? newprogressctrl.cpp
字號(hào):
// NewProgressCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "..\commonctrls.h"
#include "NewProgressCtrl.h"
#include "ProgressWnd.h"
#include <time.h>
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNewProgressCtrl
CNewProgressCtrl::CNewProgressCtrl()
{
LOGFONT logFount;
m_lfPercent = 0.0;
m_start = clock();
// create a horizontal fount
m_fountHorz.CreatePointFont(90, "MS Sans", NULL);
// create a vertical fount the same as the horizontal one
m_fountHorz.GetLogFont(&logFount);
logFount.lfEscapement = 900;
logFount.lfOrientation = 900;
m_fountVert.CreateFontIndirect(&logFount);
}
CNewProgressCtrl::~CNewProgressCtrl()
{
}
BEGIN_MESSAGE_MAP(CNewProgressCtrl, CProgressCtrl)
//{{AFX_MSG_MAP(CNewProgressCtrl)
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNewProgressCtrl message handlers
void CNewProgressCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CProgressWnd wndProgress(this, "彈出式進(jìn)度條", TRUE);
#define TEST_RANGE 20000
wndProgress.SetRange(0,TEST_RANGE);
wndProgress.SetText("這是一個(gè)彈出式進(jìn)度條窗口...");
for (int i = 0; i < TEST_RANGE; i++) {
wndProgress.StepIt();
wndProgress.PeekAndPump();
if (wndProgress.Cancelled()) {
MessageBox("取消進(jìn)度條窗口");
break;
}
}
CProgressCtrl::OnLButtonDown(nFlags, point);
}
BOOL CNewProgressCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CNewProgressCtrl::OnPaint()
{
// TODO: Add your message handler code here
int nRange, nLower, nUpper;
CBrush brush(::GetSysColor(COLOR_HIGHLIGHT));
CPaintDC dcPaint(this);// device context for painting
CMemDC dc(&dcPaint);
CRect rcClient, rcBlock, rcComplete;
LONG lStyle;
int nGapWidth = 2;
int nLoop, nBlocks;
clock_t now = clock();
CString strRemaining;
GetRange(nLower, nUpper);
GetClientRect(&rcClient);
rcBlock = rcClient;
nRange = nUpper - nLower;
m_lfPercent = (double)GetPos() / (double)nRange;
lStyle = ::GetWindowLong(*this, GWL_STYLE);
if ((lStyle & PBS_VERTICAL) == PBS_VERTICAL)
rcBlock.top = rcBlock.bottom - (int)(rcBlock.Height() * m_lfPercent);
else
rcBlock.right = rcBlock.left + (int)(rcBlock.Width() * m_lfPercent);
rcComplete = rcBlock;
if ((lStyle & PBS_SMOOTH) == PBS_SMOOTH)
nBlocks = 1;
else
{
#define BLOCKS 18.0
rcBlock = rcClient;
if ((lStyle & PBS_VERTICAL) == PBS_VERTICAL)
rcBlock.top = rcBlock.bottom - (int)((rcClient.Height()-(9*nGapWidth)) / BLOCKS);
else
rcBlock.right = rcBlock.left + (int)((rcClient.Width()-(9*nGapWidth)) / BLOCKS);
nBlocks = (int)((m_lfPercent*BLOCKS)+0.9);
}
if (GetParent() != NULL)
::FillRect(dc, &rcClient, (HBRUSH)GetParent()->SendMessage(WM_CTLCOLORSTATIC, (WPARAM)(HDC)dc, (LPARAM)this->GetSafeHwnd()));
for (nLoop=1; nLoop<=nBlocks; nLoop++)
{
::FillRect(dc, &rcBlock, brush);
if ((lStyle & PBS_VERTICAL) == PBS_VERTICAL)
{
rcComplete.top = rcBlock.top;
rcBlock.OffsetRect(0, -(rcBlock.Height()+nGapWidth));
}
else
{
rcComplete.right = rcBlock.right;
rcBlock.OffsetRect(rcBlock.Width()+nGapWidth, 0);
}
}
if (m_lfPercent > 0.02)
{
CFont *pFount;
double lfSecsSoFar = (double)(now - m_start) / CLOCKS_PER_SEC;
strRemaining = GetRemainingText(m_lfPercent, (lfSecsSoFar / m_lfPercent) * (1.0 - m_lfPercent));
dc.SetBkMode(TRANSPARENT);
if ((lStyle & PBS_VERTICAL) == PBS_VERTICAL)
{
CSize szExt;
CPoint ptText;
// draw the vertical text centred in the control
pFount = dc.SelectObject(&m_fountVert);
szExt = dc.GetTextExtent(strRemaining);
ptText.x = (rcClient.Width() - szExt.cy) >> 1;
ptText.y = rcClient.bottom - ((rcClient.Height() - szExt.cx) >> 1);
dc.SaveDC();
dc.IntersectClipRect(rcComplete);
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
dc.ExtTextOut(ptText.x, ptText.y, ETO_CLIPPED, rcClient, strRemaining, NULL);
dc.RestoreDC(-1);
dc.SaveDC();
dc.ExcludeClipRect(rcComplete);
dc.SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
dc.ExtTextOut(ptText.x, ptText.y, ETO_CLIPPED, rcClient, strRemaining, NULL);
dc.RestoreDC(-1);
dc.SelectObject(pFount);
}
else
{
// horizontal text is easier
pFount = dc.SelectObject(&m_fountHorz);
dc.SaveDC();
dc.IntersectClipRect(rcComplete);
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
dc.DrawText(strRemaining, rcClient, DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER | DT_CENTER);
dc.RestoreDC(-1);
dc.SaveDC();
dc.ExcludeClipRect(rcComplete);
dc.SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
dc.DrawText(strRemaining, rcClient, DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER | DT_CENTER);
dc.RestoreDC(-1);
dc.SelectObject(pFount);
}
}
// Do not call CProgressCtrl::OnPaint() for painting messages
}
CString CNewProgressCtrl::GetRemainingText(double lfPercent, double lfSecsRemaining)
{
CString str;
int nSeconds = (int)fmod(lfSecsRemaining, 60.0);
if (lfSecsRemaining < 60)
{
if (nSeconds < 1)
str = "少于1秒";
else
str.Format("剩余%d 秒", nSeconds);
}
else
{
int nMinutes = (int)(lfSecsRemaining/60.0);
str.Format("剩余%d分%d 秒", nMinutes,nSeconds);
}
return str;
}
void CNewProgressCtrl::ResetStartTime(void)
{
m_start = clock();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -