📄 vcg11.htm
字号:
//{{AFX_DATA_MAP(Progress)
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Progress, CDialog)
//{{AFX_MSG_MAP(Progress)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Progress message handlers
BOOL Progress::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
<B> m_Progress.SetRange(0, 100); // Default values!</B>
<B> m_Progress.SetPos(0); // Default value!</B>
<B> m_Progress.SetStep(1); // Default is 10</B>
SetTimer(999, 100, NULL);
return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void Progress::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// Increment our progress. (When we exceed 100%,
// CProgressCtrl will reset the indicator to 0!)
// If this action is not desired, you must track
// the current position and call CProgressCtrl::SetPos()
// to set the Progress bar's position.
<B> m_Progress.StepIt();</B>
CDialog::OnTimer(nIDEvent);
}
void Progress::OnOK()
{
// TODO: Add extra validation here
KillTimer(999);
CDialog::OnOK();
}
void Progress::OnCancel()
{
// TODO: Add extra cleanup here
KillTimer(999);
CDialog::OnCancel();
}</FONT></PRE>
<P>The initialization process consists of this code:
<BR>
<PRE>
<FONT COLOR="#000080"><B>m_Progress.SetRange(0, 100); // Default values!</B>
<B>m_Progress.SetPos(0); // Default value!</B>
<B>m_Progress.SetStep(1); // Default is 10</B></FONT></PRE>
<P>The code to update the Progress Indicator control is a simple call to the member function StepIt(), which will increment the control's indicator by the step value defined when the control is initialized.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>The default range is 0 to 100, and the step value is 10. This can create a choppy-acting control. Generally, you will want to increment the control in about one-percent steps.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E182"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The Trackbar Control</B></FONT></CENTER></H4>
<BR>
<P>A Trackbar control lets the user input an inexact value within a predefined range of values. Typical uses for Trackbar controls include multimedia volume and mixer controls and other pseudo-analog applications.
<BR>
<P>A Trackbar control is easy to implement. You first define the Trackbar control in a dialog box, and then you use ClassWizard to attach a CSliderCtrl object to the Trackbar control. The sample application in Figure 11.10 shows a simple Trackbar control. This control is a minimum implementation of the Trackbar control, with index marks (called <I>tick </I><I>marks</I>) on the trackbar.
<BR>
<P><B><A HREF="11vcg10.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg10.gif">Figure 11.10. The standard Trackbar control in an MFC application.</A></B>
<BR>
<P>The code to implement a basic CSliderCtrl object is simple. In the header file for the dialog box (or in any other window), you must define a member variable of CSliderCtrl type, which is usually done by using ClassWizard. This line appears in bold.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.11. Creating a CSliderCtrl object.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// trackbar.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// TrackBar dialog
class TrackBar : public CDialog
{
// Construction
public:
TrackBar(CWnd* pParent = NULL); // Standard constructor
// Dialog data
//{{AFX_DATA(TrackBar)
enum { IDD = IDD_TRACKBAR };
<B> CSliderCtrl m_Slider;</B>
//}}AFX_DATA
// Overrides
// ClassWizard-generated virtual function overrides
//{{AFX_VIRTUAL(TrackBar)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(TrackBar)
virtual BOOL OnInitDialog();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};</FONT></PRE>
<P>Listing 11.12 is the actual implementation of a CSliderCtrl object. This code implements the minimum programming needed to create a usable control. You could also modify the tick mark frequency and add numeric indicators at each end of the slider control to show minimum and maximum values.
<BR>
<P>The bold code in Listing 11.12 shows how you set both the range of the slider control and the display of tick marks that assist the user in determining just where the slider is positioned.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.12. Creating a CSliderCtrl object.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// trackbar.cpp : implementation file
//
#include "stdafx.h"
#include "win32cc.h"
#include "trackbar.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// TrackBar dialog
TrackBar::TrackBar(CWnd* pParent /*=NULL*/)
: CDialog(TrackBar::IDD, pParent)
{
//{{AFX_DATA_INIT(TrackBar)
// NOTE: ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void TrackBar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(TrackBar)
DDX_Control(pDX, IDC_TRACKBAR, m_Slider);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(TrackBar, CDialog)
//{{AFX_MSG_MAP(TrackBar)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// TrackBar message handlers
BOOL TrackBar::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
<B> // Set the slider's range from 10 to 100</B>
<B> m_Slider.SetRange(10, 100);</B>
<B> // Set the slider's ticks to every tenth point</B>
<B> m_Slider.SetTicFreq(10);</B>
return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void TrackBar::OnOK()
{
// TODO: Add extra validation here
<B> int nPosition;</B>
<B> char szBuffer[255];</B>
<B>// OK, user is done. Get the Trackbar's current value:</B>
<B> nPosition = m_Slider.GetPos();</B>
<B> sprintf(szBuffer, "Trackbar position = %d", nPosition);</B>
<B> AfxMessageBox(szBuffer);</B>
CDialog::OnOK();
}</FONT></PRE>
<BR>
<A NAME="E69E183"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The Header Control</B></FONT></CENTER></H4>
<BR>
<P>The Header control is used by the ListView control to display column headings when data is being displayed in the report view. There are bound to be other implementations for the Header control in the future, but currently there is only limited usage for the Header control as a stand-alone control in a dialog box. The Header control is automatically implemented by the ListView control.
<BR>
<BR>
<A NAME="E69E184"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The ListView Control</B></FONT></CENTER></H4>
<BR>
<P>The ListView control is one of the most complex controls implemented by the Win32 Common Controls. This control supports four different views of the data:
<BR>
<UL>
<LI>Large Icon View displays the data as icons. These icons have multiline titles below them and are large and easy to select. Far fewer data objects can be represented in the Large Icon View than can be displayed by using the Small Icon View. The detail of the icon displayed in the Large Icon View is very detailed, however.
<BR>
<BR>
<LI>Small Icon View displays the data as small icons followed by a single line of title text. There can be multiple columns of small icons; the number of columns is determined by the size of the ListView control.
<BR>
<BR>
<LI>List View is a lot like the Small Icon View, except that only one column is presented, and the limitations on the title text length are not as severe.
<BR>
<BR>
<LI>Report View lets the user see multiple columns of data. Much like a spreadsheet view, the user can sort on any column (if you implement this capability). The program can set the width of the columns, and the user can resize a column at any time.
<BR>
<BR>
</UL>
<P>The sample program in Figure 11.11 shows the Report View mode of the ListView control. The Report View mode might be the most important mode that this control offers, and it's the most difficult mode to implement. Figure 11.11 also shows the ListView control displaying a set of columnar data. This data is from a simple table contained in the program; however, in database programs, the data can be retrieved from a datasource.
<BR>
<P><B><A HREF="11vcg11.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg11.gif">Figure 11.11. The ListView control in Report View mode.</A></B>
<BR>
<P>The code to implement the CListCtrl object (see Listing 11.13) is usually simple. In the header file for the dialog box (or any other window), you must define a member variable of CListCtrl type. You can use ClassWizard to do this. This line appears in bold.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 11.13. Creating a CListCtrl object.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">// listview.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// ListView dialog
class ListView : public CDialog
{
// Construction
public:
ListView(CWnd* pParent = NULL); // Standard constructor
// Dialog data
//{{AFX_DATA(ListView)
enum { IDD = IDD_LISTVIEW };
<B> CListCtrl m_ListView;</B>
//}}AFX_DATA
// Overrides
// ClassWizard-generated virtual function overrides
//{{AFX_VIRTUAL(ListView)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(ListView)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};</FONT></PRE>
<P>The real work is done in the source file, where the ListView control must be initialized and have the data adde
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -