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

📄 progress_in_status2.shtml.htm

📁 mfc资料集合5
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Chris Maunder">
   <TITLE>StatusBar - Showing progress bar in a status bar pane (2)</TITLE>
</HEAD>
<BODY>

<CENTER>
<H3>
<FONT COLOR="#000099">Showing progress bar in a status bar pane (2)</FONT></H3></CENTER>

<CENTER>
<H3>

<HR></H3></CENTER>
This article was contributed by 
<A HREF="mailto:Chris.Maunder@cbr.clw.csiro.au">Chris Maunder</A>. 

<br><br><IMG SRC="CProgressBar.gif" tppabs="http://www.codeguru.com/statusbar/CProgressBar.gif">
<br><A HREF="Progress2.zip" tppabs="http://www.codeguru.com/statusbar/Progress2.zip">Download source files</A><br>

<p>
Adding a CProgressCtrl to the status bar has already been addressed
by Brad Mann. His method involved modifying the status bar and messing
around with the resource editor. I developed a separate CProgressBar
class in order to allow the programmer to just drop in a progress bar
whereever they wanted using a single "CProgressBar Bar(...)" declaration,
which would initialise and display itself and clean up after itself
after it was done. The progress bar can also be created once (say as a 
member variable) and reused multiple times. This new version of the
progress bar also resizes itself if the status bar size changes.
</p>

<p>
The progress bar lets you specify a message (displayed to the left of the bar)
and a Progress Control bar in the first pane of your applications status bar 
(if it has one - thanks to Patty You for a bug fix on that one). The message 
for the progress bar can be changed at any time, as can the size and range
of the bar.
</p> 

<p><b>Construction</b></p>

<PRE><TT><FONT COLOR="#990000">
	CProgressBar(); 
	CProgressBar(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE);
	BOOL Create(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE);
</FONT></TT></PRE>

<p>
Construction is either via the constructor or a two-step process using the
constructor and the "Create" function. "strMessage" is the message to be
displayed, "nSize" is the percentage width of the status bar that will be
occupied by the bar (including the text), and "MaxValue" is the maximum
range of the bar.</p>

<p>
"bSmooth" will only be effective if you have the header files and commctrl32.dll
from IE 3.0 or above (no problems for MS VC 5.0). It specifies whether the progress 
bar will be smooth or chunky. 
</p>

<p><b>Operations</b></p>
<PRE><TT><FONT COLOR="#990000">
	BOOL Success()                      // Construction successful?
	int  SetPos(int nPos);              // Same as CProgressCtrl
	int  OffsetPos(int nPos);           // Same as CProgressCtrl
	int  SetStep(int nStep);            // Same as CProgressCtrl
	int  StepIt();                      // Same as CProgressCtrl
	void Clear();                       // Clear the status bar
	void SetRange(int nLower, int nUpper, int nStep = 1);
	                                    // Set min, max and step size
	void SetText(LPCTSTR strMessage);   // Set the message
	void SetSize(int nSize);            // Set the bar size
</FONT></TT></PRE>

<p>To use the bar, just do something like:

<PRE><TT><FONT COLOR="#990000">
	CProgressBar Bar("Testing", 40, 1000);
	
	for (int i = 0; i < 1000; i++) {
	    // perform operation
	    Bar.StepIt();
	}
</FONT></TT></PRE>

or it can be done two stage as:

<PRE><TT><FONT COLOR="#990000">
	CProgressBar bar;
	
	bar.Create("Processing", 40, 1000);
	for (int i = 0; i < 1000; i++) {
	    // perform operation
	    bar.StepIt();
	}

	bar.SetText("Writing");
	for (int i = 0; i < 1000; i++) {
	    // perform operation
	    bar.StepIt();
	    PeekAndPump();	// Message pump
	}
	bar.Clear();
</FONT></TT></PRE>

<p>
In the above case, PeekAndPump() is a function which simply peeks and pumps
messages, allowing user interaction with the window during a lengthy process.
If the window size changes during the processing, the progress bar size will
alsow change.
</p>

<p><b>Source Code</b></p>

<PRE><TT><FONT COLOR="#990000">
	// ProgressBar.h : header file
	//

	#ifndef _INCLUDE_PROGRESSBAR_H_
	#define _INCLUDE_PROGRESSBAR_H_


	/////////////////////////////////////////////////////////////////////////////
	// CProgressBar -  status bar progress control
	//
	// Copyright (c) Chris Maunder, 1997
	// Please feel free to use and distribute.

	class CProgressBar: public CProgressCtrl
	// Creates a ProgressBar in the status bar
	{
	public:
	    CProgressBar();
	    CProgressBar(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE);
	    ~CProgressBar();
	    BOOL Create(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE);

	    DECLARE_DYNCREATE(CProgressBar)

	// operations
	public:
	    BOOL Success() {return m_bSuccess;}	// Was the creation successful?

	    void SetRange(int nLower, int nUpper, int nStep = 1);
	    void SetText(LPCTSTR strMessage) { m_strMessage = strMessage; Resize(); }
	    void SetSize(int nSize)          { m_nSize = nSize; Resize(); }
	    int  SetPos(int nPos);
	    int  OffsetPos(int nPos);
	    int  SetStep(int nStep);
	    int  StepIt();
	    void Clear();

	// Overrides
	    //{{AFX_VIRTUAL(CProgressBar)
	    //}}AFX_VIRTUAL

	// implementation
	protected:
	    BOOL		m_bSuccess;		// Successfully created?
	    int			m_nSize;		// Percentage size of control
	    CString		m_strMessage;	// Message to display to left of control
	
	    CStatusBar *GetStatusBar();
	    void Resize();
	
	// Generated message map functions
	protected:
	    //{{AFX_MSG(CProgressBar)
	    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	    //}}AFX_MSG
	    DECLARE_MESSAGE_MAP()
	
	};
	
	#endif
	/////////////////////////////////////////////////////////////////////////////


</FONT></TT></PRE>
	
<PRE><TT><FONT COLOR="#990000">	
	// ProgressBar.cpp : implementation file

	/////////////////////////////////////////////////////////////////////////////
	// CProgressBar -  status bar progress control
	//
	// Copyright (c) Chris Maunder (Chris.Maunder@cbr.clw.csiro.au), 1997
	// Please feel free to use and distribute.
		
	#include "stdafx.h"
	#include "ProgressBar.h"
	
	#ifdef _DEBUG
	#define new DEBUG_NEW
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#endif
	
	IMPLEMENT_DYNCREATE(CProgressBar, CProgressCtrl)
	
	BEGIN_MESSAGE_MAP(CProgressBar, CProgressCtrl)
	    //{{AFX_MSG_MAP(CProgressBar)
	    ON_WM_ERASEBKGND()
	    //}}AFX_MSG_MAP
	END_MESSAGE_MAP()
	
	CProgressBar::CProgressBar()
	{
	    m_bSuccess = FALSE;
	}
	
	CProgressBar::CProgressBar(LPCTSTR strMessage, int nSize /* = 100 */, 
	                           int MaxValue /* = 100 */, BOOL bSmooth /* = FALSE */)
	{
	    Create(strMessage, nSize, MaxValue, bSmooth);
	}
	
	CProgressBar::~CProgressBar()
	{
	    Clear();
	}
	
	CStatusBar* CProgressBar::GetStatusBar()
	{
	    CFrameWnd *pFrame = (CFrameWnd*)AfxGetMainWnd();
	    if (!pFrame || !pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd))) 
	        return NULL;
	
	    CStatusBar* pBar = (CStatusBar*) pFrame->GetMessageBar();
	    if (!pBar || !pBar->IsKindOf(RUNTIME_CLASS(CStatusBar))) 
	        return NULL;
	
	    return pBar;
	}
	
	// Create the CProgressCtrl as a child of the status bar positioned
	// over the first pane, extending "nSize" percentage across pane.
	// Sets the range to be 0 to MaxValue, with a step of 1.
	BOOL CProgressBar::Create(LPCTSTR strMessage, int nSize, int MaxValue, 
	                          BOOL bSmooth /*=FALSE*/)
	{
	    m_bSuccess = FALSE;
	    
	    CStatusBar *pStatusBar = GetStatusBar();
	    if (!pStatusBar) return FALSE;
	
		DWORD dwStyle = WS_CHILD|WS_VISIBLE;
	#ifdef PBS_SMOOTH	
		if (bSmooth)
			dwStyle |= PBS_SMOOTH;
	#endif
	
	    // Create the progress bar
	    VERIFY(m_bSuccess = CProgressCtrl::Create(dwStyle, CRect(0,0,0,0), pStatusBar, 1));
	    if (!m_bSuccess) return FALSE;
	
	    // Set range and step
	    SetRange(0, MaxValue);
	    SetStep(1);

	    m_strMessage = strMessage;
	    m_nSize      = nSize;
	
	    // Resize the control to its desired width
	    Resize();
	
	    return TRUE;
	}

	void CProgressBar::Clear()
	{
	    // Hide the window. This is necessary so that a cleared
	    // window is not redrawn if "Resise" is called
	    ModifyStyle(WS_VISIBLE, 0);
	
	    // Get the IDLE_MESSAGE
	    CString str;
	    str.LoadString(AFX_IDS_IDLEMESSAGE);
	
	    // Place the IDLE_MESSAGE in the status bar 
	    CStatusBar *pStatusBar = GetStatusBar();
	    if (pStatusBar) pStatusBar->SetWindowText(str);
	}
	
	void CProgressBar::SetRange(int nLower, int nUpper, int nStep /* = 1 */)	
	{
	    if (!m_bSuccess) return;
	    CProgressCtrl::SetRange(nLower, nUpper);
	    CProgressCtrl::SetStep(nStep);
	}
	
	int CProgressBar::SetPos(int nPos)	 
	{
	    ModifyStyle(0,WS_VISIBLE);
	    return (m_bSuccess)? CProgressCtrl::SetPos(nPos) : 0;
	}
	
	int  CProgressBar::OffsetPos(int nPos) 
	{ 
	    ModifyStyle(0,WS_VISIBLE);
	    return (m_bSuccess)? CProgressCtrl::OffsetPos(nPos) : 0;
	}
	
	int  CProgressBar::SetStep(int nStep)
	{ 
	    ModifyStyle(0,WS_VISIBLE);
	    return (m_bSuccess)? CProgressCtrl::SetStep(nStep) : 0;	
	}
	
	int  CProgressBar::StepIt()			 
	{ 
	    ModifyStyle(0,WS_VISIBLE);
	    return (m_bSuccess)? CProgressCtrl::StepIt() : 0;	
	}
	
	void CProgressBar::Resize() 
	{	
	    CStatusBar *pStatusBar = GetStatusBar();
	    if (!pStatusBar) return;
	
	    // Redraw the window text
	    if (::IsWindow(m_hWnd) && IsWindowVisible()) {
	        pStatusBar->SetWindowText(m_strMessage);
	        pStatusBar->UpdateWindow();
	    }
	
	    // Calculate how much space the text takes up
	    CClientDC dc(pStatusBar);
	    CFont *pOldFont = dc.SelectObject(pStatusBar->GetFont());
	    CSize size = dc.GetTextExtent(m_strMessage);		// Length of text
	    int margin = dc.GetTextExtent(_T(" ")).cx * 2;		// Text margin
	    dc.SelectObject(pOldFont);
	
	    // Now calculate the rectangle in which we will draw the progress bar
	    CRect rc;
	    pStatusBar->GetItemRect (0, rc);
	    rc.left = size.cx + 2*margin;
	    rc.right = rc.left + (rc.right - rc.left) * m_nSize / 100;
	    if (rc.right < rc.left) rc.right = rc.left;
	    
	    // Leave a litle vertical margin (10%) between the top and bottom of the bar
	    int Height = rc.bottom - rc.top;
	    rc.bottom -= Height/10;
	    rc.top    += Height/10;
	
	    // Resize the window
	    if (::IsWindow(m_hWnd))
	        MoveWindow(&rc);
	}
	
	BOOL CProgressBar::OnEraseBkgnd(CDC* pDC) 
	{
	    Resize();
	    return CProgressCtrl::OnEraseBkgnd(pDC);
	}
</FONT></TT></PRE>



<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; 1997 Zafir Anjum</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><IMG SRC="../cgi/Count.cgi-ft=2&dd=E-df=sts_progress_in_status2.cnt" tppabs="http://www.codeguru.com/cgi/Count.cgi?ft=2&dd=E%7cdf=sts_progress_in_status2.cnt" ALIGN="BOTTOM" BORDER="0"></CENTER>
</BODY>
</HTML>

⌨️ 快捷键说明

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