A multi-thread progress dialog source code

xiaoxiao2021-03-06  63

// Progdlg.h: header file // cg: this file was address by the program

/// CProgressDLG Dialog

#ifndef __PROGDLG_H __ # define __PROGDLG_H __ # include "resource.h" // LTclass CProgressDlg: public CDialog {// Construction / Destructionpublic: CProgressDlg (UINT nCaptionID = 0); // standard constructor ~ CProgressDlg ();

Bool create (CWND * pParent = NULL);

// Progress Dialog manipulation void SetStatus (LPCTSTR lpszMessage); void SetRange (int nLower, int nUpper); int SetStep (int nStep); int SetPos (int nPos); int OffsetPos (int nPos); int StepIt (); // Dialog data // {{AFX_DATA (CPROGRESSDLG) enum {IDD = cg_idd_progress}; canimateCtrl m_trans_animatectrl; cProgressCtrl M_Progress; //}} AFX_DATA

// Overrides // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CProgressDlg) public: virtual BOOL DestroyWindow (); protected: virtual void DoDataExchange (CDataExchange * pDX); // DDX / DDV support //}} AFX_VIRTUAL

//MplementationProtace: uint m_ncaptionid; int m_nlower; int m_nupper; int m_nstep; bool m_bparentdisabled;

Void reenableparent ();

Virtual void oncancel (); virtual void onok () {}; void updatePercent (int ncurrent); void pumpMessages ();

// generated message map functions // {{AFX_MSG (CProgressDLG) Virtual Bool OnInitdialog (); //}} AFX_MSG DECLARE_MESSAGE_MAP ()};

#ENDIF / / __PROGDLG_H __ // Progdlg.cpp: Implementation File // CG: This File Was Added by The Progress Dialog Component

#include "stdafx.h" #include "resource.h" #include "progdlg.h"

#ifdef _debug # undef this_filestatic char based_code this_file [] = __file __; # eDIF

/// CProgressDLG Dialog

CProgressDlg :: CProgressDlg (UINT nCaptionID) {m_nCaptionID = CG_IDS_PROGRESS_CAPTION; if (nCaptionID = 0!) M_nCaptionID = nCaptionID; m_nLower = 0; m_nUpper = 100; m_nStep = 10; // {{AFX_DATA_INIT (CProgressDlg) // NOTE: the ClassWizard Will Add Member Initialization Here //}} AFX_DATA_INIT M_BPARENTDISABED = FALSE;}

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 buy t. 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_TRANS_ANIMATE, m_Trans_AnimateCtrl); DDX_Control (pDX, CG_IDC_PROGDLG_PROGRESS, m_Progress); //}} AFX_DATA_MAP }

Begin_MESSAGE_MAP (CPROGRESSDLG, CDIALOG) / / {{AFX_MSG_MAP (CPROGRESSDLG) / /}} AFX_MSG_MAPEND_MESSAGE_MAP ()

void CProgressDlg :: SetStatus (LPCTSTR lpszMessage) {ASSERT (m_hWnd);. // Do not 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 () {}

void CProgressDlg :: SetRange (int nLower, int nUpper) {m_nLower = nLower; m_nUpper = nUpper; m_Progress.SetRange ((short) nLower, (short) nUpper);} int CProgressDlg :: SetPos (int nPos) {PumpMessages () INT IRESULT = M_Progress.SetPos (NPOS); UpdatePercent (NPOS); Return IResult;

INT CPROGRESSDLG :: setstep = nstep; // store for later us in calculating percentage return m_progress.setstep (nstep);

INT CPROGRESSDLG :: OffsetPos (INT NPOS) {pumpMessages (); int IRESULT = m_Progress.OffSetPos (NPOS); UpdatePOS (IRESULT NPOS); Return IRESULT;}

Int cprogressdlg :: step () {pumpMess (); int tent = m_progress.stepit (); UpdatePercent (IRESULT M_NSTEP); RETURN IRESULT;}

Void CProgressdlg :: pumpMessages () {// must call create () before using the dialog assert (m_hwnd! = null);

Msg Msg; // Handle Dialog Messages While (PEEKMESG, NULL, 0, 0, PM_Remove) {if (! Isdialogmessage (& msg)) {TranslateMessage (& MSG); DispatchMessage (& MSG);}}}

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_nUpperint nDividend = (nNewPos - m_nLower ); AskERT (ndividnd> = 0); // Current Position Should Be Greater Than M_Nlower

NPERCENT = NDIVIDEND * 100 / NDIVISOR;

// Since The Progress Control Wraps, We Well 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 :: OnItDialog () {cdialog :: OnNInitdialog (); m_progress.setRange (short) m_nlower, (short) m_nupper); m_progress.setstep; m_progress.setpos (m_nlower);

// cstring strcaption; // verify (strcaption.loadID)); // setWindowText (strcaption);

M_Trans_animateCtrl.Open ("Filemove.avi"); Return True;} Call Method: In the file you need to use #include "progdlg.h" CProgressDLG ProgressDLG; // Define Variables ProgressDlg.create (NULL); // Initialize ProgressDLG. SetRange (0,200); // Set the progress bar start and endpoint range progressdlg.setpos (0); // Set the progress bar start PROGRESSDLG.SETSTEP (1); // Set the progress bar each step PROGRESSDLG.SETSTATUS ("Tip Information"); Progressdlg.Stepit (); // Further Progressdlg.SetWindowText ("Dialog Title") before the progress bar;

转载请注明原文地址:https://www.9cbs.com/read-89267.html

New Post(0)