How to Use the SS control in VC ++

Source: Internet
Author: User
How to Use the Progress control in VC ++-general Linux technology-Linux programming and kernel information. The Progress control allows people to feel the Progress of an application execution. It can be used in many applications, but it is generally only supported in a single task, in Windows 98/NT Chinese operating system, in the VC ++ 6.0 environment, a very small application program is compiled using threads to implement the use of the Progress control. It supports multiple threads and is easy to use.

The source code of this application is as follows:

// ProgressDialog. h

Class CProgressDialog: public CDialog

{

// Construction

Public:

CProgressDialog (LPCSTR caption, BOOL

EnableCancel = TRUE, CWnd * pParent = NULL );

// Standard constructor

Virtual ~ CProgressDialog (void );

// Dialog Data

// {AFX_DATA (CProgressDialog)

Enum {IDD = IDD_PROGRESS_DIALOG };

CStatic

M_MessageStatic; // process Entry Title

CButton m_CancelButton; // stop button control key

CProgressCtrl

M_ProgressCtrl;

//} AFX_DATA

CString m_Caption;

// Dialog box title

BOOL m_EnableCancel; // the stop button display switch.

BOOL m_IsCancel; // whether to press the stop button

HANDLE m_Thread;

// Thread handle

Static dword winapi ThreadProc (CProgressDialog * dlg); // static thread

Void SetProgress (int percent) // sets the process location

{M_ProgressCtrl.SetPos (percent );}

Void SetMessage (LPCSTR msg) // you can specify the title of a process entry.

{M_MessageStatic.SetWindowText (msg );}

BOOL IsCancel (void)

{Return m_IsCancel ;}

Virtual DWORD ProgressProc () = 0; // pure virtual function in thread process

// 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 (LPCSTR caption, BOOL enableCancel, CWnd * pParent/* = NULL */):

CDialog (CProgressDialog: IDD, pParent)

{

// {AFX_DATA_INIT (CProgressDialog)

M_Caption = caption;

M_EnableCancel = 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, IDCANCEL, 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: OnInitDialog ()

{

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 unless you set the focus to a control

// EXCEPTION: OCX Property Pages shocould 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 here

If (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;

}

You only need to call the object of the CMyProgressDialog class where the Progress control is applied. You can modify DWORD CMyProgressDialog: ProgressProc () to adapt to the corresponding program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.