Talking about the use of the Progress control in VC
Dalian Jietong Computer Technology Co., Ltd. Wang Wei (Jtone@mail.dlptt.ln.cn)
The Progress control allows people to feel the progress of an application, it can be used in many applications, but usually only support in a single task, I am in a Windows 98 / NT Chinese operating system, in the VC 6.0 environment Under the use of threads, a very small application is used to implement the use of the Progress control. It can support multithreading, which is very convenient to use.
The following is the source code for this app:
//Progressdialog.h
Class CProgressDialog: Public CDialog
{
// construction
PUBLIC:
CPROGRESSDIALOG (LPCSTR CAPTION, BOOL
Enablecancel = true, cWnd * pParent = null;
// Standard Constructionor
Virtual ~ CProgressDialog (Void);
// Dialog Data
// {{AFX_DATA (CPROGRESSDIALOG)
ENUM {IDD = IDD_PROGRESS_DIALOG};
CSTATIC
m_MessageStatic; // Processes Title
Cbutton m_cancelbutton; // abort button control
CPROGRESSCTRL
M_ProgressCtrl;
//}} AFX_DATA
CSTRING M_CAPTION;
// Dialog Box Title
BOOL M_ENABLECANCEL; // Mediation Button Display Switch
Bool m_iscancel; // Mediation button Press the switch
Handle m_thread;
// thread handle
Static DWORD WINAPI ThreadProc (CPROGRESSDIALOG * DLG); // Static thread
Void SetProgress // Setting Procedure Location
{m_progressctrl.setpos (percent);
Void SetMessage (LPCSTR MSG) // Settings Processes Title
{m_MessageStatic.SetWindowText (MSG);
Bool Iscancel (Void)
{RETURN M_ICANCANCANCANCANCANCANCANCANCANCANCANCANCENCEL
Virtual dword progressProc () = 0; // thread process pure virtual function
// Overrides
// ClassWizard Generated Virtual Function Overrides
// {{AFX_VIRTUAL (CPROGRESSDIALOG)
protected:
Virtual Void DodataExchange (CDataExchange * PDX);
// DDX / DDV Support
//}} AFX_VIRTUAL
// Implementation
protected:
// generated message map functions
// {{AFX_MSG (CPROGRESSDIALOG)
Virtual Bool OnInitdialog ();
Virtual void oncancel ();
Virtual void onok ();
//}} AFX_MSG
Declare_message_map ()
}
Class CMYPROGRESSDIALOG: PUBLIC CPROGRESSDIALOG {
PUBLIC:
CMYPROGRESSDIALOG (LPCSTR CAPTION): CPROGRESSDIALOG (CAPTION) {}
Virtual dword progressproc (); // inheritance process
}
// ProgressDialog.cpp
#include "stdafx.h"
#include "progresstest.h" #include "progressdialog.h"
#ifdef _Debug
#define new debug_new
#undef this_file
Static char this_file [] = __file__;
#ENDIF
CPROGRESSDIALOG :: CProgressDialog (LPCSTDILOG (LPCSTR CAPTION, BOOL Enablecancel, CWnd * PParent / * = null * /):
CDialog (CProgressDialog :: IDD, PParent)
{
// {{AFX_DATA_INIT (CPROGRESSDIALOG)
M_CAPTION = CAPTION;
M_ENABLANCEL = EnableCancel;
m_iscancel = false;
m_thread = NULL;
//}} AFX_DATA_INIT
}
CPROGRESSDIALOG :: ~ CProgressDialog (void)
{
IF (m_thread) {
CloseHandle (M_THREAD);
}
}
Void
CPROGRESSDIALOG :: DODATAEXCHANGE (CDataExchange * PDX)
{
CDIALOG :: DODATAEXCHANGE (PDX);
// {{AFX_DATA_MAP (CPROGRESSDIALOG)
DDX_Control (PDX IDC_MESSAGE_STATIC, M_MESSAGESTATIC);
DDX_Control (PDX, IDCANCANCEL, M_CANCELBUTTON);
DDX_Control (PDX, IDC_Progress, M_ProgressCtrl);
//}} AFX_DATA_MAP
}
Begin_MESSAGE_MAP (CPROGRESSDIALOG, CDIALOG)
// {{AFX_MSG_MAP (CPROGRESSDIALOG)
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
// CProgressDialog Message HandlersBool CProgressDialog :: OnItDialog ()
{
CDIALOG :: OnInitdialog ();
// Todo: Add Extra Initialization Here
M_ProgressCtrl.setRange (0, 100);
m_progressctrl.setpos (0);
SetwindowText; M_CAption
IF (! m_enablecancel) {
m_cancelbutton.showwindow (sw_hide);
}
DWORD thREADID;
m_thread = CreateThread (null, 0,
(Lpthread_start_routine) CPROGRESSDIALOG :: Thread
Proc, (LPVOID) THIS, 0, & ThreadID;
Return True; // Return True UnsS you set the focus to a control
// Exception: OCX Property Pages SHOULD RETURN
False
}
DWORD WINAPI
CPROGRESSDIALOG :: ThreadProc (CPROGRESSDIALOG * DLG)
{
DWORD RET = DLG-> ProgressProc ();
DLG-> PostMessage (WM_COMMAND, IDOK);
Return Ret;
}
Void CProgressDialog :: oncancel ()
{
// Todo: add extra cleanup hered (m_enablecancel) m_iscancel = true;
}
Void CProgressDialog :: Onok ()
{
// Todo: Add extra validation here
WaitforsingleObject (m_thread, infinite);
CDIALOG :: Onok ();
}
DWORD CMYPROGRESSDIALOG :: ProgressProc ()
{
SetMessage ("Progress ...");
For (int i = 0; i <100; i ) {
IF (ISCANCEL ()) BREAK;
Sleep (100);
SetProgress (i);
}
Return 0;
}
Simply call the object of the CMYPROGRESSDIALOG class in places where the Progress control is applied. Deword CMYPROGRESSDIALOG :: ProgressProcressDialog :: ProgressProcressDialog :: ProgressProcressDialog :: ProgressProcressDialog :: ProgressProcressDialog :: ProgressProcressDialog :: ProgressProc () can be adapted to the corresponding program.