www.pudn.com > yuzhishuanfa.zip > ProgDlg.cpp
//来自ImagePower工程,鲍捷,1998年8月-9月
// ProgDlg.cpp : implementation file
// CG: This file was added by the Progress Dialog component
#include "stdafx.h"
#include "resource.h"
#include "ProgDlg.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProgressDlg dialog
CProgressDlg::CProgressDlg(int nLower,int nUpper,int nStep,BOOL bClock)
{
// m_nCaptionID = CG_IDS_PROGRESS_CAPTION;
m_bCancel=FALSE;
m_bParentDisabled = FALSE;
EnableClock(bClock);
Create();
SetRange(nLower,nUpper);
SetStep(nStep);
m_Progress.SetPos(m_nLower);
return;
}
CProgressDlg::~CProgressDlg()
{
if(m_hWnd!=NULL)
DestroyWindow();
}
BOOL CProgressDlg::DestroyWindow()
{
ReEnableParent();
return CDialog::DestroyWindow();
}
void CProgressDlg::ReEnableParent()
{
if(m_bParentDisabled && (m_pParentWnd!=NULL))
m_pParentWnd->EnableWindow(TRUE);
m_bParentDisabled=FALSE;
}
BOOL CProgressDlg::Create(CWnd *pParent)
{
// Get the true parent of the dialog
m_pParentWnd = CWnd::GetSafeOwner(pParent);
// m_bParentDisabled is used to re-enable the parent window
// when the dialog is destroyed. So we don't want to set
// it to TRUE unless the parent was already enabled.
if((m_pParentWnd!=NULL) && m_pParentWnd->IsWindowEnabled())
{
m_pParentWnd->EnableWindow(FALSE);
m_bParentDisabled = TRUE;
}
if(!CDialog::Create(CProgressDlg::IDD,pParent))
{
ReEnableParent();
return FALSE;
}
return TRUE;
}
void CProgressDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProgressDlg)
DDX_Control(pDX, IDC_CLOCK, m_Clock);
DDX_Control(pDX, CG_IDC_PROGDLG_PROGRESS, m_Progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
//{{AFX_MSG_MAP(CProgressDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*
void CProgressDlg::SetStatus(LPCTSTR lpszMessage)
{
ASSERT(m_hWnd); // Don't call this _before_ the dialog has
// been created. Can be called from OnInitDialog
CWnd *pWndStatus = GetDlgItem(CG_IDC_PROGDLG_STATUS);
// Verify that the static text control exists
ASSERT(pWndStatus!=NULL);
pWndStatus->SetWindowText(lpszMessage);
}
*/
void CProgressDlg::OnCancel()
{
m_bCancel=TRUE;
}
void CProgressDlg::SetRange(int nLower,int nUpper)
{
m_nLower = nLower;
m_nUpper = nUpper;
m_Progress.SetRange(nLower,nUpper);
}
int CProgressDlg::SetPos(int nPos)
{
PumpMessages();
int iResult = m_Progress.SetPos(nPos);
UpdatePercent(nPos);
return iResult;
}
int CProgressDlg::SetStep(int nStep)
{
m_nStep = nStep; // Store for later use in calculating percentage
return m_Progress.SetStep(nStep);
}
int CProgressDlg::OffsetPos(int nPos)
{
PumpMessages();
int iResult = m_Progress.OffsetPos(nPos);
UpdatePercent(iResult+nPos);
return iResult;
}
int CProgressDlg::StepIt()
{
PumpMessages();
int iResult = m_Progress.StepIt();
UpdatePercent(iResult+m_nStep);
//{{绘制钟
if(m_bClock)
{
int nDivisor = m_nUpper - m_nLower;
int nDividend = (iResult+m_nStep - m_nLower);
CRect rectClock;
m_Clock.GetClientRect(&rectClock);
CClientDC dc(CWnd::FromHandle(m_Clock.m_hWnd));
dc.Ellipse(&rectClock);
CBrush brush(RGB(0,0,128));
CBrush *pOldBrush=(CBrush*)dc.SelectObject(&brush);
double StartAngle=0;
double EndAngle= (double)nDividend / (double)nDivisor*2*3.14159;
CPoint ptStart,ptEnd,ptCenter;
int m_Radius=min(rectClock.Width(),rectClock.Height())/2;
ptCenter=CPoint(m_Radius,m_Radius);
ptStart.x=ptCenter.x+long(m_Radius*cos(StartAngle));
ptStart.y=ptCenter.y-long(m_Radius*sin(StartAngle));
ptEnd .x=ptCenter.x+long(m_Radius*cos(EndAngle ));
ptEnd .y=ptCenter.y-long(m_Radius*sin(EndAngle ));
dc.Pie(rectClock.left,
rectClock.top,
rectClock.right,
rectClock.bottom,
ptStart.x,ptStart.y,
ptEnd.x,ptEnd.y
);
dc.SelectObject(pOldBrush);
}
//}}
return iResult;
}
void CProgressDlg::PumpMessages()
{
// Must call Create() before using the dialog
ASSERT(m_hWnd!=NULL);
MSG msg;
// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(!IsDialogMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
BOOL CProgressDlg::CheckCancelButton()
{
// Process all pending messages
PumpMessages();
// Reset m_bCancel to FALSE so that
// CheckCancelButton returns FALSE until the user
// clicks Cancel again. This will allow you to call
// CheckCancelButton and still continue the operation.
// If m_bCancel stayed TRUE, then the next call to
// CheckCancelButton would always return TRUE
BOOL bResult = m_bCancel;
m_bCancel = FALSE;
return bResult;
}
void CProgressDlg::UpdatePercent(int nNewPos)
{
CWnd *pWndPercent = GetDlgItem(CG_IDC_PROGDLG_PERCENT);
int nPercent;
int nDivisor = m_nUpper - m_nLower;
ASSERT(nDivisor>0); // m_nLower should be smaller than m_nUpper
int nDividend = (nNewPos - m_nLower);
ASSERT(nDividend>=0); // Current position should be greater than m_nLower
nPercent = nDividend * 100 / nDivisor;
// Since the Progress Control wraps, we will wrap the percentage
// along with it. However, don't reset 100% back to 0%
if(nPercent!=100)
nPercent %= 100;
// Display the percentage
CString strBuf;
strBuf.Format(_T("%d%c"),nPercent,_T('%'));
CString strCur; // get current percentage
pWndPercent->GetWindowText(strCur);
if (strCur != strBuf)
pWndPercent->SetWindowText(strBuf);
}
/////////////////////////////////////////////////////////////////////////////
// CProgressDlg message handlers
BOOL CProgressDlg::OnInitDialog()
{
CDialog::OnInitDialog();
if(m_bClock)
{
m_Clock.ShowWindow(SW_SHOW);
m_Progress.ShowWindow(SW_HIDE);
}
else
{
m_Clock.ShowWindow(SW_HIDE);
m_Progress.ShowWindow(SW_SHOW);
}
m_Progress.SetRange(m_nLower,m_nUpper);
m_Progress.SetStep(m_nStep);
m_Progress.SetPos(m_nLower);
// CString strCaption;
// VERIFY(strCaption.LoadString(m_nCaptionID));
// SetWindowText(strCaption);
// CG: The following block was added by the ToolTips component.
{
// Create the ToolTip control.
m_tooltip.Create(this);
m_tooltip.Activate(TRUE);
// TODO: Use one of the following forms to add controls:
// m_tooltip.AddTool(GetDlgItem(IDC_), );
// m_tooltip.AddTool(GetDlgItem(IDC_), "");
m_tooltip.AddTool(GetDlgItem(IDCANCEL), "取消本操作");
m_tooltip.AddTool(GetDlgItem(CG_IDC_PROGDLG_PROGRESS), "本次处理的进度");
}
return TRUE;
}
BOOL CProgressDlg::PreTranslateMessage(MSG* pMsg)
{
// CG: The following block was added by the ToolTips component.
{
// Let the ToolTip process this message.
m_tooltip.RelayEvent(pMsg);
}
return CDialog::PreTranslateMessage(pMsg); // CG: This was added by the ToolTips component.
}
//是否显示时钟
void CProgressDlg::EnableClock(BOOL bClock)
{
m_bClock = bClock;
}