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

📄 vcg11.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<B>    RECT rect;  // Never really used! Default sizes used.</B>

<B>    rect.top = 0;</B>

<B>    rect.left = 0;</B>

<B>    rect.right = 100;</B>

<B>    rect.bottom = 60;</B>

<B>    m_ToolBar.Create(WS_CHILD | WS_VISIBLE | CCS_TOP, rect, this, 0);</B>

<B>    m_Buttons[0].iBitmap = 0;</B>

<B>    m_Buttons[0].idCommand = IDOK;</B>

<B>    m_Buttons[0].fsState = TBSTATE_ENABLED;</B>

<B>    m_Buttons[0].fsStyle = TBSTYLE_BUTTON;</B>

<B>    m_Buttons[0].dwData = 0;</B>

<B>    m_Buttons[0].iString = IDS_FIRST_BUTTON;</B>

<B>    m_Buttons[1].iBitmap = 1;</B>

<B>    m_Buttons[1].idCommand = IDCANCEL;</B>

<B>    m_Buttons[1].fsState = TBSTATE_ENABLED;</B>

<B>    m_Buttons[1].fsStyle = TBSTYLE_BUTTON;</B>

<B>    m_Buttons[1].dwData = 0;</B>

<B>    m_Buttons[1].iString = IDS_SECOND_BUTTON;</B>

<B>    m_ToolBar.AddButtons(2, m_Buttons);</B>

<B>    m_ToolBar.AddBitmap(2, IDB_TOOLBAR);</B>

<B>    CSize sizeButton(55, 65);</B>

<B>    m_ToolBar.SetBitmapSize(sizeButton);</B>

<B>    CSize sizeBitmap(48, 45);</B>

<B>    m_ToolBar.SetBitmapSize(sizeBitmap);</B>

<B>    m_ToolBar.AutoSize();</B>

    return TRUE;  // Return TRUE unless you set the focus to a control

                  // EXCEPTION: OCX Property Pages should return FALSE

}</FONT></PRE>

<P>Creating and implementing a toolbar by using CToolBarCtrl is more complex than creating and implementing a simple status bar. You must give the toolbar object information about each button, including the button's size and the bitmap size.

<BR>

<P>There are several steps to creating the toolbar. First, you must initialize your toolbar with a call to CToolBarCtrl::Create(). Then you must initialize the TBBUTTON array members as shown in Listing 11.6. After the TBBUTTON array has been initialized, it is then passed to CToolBarCtrl::AddButtons(). Finally, the bitmap for the buttons is given to the toolbar using a call to CToolBarCtrl::AddBitmap(). After the bitmap has been attached, the bitmap size and the button size are set, and a call is made to CToolBarCtrl::AutoSize() to resize the toolbar to fit the new sizes passed.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>You need to specify button and bitmap sizes only if your button bitmap isn't the same size as the default. For both CToolBar and CToolBarCtrl, the default size for a bitmap is 16x15 pixels. The default button size is 24x22 pixels. The button is larger than the bitmap to allow for three-dimensional borders on the button image.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E180"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Up-Down (Spinbox) Control</B></FONT></CENTER></H4>

<BR>

<P>The Up-Down control, usually called a <I>spinbox control</I> when combined with an edit control that displays the selected value, provides a handy way for users to enter or update a value when the normal action would be for the user to increment or decrement the value. The Up-Down control is implemented by using the CSpinButtonCtrl object.

<BR>

<P>Using the Up-Down control is simple. As with all the other Win32 Common Controls, you create a dialog box and place the Up-Down control somewhere on the dialog box. If you're creating a spinbox-type control, you also create an edit control and place the edit control next to the Up-Down control. Figure 11.7 shows the sample dialog box under development in the resource editor.

<BR>

<P><B><A HREF="11vcg07.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg07.gif">Figure 11.7. The Up-Down control in the resource editor.</A></B>

<BR>

<P>Figure 11.8 shows the Up-Down control in operation. This example shows the numeric value displayed in the edit control. Notice that when you use the standard font for the edit control (which is very large in this figure so that it will show up well), the number is too small to be seen well. If you're going to create a large edit control, you might want to use a larger typeface to make the displayed number more readable.

<BR>

<P><B><A HREF="11vcg08.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg08.gif">Figure 11.8. The spin control, using an Up-Down control.</A></B>

<BR>

<P>The edit control that is attached to the Up-Down control (logically, and usually physically) is referred to as the <I>buddy control</I>. This edit control is updated by the Up-Down control as the Up-Down control's value changes. Listing 11.7 shows the header file for the dialog box that contains the Up-Down control. This header file includes the definition of the Up-Down control, shown in bold. This code was added by using ClassWizard.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 11.7. The CSpinButtonCtrl initialization code.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">// updown.h : header file

//

/////////////////////////////////////////////////////////////////////////////

// UpDown dialog

class UpDown : public CDialog

{

// Construction

public:

    UpDown(CWnd* pParent = NULL);   // standard constructor

// Dialog data

    //{{AFX_DATA(UpDown)

    enum { IDD = IDD_UPDOWN };

<B>    CEdit    m_EditWnd;</B>

<B>    CSpinButtonCtrl    m_UpDown;</B>

    //}}AFX_DATA

// Overrides

    // ClassWizard-generated virtual function overrides

    //{{AFX_VIRTUAL(UpDown)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    //}}AFX_VIRTUAL

// Implementation

protected:

    // Generated message map functions

    //{{AFX_MSG(UpDown)

    virtual void OnOK();

    virtual BOOL OnInitDialog();

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};</FONT></PRE>

<P>The actual code to use the UpDown class is shown in Listing 11.8. This code shows how little programming is needed to create your spinbox control.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 11.8. Creating a CSpinButtonCtrl object.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">// updown.cpp : implementation file

//

#include &quot;stdafx.h&quot;

#include &quot;Win32CC.h&quot;

#include &quot;updown.h&quot;

#ifdef _DEBUG

#undef THIS_FILE

static char BASED_CODE THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// UpDown dialog

UpDown::UpDown(CWnd* pParent /*=NULL*/)

    : CDialog(UpDown::IDD, pParent)

{

    //{{AFX_DATA_INIT(UpDown)

    //}}AFX_DATA_INIT

}

void UpDown::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(UpDown)

    DDX_Control(pDX, IDC_EDIT1, m_EditWnd);

    DDX_Control(pDX, IDC_UPDOWN, m_UpDown);

    //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(UpDown, CDialog)

    //{{AFX_MSG_MAP(UpDown)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// UpDown message handlers

void UpDown::OnOK()

{

    // TODO: Add extra validation here

    int        nPosition;

    char    szBuffer[255];

// OK, user is done. Get the Trackbar's current value:

    nPosition = m_UpDown.GetPos();

    sprintf(szBuffer, &quot;Trackbar position = %d&quot;, nPosition);

    AfxMessageBox(szBuffer);

    CDialog::OnOK();

}

BOOL UpDown::OnInitDialog()

{

    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

<B>    m_UpDown.SetBuddy(&amp;m_EditWnd);</B>

<B>    m_UpDown.SetRange(10, 100);</B>

<B>    m_UpDown.SetPos(10);</B>

    return TRUE;  // Return TRUE unless you set the focus to a control

                  // EXCEPTION: OCX Property Pages should return FALSE

}</FONT></PRE>

<P>As Listing 11.8 shows (in bold), you assign the buddy control and then set the range of the Up-Down control as desired. Finally, you set the initial position. There are no defaults for the Up-Down control's range or desired position. It's good programming practice to initialize these values.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>When you're creating the Up-Down control in the dialog box, make sure that if you're using an automatically updated edit control, you select the Set Buddy Integer property. If this property isn't set, the edit control won't be automatically updated.

<BR>

<BR>Time I lost to this error: one hour!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E69E181"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Progress Indicator Control</B></FONT></CENTER></H4>

<BR>

<P>The Progress Indicator control gives feedback to the user about the progress of a lengthy operation. For example, an install program that must copy and process files from disks or a CD-ROM might use the Progress Indicator control to tell the user how far the process has progressed. This is better than simply using a wait cursor because the user will know that something is happening! The Progress Indicator is implemented by using CProgressCtrl.

<BR>

<P>Using a Progress Indicator control is easy. You can set a few simple parameters and then utilize the control. First, you must define the control in a dialog box. Then, as with other controls, use ClassWizard to bind a control object (of type CProgressCtrl) to the Progress Indicator control. Figure 11.9 shows a Progress Indicator control in a simple dialog box. This indicator is being updated with a timer loop to simulate a lengthy process.

<BR>

<P><B><A HREF="11vcg09.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/11vcg09.gif">Figure 11.9. The standard Progress Indicator control in an MFC application.</A></B>

<BR>

<P>The code to implement the CProgressCtrl object is rather simple. In the header file for the dialog box (or any other window in which you place a Progress Indicator control), you must define a member variable of CProgressCtrl type. In most cases, you can use ClassWizard to link the CProgressCtrl object to the dialog box's control. The CProgressCtrl definition in Listing 11.9 appears in bold.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 11.9. Creating a CProgressCtrl object.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">// progress.h : header file

//

/////////////////////////////////////////////////////////////////////////////

// Progress dialog

class Progress : public CDialog

{

// Construction

public:

    Progress(CWnd* pParent = NULL);   // Standard constructor

// Dialog data

    //{{AFX_DATA(Progress)

    enum { IDD = IDD_PROGRESS };

<B>    CProgressCtrl    m_Progress;</B>

    //}}AFX_DATA

// Overrides

    // ClassWizard-generated virtual function overrides

    //{{AFX_VIRTUAL(Progress)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    //}}AFX_VIRTUAL

// Implementation

protected:

    // Generated message map functions

    //{{AFX_MSG(Progress)

    virtual BOOL OnInitDialog();

    afx_msg void OnTimer(UINT nIDEvent);

    virtual void OnOK();

    virtual void OnCancel();

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};</FONT></PRE>

<P>Listing 11.10 shows the code used to initialize the CProgressCtrl control and to update the control. Lines that were added to the original AppWizard shell appear in bold. In this example, the CProgressCtrl is set to have a range of 0 to 100 and an increment of 1. A timer loop is then created, which increments the Progress Indicator control's display.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 11.10. Creating a CProgressCtrl object.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">// progress.cpp : implementation file

//

#include &quot;stdafx.h&quot;

#include &quot;Win32CC.h&quot;

#include &quot;progress.h&quot;

#ifdef _DEBUG

#undef THIS_FILE

static char BASED_CODE THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// Progress dialog

Progress::Progress(CWnd* pParent /*=NULL*/)

    : CDialog(Progress::IDD, pParent)

{

    //{{AFX_DATA_INIT(Progress)

        // NOTE: ClassWizard will add member initialization here

    //}}AFX_DATA_INIT

}

void Progress::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

⌨️ 快捷键说明

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