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

📄 vcg17.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:

</BLOCKQUOTE></BLOCKQUOTE>

<P>A future set of imaging OLE controls from Wang looks interesting. The Wang Image Controls are free for both runtime and development. They are supposed to be included in future releases of Windows and Windows NT. The end user will get the controls in a package oriented around the Windows Fax Viewer application.

<BR>

<P>In the Component Gallery dialog box, select the OLE control that you want to include in your project and click the Insert button. You need to insert a control into a project only one time, even if you'll need to use the control in multiple locations. For example, if you wanted to include a Grid control in your project, you would select the Grid Control icon in the Component Gallery and click the Insert button. Then, whenever the dialog box editor were displayed, the toolbox would include a button for the Grid control. Figure 17.10 shows the dialog editor toolbox after you insert all the OLE controls for the sample program.

<BR>

<P><B><A HREF="17vcg10.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg10.gif">Figure 17.10. The dialog editor's toolbox with buttons for each OLE control.</A></B>

<BR>

<P>You can either add all the OLE controls that your project will use at one time or add them as needed. There's no advantage in adding all of them at the same time, other than convenience.

<BR>

<BR>

<A NAME="E70E94"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Anibutton OLE Control</B></FONT></CENTER></H5>

<BR>

<P>The Anibutton is used to display simple animation. You design bitmaps to display when various events occur.

<BR>

<P>The Anibutton OLE control is a rather useful button type control that uses an icon, bitmap, or metafile to define the appearance of a custom button control. Typical applications of the Anibutton OLE control include animated buttons, multistate buttons, and animated check boxes. The sample program creates a multistate button.

<BR>

<P>Each of your Anibutton OLE controls may have zero or more images. The Anibutton may also have an optional text caption. In most circumstances, the Anibutton OLE control may be considered to be simply a group of frames (defined with bitmaps, icons, or metafiles) that may be displayed in a given sequence.

<BR>

<P>The programmer uses the CCanibuton::SetPicture() property function to load images into the Anibutton OLE control. The CCanibuton::GetFrame() property indicates which picture is currently accessible through the CCanibuton::SetPicture() property. In other words, the CCanibuton::GetFrame() property is an index of the array of images in the control. You could use CCanibuton::SetFrame() to set the frame to be displayed if you want.

<BR>

<P>The images are displayed within the control's border. The default is to display the images in the center of the control, but you can use the CCanibuton::SetPictureXpos() and CCanibuton::SetPictureYpos() properties to position the image within the control. You can also use the CCanibuton::SetPictDrawMode() property to scale the image to the exact size of the control or to adjust the control to the size of your image.

<BR>

<P>The Anibutton's text can be displayed next to the images or on the images, depending on the CCanibuton::SetTextPosition() property.

<BR>

<P>There are a number of Anibutton OLE control types. The type of control is set either at design time by setting the Button type property or at runtime by calling the CCanibuton::SetCycle() property. The values listed in Table 17.2 are the valid styles for the Anibutton OLE control.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 17.2. Anibutton OLE control cycle types.</B></FONT></CENTER>

<BR>



<CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Button Type</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Cycle</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Animated (1/2 and 1/2)

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

0

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Each time the left mouse button is clicked, the first half of the frames are displayed in order. When the left button is released, the remaining frames are displayed in order. After the frames are displayed, the Anibutton OLE control returns to the first frame. If the Anibutton OLE control has only two frames defined, this type is referred to as an Enhanced button style.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Multistate

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

1

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Each frame is used to specify a given state. There will be as many states as there are frames in the Anibutton OLE control. Each time the left button is clicked, the Anibutton OLE control automatically switches to the next state and displays the appropriate frame. If the Anibutton OLE control has only two frames defined, this type is referred to as an Enhanced check box style.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

2-state animated (two state 1/2 and 1/2)

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

2

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The first time the left mouse button is clicked, the first half of the frames are displayed in order. The Anibutton OLE control's state changes to the second state. When the left button is clicked for the second time, the remaining frames are displayed in order. The state is then returned to the first state. After the frames are displayed, the Anibutton OLE control returns to the first frame.</FONT>

</TABLE></CENTER><P><A NAME="I2"></A>There are no real limits to the size of the Anibutton OLE control. Usually it's sized to be comparable to other dialog button type controls. However, you can use large bitmaps in the Anibutton OLE control if you want. The bitmaps and icons used by the Anibutton OLE control don't use many Windows resources. This data is stored in global memory using a private format. The Anibutton OLE control won't use Windows bitmap or icon resource handles. Microsoft has even suggested that the Anibutton can be used as a method of storing or archiving bitmaps or icons.

<BR>

<P>To use the Anibutton OLE control, first install it into your project. Once it's installed, your dialog editor tool palette (or toolbox) will have a new button for the Anibutton OLE control; it will have a picture of a small piece of film. (See Figure 17.10.) Next, open the dialog box that will receive the Anibutton OLE control and locate your control. After you've located, sized, and named the control, you should set whatever properties you want (including defining the frame bitmaps) for this control.

<BR>

<P>Figure 17.11 shows the sample Anibutton OLE control, along with the Control Properties page. Notice that I've chosen to use the multistate type for this control.

<BR>

<P><B><A HREF="17vcg11.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg11.gif">Figure 17.11. The Anibutton OLE control located in a dialog, along with the property sheet.</A></B>

<BR>

<P>After you've created your control in the dialog editor, start ClassWizard (while the dialog has focus, press Ctrl-W) and bind a CCanibuton class variable to the control. The example uses a variable name of m_Anibutton for this variable, as shown in Figure 17.12. Once the CCanibuton class variable is bound to the control, you can work with the control in the dialog handler class, as shown in Listing 17.2. The added lines appear in bold.

<BR>

<P><B><A HREF="17vcg12.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg12.gif">Figure 17.12. The Anibutton OLE control bound CCanibuton variable in ClassWizard.</A></B>

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 17.2. The AnibuttonDlg.CPP dialog box handler file.</B></FONT>

<BR>

<PRE>

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

//

#include &quot;stdafx.h&quot;

#include &quot;OLE Controls Demo.h&quot;

#include &quot;AnibuttonDlg.h&quot;

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

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

// CAnibuttonDlg dialog

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

    : CDialog(CAnibuttonDlg::IDD, pParent)

{

    //{{AFX_DATA_INIT(CAnibuttonDlg)

        // NOTE: the ClassWizard will add member initialization here

    //}}AFX_DATA_INIT

}

void CAnibuttonDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CAnibuttonDlg)

    DDX_Control(pDX, IDC_ANIPUSHBUTTON1, m_Anibutton);

    //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAnibuttonDlg, CDialog)

    //{{AFX_MSG_MAP(CAnibuttonDlg)

        // NOTE: the ClassWizard will add message map macros here

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

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

// CAnibuttonDlg message handlers

BEGIN_EVENTSINK_MAP(CAnibuttonDlg, CDialog)

    //{{AFX_EVENTSINK_MAP(CAnibuttonDlg)

    ON_EVENT(CAnibuttonDlg, IDC_ANIPUSHBUTTON1, 1

        /* Click */, OnClickAnipushbutton1, VTS_NONE)

    //}}AFX_EVENTSINK_MAP

END_EVENTSINK_MAP()

void CAnibuttonDlg::OnClickAnipushbutton1()

{

    // TODO: Add your control notification handler code here

<B>    // This function is called each time the button is clicked</B>

<B>    // by the user. You can use this function to determine the</B>

<B>    // button's state:</B>

<B>    switch(m_Anibutton.Get_Value())</B>

<B>    {</B>

<B>        case 0:</B>

<B>            MessageBox(&quot;Value is zero&quot;);</B>

<B>            break;</B>

<B>        case 1:</B>

<B>            MessageBox(&quot;Value is one&quot;);</B>

<B>            break;</B>

<B>        case 2:</B>

<B>            MessageBox(&quot;Value is two&quot;);</B>

<B>            break;</B>

<B>        case 3:</B>

<B>            MessageBox(&quot;Value is three&quot;);</B>

<B>            break;</B>

<B>        case 4:</B>

<B>            MessageBox(&quot;Value is four&quot;);</B>

<B>            break;</B>

<B>        default:</B>

<B>            MessageBox(&quot;Value is undefined!&quot;);</B>

<B>            break;</B>

<B>    }</B>

}</FONT></PRE>

<P>You only needed to add code to receive the Anibutton OLE control's clicks and report the state to the user. A typical application probably would set a member variable to reflect the Anibutton OLE control's state and process this information as appropriate.

<BR>

<BR>

<A NAME="E70E95"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Grid OLE Control</B></FONT></CENTER></H5>

<BR>

<P>The Grid OLE control looks much like a spreadsheet. It has addressable locations arranged in a grid. You can have title columns and rows.

<BR>

<P>With the Grid OLE control, you can put either text or a picture in any cell. You need to use the Row and Col properties to specify the current cell in a grid. The two Visual C++ functions that do this are CGridCtrl::SetRow() and CGridCtrl::SetCol(). However, a large number of functions are available in the CGridCtrl class to manipulate your Grid OLE control. When you initialize your Grid OLE control, you may specify the current Row and Col. At runtime, the user can change the current Row and Col using either the keyboard or the mouse. Your application can obtain the contents of the current cell using the CGridCtrl::GetText() property function.

<BR>

<P>The Grid OLE control uses text wrapping if a cell's text won't fit within the currently defined columns. If a column isn't sufficiently wide, the Grid OLE control won't extend it automatically like Excel does. When text is wrapped, the cell must be made wider or taller to let the user read all the text. At runtime, the user can modify cells' height and width. If you want, your application can query the cells' width and save these values to be used the next time the control is used. (To see a grid control saving column widths, refer to the application called Contin using Grid Control in Chapter 15, &quot;Designing Online Transaction-Processing Applications.&quot;)

<BR>

<P>You can set the number of Cols and Rows in your Grid OLE control at design time, or you can use the CGridCtrl::SetCols() and CGridCtrl::SetRows() properties to set the number of columns and rows in a Grid control at runtime. The Grid OLE control can't have more than 16,352 rows and 5,450 columns.

<BR>

<P>To use the Grid OLE control, first install it into your project. Once it's installed, your dialog editor tool palette (or toolbox) will have a new button for the Grid OLE control; it will have a picture of a small grid. (See Figure 17.10.) Next, open the dialog box that will receive the Grid OLE control and locate your control. After you've located, sized, and named the control, you should set whatever properties you want for this control.

<BR>

<P>Figure 17.13 shows the sample Grid OLE control, along with the Control Properties page.

<BR>

<P><B><A HREF="17vcg13.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg13.gif">Figure 17.13. The Grid OLE control located in a dialog, along with the property sheet.</A></B>

<BR>

<P>After you've created your control in the dialog editor, start ClassWizard (while the dialog has focus, press Ctrl-W) and bind a CGridCtrl class variable to the control. The example uses a variable name of m_GridCtrl for this variable, as shown in Figure 17.14. Once the CGridCtrl class variable is bound to the control, you can work with the control in the dialog handler class, as shown in Listing 17.3.

<BR>

<P><B><A HREF="17vcg14.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg14.gif">Figure 17.14. The Grid OLE control bound CGridCtrl variable in ClassWizard.</A></B>

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 17.3. The GridDlg.CPP dialog box handler file.</B></FONT>

<BR>

<PRE>

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

//

#include &quot;stdafx.h&quot;

#include &quot;OLE Controls Demo.h&quot;

#include &quot;GridDlg.h&quot;

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

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

// CGridDlg dialog

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

    : CDialog(CGridDlg::IDD, pParent)

{

    //{{AFX_DATA_INIT(CGridDlg)

        // NOTE: the ClassWizard will add member initialization here

    //}}AFX_DATA_INIT

}

void CGridDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CGridDlg)

    DDX_Control(pDX, IDC_GRID1, m_Grid);

    //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CGridDlg, CDialog)

    //{{AFX_MSG_MAP(CGridDlg)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

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

// CGridDlg message handlers

BOOL CGridDlg::OnInitDialog()

{

    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

<B>    int Column = 0;</B>

<B>    int Row = 0;</B>

<B>    CString Formatted;</B>

<B>    // Fill in the Grid control's column titles:</B>

<B>    m_Grid.SetRow(0);</B>

<B>    for (Column = 1; Column &lt; m_Grid.GetCols(); ++Column)</B>

<B>    {// Fill only the predetermined number of columns...</B>

<B>        m_Grid.SetCol(Column);</B>

<B>        Formatted.Format(_T(&quot;Col %d&quot;), Column);</B>

<B>        m_Grid.SetText(Formatted);</B>

<B>    }</B>

<B>    // Fill in the Grid control's row titles:</B>

<B>    m_Grid.SetCol(0);</B>

<B>    for (Row = 1; Row &lt; m_Grid.GetRows(); ++Row)</B>

<B>    {// Fill only the predetermined number of columns...</B>

<B>        m_Grid.SetRow(Row);</B>

<B>        Formatted.Format(_T(&quot;Row %d&quot;), Row);</B>

<B>        m_Grid.SetText(Formatted);</B>

<B>    }</B>

<B>    // Fill in the Grid control's data area.</B>

<B>    // See Chapter 15 for an expanded example of how to</B>

<B>    // use the Grid control in an application.</B>

<B>    for (Row = 1; Row &lt; m_Grid.GetRows(); ++Row)</B>

<B>    {// Fill only the predetermined number of rows...</B>

<B>        m_Grid.SetRow(Row);</B>

<B>        for (Column = 1; Column &lt; m_Grid.GetCols(); ++Column)</B>

<B>        {// Fill only the predetermined number of columns...</B>

<B>            m_Grid.SetCol(Column);</B>

<B>            Formatted.Format(_T(&quot;R%dC%d&quot;), Row, Column);</B>

<B>            m_Grid.SetText(Formatted);</B>

<B>        }</B>

<B>    }</B>

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

                  // EXCEPTION: OCX Property Pages should return FALSE

}</FONT></PRE>

<P>You needed to add code to initialize the Grid OLE control's cells. This control was created with fixed top and left side columns, used for column and row titles. You fill the title column and row first and then fill in the data area.

<BR>

<BR>

<A NAME="E70E96"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Keystate OLE Control</B></FONT></CENTER></H5>

<BR>

<P>The Keystate control provides user feedback on the state of a number of keys, including Caps Lock, Num Lock, Insert, and Scroll Lock. Your application also can query the state of these keys using the Keystate OLE control. You use one Keystate OLE control for each key whose state is to be monitored.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Although Microsoft doesn't mention this, having two Keystate controls can lead to interesting results. Once I had the sample program running and the Keystate OLE control open in design mode at the same time. Whenever I pressed the Caps Lock key, the light would constantly flash at me. Interesting, but not quite the desired effect!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The Style property determines which key the control affects. A control can affect only one key at a time. A

⌨️ 快捷键说明

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