⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progress.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// progress.cpp : implementation file
//

#include "stdafx.h"
#include "dialogs.h"
#include "progress.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DProgress dialog

BEGIN_MESSAGE_MAP(DProgress, CDialog)
	//{{AFX_MSG_MAP(DProgress)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
    ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()

// PERCENT_MAX * PERCENT_PART = 100 (approx)
// Good pairs: (Max,Part) = (20,5) or (25,4) or (33,3) or (50,2) or (100,1)
const int PERCENT_MAX  = 50;  // == 100 percent.
const int PERCENT_PART =  2;  // == Our scaling factor.

/////////////////////////////////////////////////////////////////////////////
// DProgress constructors and setup functions.

DProgress::DProgress(CWnd* pParent /*=NULL*/)
	: CDialog(DProgress::IDD, pParent)
{
    d_nPercent = 0;
	//{{AFX_DATA_INIT(DProgress)
	//}}AFX_DATA_INIT
}


void DProgress::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DProgress)
	//}}AFX_DATA_MAP
}


/////////////////////////////////////////////////////////////////////////////
// DProgress helper functions.

//-----------------------------------------------------------------------------
// Helper function to set current percentage.
void DProgress::SetPercent (int nSetPercent)
{
    // Enforce 0-100 range for percent.
    if (nSetPercent < 0)   nSetPercent = 0;
    if (nSetPercent > 100) nSetPercent = 100;

    // Store current percent.
    d_nPercent = nSetPercent;

    // Calculate percent to closest 5%.
    int nPart = nSetPercent/PERCENT_PART;

    // Create pointer to end of string array.
    LPTSTR lpstr = pstrBar + (PERCENT_MAX - nPart - 1);

    CStatic * pstPercent = (CStatic *)GetDlgItem(IDC_PROGRESS);
    pstPercent->SetWindowText(lpstr);
}

//-----------------------------------------------------------------------------
// Helper function to return current percent.
int  DProgress::GetPercent ()
{
    return d_nPercent;
}


//-----------------------------------------------------------------------------
// Resize static control to fit text.
void DProgress::ResizeStatic(CStatic * pst)
{
    // Calculate size of 100% string.
    CClientDC dc(this);
    dc.SelectObject(d_pfont);
    CSize szString = dc.GetTextExtent(pstrBar, PERCENT_MAX);
    szString.cx += (::GetSystemMetrics(SM_CXBORDER) * 4);

    // Query existing window size.
    RECT rStatic;
    GetWindowRect (&rStatic);

    // Set new size.
    pst->SetWindowPos(&CWnd::wndTop, 0, 0, 
                      szString.cx, szString.cy, SWP_NOMOVE);
}


//-----------------------------------------------------------------------------
// Resize static control to fit text.
void DProgress::CenterChildWindow(CWnd * pwndChild)
{
    // Get pointer to parent window.
    CWnd * pwndParent = pwndChild->GetParent();

    // Fetch parent size, then calculate center.
    CRect rParent;
    pwndParent->GetClientRect(&rParent);

    // Fetch child size.
    CRect rChild;
    pwndChild->GetWindowRect(&rChild);
    pwndParent->ScreenToClient(&rChild);

    // Calculate offset from parent to child.
    int cx = ((rParent.right + rParent.left)/2) -
             ((rChild.right  + rChild.left)/2);
    int cy = ((rParent.top   + rParent.bottom)/2) -
             ((rChild.top    + rChild.bottom)/2);

    // Apply offset to child rectangle.
    rChild.OffsetRect(cx, cy);

    // Move child to center of parent window.
    pwndChild->MoveWindow(rChild, FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// DProgress message handlers

//-----------------------------------------------------------------------------
// Handle WM_INITDIALOG initialization message.
BOOL DProgress::OnInitDialog() 
{
	CDialog::OnInitDialog();

    // Fetch pointer to progress bar static control.
    CStatic * pstPercent = (CStatic *)GetDlgItem(IDC_PROGRESS);
    
    // Not defined for this class -- BOGUS!!
    // ASSERT(pstPercent->IsKindOf(RUNTIME_CLASS(CStatic)));

    // Add a border to progress bar window.
    DWORD dwStyle = pstPercent->GetStyle();
    dwStyle |= WS_BORDER;
    ::SetWindowLong(pstPercent->m_hWnd, GWL_STYLE, dwStyle);

    // Calculate font width based on window size.
    RECT rChild;
    pstPercent->GetClientRect(&rChild);

    // Define a WinAPI logical font.
    d_lf.lfWidth  = rChild.right / PERCENT_MAX;
    lstrcpy(d_lf.lfFaceName, "WingDings");
    d_lf.lfHeight = 0;
    d_lf.lfEscapement = 0;
    d_lf.lfOrientation = 0;
    d_lf.lfWeight = 0;
    d_lf.lfItalic = 0;
    d_lf.lfUnderline = 0;
    d_lf.lfStrikeOut = 0;
    d_lf.lfCharSet = SYMBOL_CHARSET;
    d_lf.lfOutPrecision = 0;
    d_lf.lfClipPrecision = 0;
    d_lf.lfQuality = 0;
    d_lf.lfPitchAndFamily = 0;

    // Connect WinAPI font to CFont (MFC) font object.
    d_pfont = new CFont();
    if (!d_pfont)
    {
        DestroyWindow();
        return FALSE;
    }
    d_pfont->CreateFontIndirect(&d_lf);

    // Set up percent complete box to hold WingDing font.
    pstPercent->SetFont(d_pfont, FALSE);

    // Center dialog over frame window.
    CenterWindow();

	pstrBar = new TCHAR[PERCENT_MAX+1];
    if (!pstrBar)
    {
        DestroyWindow();
    }
    LPTSTR lp = pstrBar;
    for (int i = 0; i < PERCENT_MAX; i++, lp++)
        *lp = (TCHAR)'n';
    pstrBar[PERCENT_MAX] = (TCHAR)'\0';
	
    // Resize progress indicator and center over dialog.
    ResizeStatic(pstPercent);
    CenterChildWindow(pstPercent);

	return TRUE;  
}

//-----------------------------------------------------------------------------
// Handle WM_CTLCOLOR message -- what color are my children?
HBRUSH DProgress::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
    // Call default handler to do most of the work.
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // Draw blue blocks for progres indicator bars.
    pDC->SetTextColor(RGB(0, 0, 255));

    return hbr;
}

//-----------------------------------------------------------------------------
// Handle OnCancel event when user hits Escape key.
void DProgress::OnCancel()
{
    // Delete font.
    if (d_pfont)
    {
        delete d_pfont;
        d_pfont = 0;
    }

    // Tell parent that we're shutting down.
    CWnd * pMain = GetParent();
    pMain->PostMessage(PM_PROGRESS_CLOSING);

    // Destroy window.
    DestroyWindow();

    // Clean up percent bar string.
    delete [] pstrBar;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -