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

📄 vcg17.htm

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

<HEAD>

<TITLE>vcg17.htm </TITLE>

<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">

<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">

<LINK REL="Next" HREF="vcg18.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg18.htm">

<LINK REL="Previous" HREF="vcg16.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg16.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

<A NAME="I0"></A><P ALIGN=CENTER>

<A HREF="vcg16.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg16.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>

<A HREF="vcg18.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg18.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>

<HR ALIGN=CENTER>
<P>

<UL>

<UL>

<UL>

<LI>

<A HREF="#E68E86" >The Application User</A>

<LI>

<A HREF="#E68E87" >The Application Developer</A>

<LI>

<A HREF="#E68E88" >The OLE Control Developer</A>

<LI>

<A HREF="#E68E89" >Access and OLE Controls</A>

<LI>

<A HREF="#E68E90" >Using VARIANT Types</A>

<LI>

<A HREF="#E68E91" >Using OLE Controls in a Visual C++ 4 Project</A>

<UL>

<LI>

<A HREF="#E69E227" >Microsoft-Supplied OLE Controls</A>

<UL>

<LI>

<A HREF="#E70E94" >The Anibutton OLE Control</A>

<LI>

<A HREF="#E70E95" >The Grid OLE Control</A>

<LI>

<A HREF="#E70E96" >The Keystate OLE Control</A>

<LI>

<A HREF="#E70E97" >The Microsoft Communications OLE Control</A>

<LI>

<A HREF="#E70E98" >The Microsoft Masked Edit OLE Control</A>

<LI>

<A HREF="#E70E99" >The Multimedia OLE Control</A>

<LI>

<A HREF="#E70E100" >The PicClip OLE Control</A></UL></UL>

<LI>

<A HREF="#E68E92" >Summary</A></UL></UL></UL>

<HR ALIGN=CENTER>

<A NAME="E66E43"></A>

<H1 ALIGN=CENTER>

<CENTER>

<FONT SIZE=6 COLOR="#FF0000"><B>- 17 -</B>

<BR><B>Using OLE Controls and Automation with Visual C++ Applications</B></FONT></CENTER></H1>

<BR>

<P>Three different groups are involved with an OLE control: the OLE control developer, the application's developer, and the end user. Each has different objectives, wants, and needs. You were an OLE control developer when you developed the clock control in Chapter 16, &quot;Creating OLE Controls with Visual C++ 4.&quot; An OLE control developer creates a control that he or she might (or might not) use in an application. This person knows how the OLE control works but might have only incidental information about how the application's developer will actually use the OLE control.

<BR>

<P>The application's developer is the person who develops applications, perhaps using a system such as Microsoft Access. If this person is using Access, he or she will develop data entry forms, tables, reports, and so on. The forms and reports probably will need OLE controls to perform special functions. Just look at your clock control, which can be embedded in an Access data entry form. The application's developer might have some understanding of how the OLE control works internally but doesn't need to fully understand its inner workings in order to use it.

<BR>

<P>Finally, the end user has his own needs and wants. Often developers try to guess what the user will want and like. However, successful developers listen to the user and provide the features that he needs. If the user says he needs a clock in the data entry form, it's needed. The user is rarely interested in the mechanics of the OLE control as long as it works as intended.

<BR>

<BR>

<A NAME="E68E86"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>The Application User</B></FONT></CENTER></H3>

<BR>

<P>The application user wants an application that is easy to use and that works well. Microsoft can't provide all the types of controls that a typical application might need. Typical custom controls might include

<BR>

<UL>

<LI>Clocks and timer controls

<BR>

<BR>

<LI>Spreadsheet-type controls for data display

<BR>

<BR>

<LI>Data access controls that show the data from a data source

<BR>

<BR>

<LI>Specialty pushbutton controls (which typically have bitmap images)

<BR>

<BR>

</UL>

<BR>

<A NAME="E68E87"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>The Application Developer</B></FONT></CENTER></H3>

<BR>

<P>The application developer creates applications for the application user. This person might also be the OLE control developer, but that isn't always the case.

<BR>

<P>If the application developer is restricted to using just the controls that are supplied with Windows, many applications won't present a user interface with the needed functionality. When the application developer must use an OLE control, the control must be easy to use and have an attractive appearance. The control's look and feel must match both Windows and the application being created.

<BR>

<P>OLE controls for applications developers typically are more complex than those used by application users. Controls such as calendars, database grid controls, and graphing controls are all possible objects that application developers would use.

<BR>

<BR>

<A NAME="E68E88"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>The OLE Control Developer</B></FONT></CENTER></H3>

<BR>

<P>The OLE control developer is responsible for creating OLE controls that can be used by both the application developer and the application user. The OLE control developer must work with the application developer to create controls that can perform the needed tasks.

<BR>

<BR>

<A NAME="E68E89"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Access and OLE Controls</B></FONT></CENTER></H3>

<BR>

<P>At the time this book was written, Access was the most common end-user application that offered support for OLE controls. With Access, you can create an application that has OLE controls in forms and reports. As shown in Chapter 16, a control can easily be integrated into an Access form.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Because this isn't an Access book, I've skipped a number of minor details about custom controls in the following sections that use your Clock OLE control in an Access form. The skipped details will become self-evident when you add an OLE control to an Access form.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Now, see what you can do with your OLE control in an Access form. First, as Chapter 16 showed, your Clock OLE control is included in an Access form. Figure 17.1 reminds you what this form looks like.

<BR>

<P><B><A HREF="17vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg01.gif">Figure 17.1. The Clock OLE control in an Access form.</A></B>

<BR>

<P>Figure 17.1 shows the clock control in an Access form that is currently in user mode. There are two modes: user mode, in which the user interacts with the form, and design mode, in which the form is designed or modified. Notice that the time is displayed on the form.

<BR>

<P>You might remember that when you designed the clock control you added a few bells and whistles. One of these additions was an alarm function. Now your Access user wants to implement the alarm function to display a message box on-screen at a certain time. To do this, you must have two things:

<BR>

<UL>

<LI>A way to display the message when the clock control sends an Alarm event

<BR>

<BR>

<LI>A way to set the alarm attributes, such as the time for the alarm to go off

<BR>

<BR>

</UL>

<P>To display a message or take any other action when an OLE control fires an event, you must add an event handler for the OLE control. As the application developer, you should open the form in design mode and then follow these steps to add an event handler for the OLE control:

<BR>

<OL>

<LI>Right-click the OLE control. You see a pop-up menu that has a Build Event selection. When you select Build Event, you're presented with an event handler shell, shown in Figure 17.2.

<BR>

<BR><A HREF="17vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg02.gif">Figure 17.2. Clock's event handler for the alarm event.</A>

<BR>

<BR>

<LI>The Events window has an empty implementation of the OLE control's registered events. You can scroll through each of the event handlers using the Page Up and Page Down keys. Figure 17.2 shows the alarm event handler.

<BR>

<BR>Notice that the alarm event handler already has the three parameters (nHour, nMinute, and nSecond) that you defined when you created the clock control. Your function will be able to determine which alarm occurred if you decide to allow for multiple alarms in your handler.

<BR>

<BR>For your handler, you want a simple message box that displays a message saying that the alarm has occurred. To display a message box in Access Basic, you can use the MsgBox() function, as shown in Figure 17.3.

<BR>

<BR>

</OL>

<P><B><A HREF="17vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg03.gif">Figure 17.3. Clock's alarm function showing the MsgBox() call.</A></B>

<BR>

<P>After you've written your handler for the OLE control event, you can compile it by selecting Run | Compile Loaded Modules from Access. When it's compiled, save the code and test the clock's alarm function.

<BR>

<P>To test the alarm function, you must become the user and then set the clock's alarm time and enable the alarm. First, close the Design Form window to save the changes you've made to the form. Next, to set the alarm time and enable the alarm, use the control's property page. You included the necessary controls in the property sheet dialog box for just such an occasion. In order to access the clock's property page dialog box, the user must do the following:

<BR>

<OL>

<LI>Make the clock control the current, active control by clicking one of the clock's digits. The Clock OLE control doesn't provide any feedback to tell the user that it has input focus, but Access knows which control has focus.

<BR>

<BR>

<LI>From Access, the user must select Edit | Digital Clock Control Object | Properties, as shown in Figure 17.4. This displays the clock's property page dialog box, shown in Figure 17.5.

<BR>

<BR>

</OL>

<P><B><A HREF="17vcg04.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg04.gif">Figure 17.4. Menu selections to activate the clock's property page dialog box.</A></B>

<BR>

<P><B><A HREF="17vcg05.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg05.gif">Figure 17.5. The clock's property page dialog box in Access.</A></B>

<BR>

<P>When the alarm time has been set and the alarm has been enabled, simply wait for the alarm to occur. When this happens, the alarm message box is displayed, as shown in Figure 17.6. Needless to say, it's an alarming experience!

<BR>

<P><B><A HREF="17vcg06.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg06.gif">Figure 17.6. The clock control: It's alarming!</A></B>

<BR>

<P>With an OLE control, you can emulate many of the functions that are provided with other Access forms' objects. For example, you could have a custom button that supports animation (actually, the clock control is just such a control) or an OLE control button that displays information in a format that isn't possible using a standard control. An example of an OLE control that provides a unique display of information might be a &quot;gas gauge&quot; type of control that could be used to show the quantity of an item in stock for an inventory database system. Many cars have a &quot;low fuel&quot; light, so perhaps this control should have a &quot;low inventory level&quot; light.

<BR>

<BR>

<A NAME="E68E90"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Using <I>VARIANT</I> Types</B></FONT></CENTER></H3>

<BR>

<P>What is a VARIANT and why do I need one? These are often two of the first questions that new Visual C++ OLE programmers have. It's bad enough that we have to deal with short, long, int, float, and so on without adding another type to our list of variable types. Well, it gets even worse: the VARIANT type can actually be of virtually any type!

<BR>

<P>VARIANT types are used to mask the type of variable that will be passed to an OLE object so that the variable's type doesn't need to be known in advance. The VARIANT type object contains information (placed there at runtime, not design time) about the type of object that is contained in the VARIANT variable.

<BR>

<P>Table 17.1 lists the types of variables that can be placed in a VARIANT type. The TYPEDESC column describes the array's dimensions and the type of the array's elements. Perhaps you're wondering what property sets are. According to the original OLE 2.0 <I>Programmer's Reference</I> (Appendix B, page 636), &quot;property sets are tagged collections of values whose meaning (schema) is known to the code that manipulates them; that is, as much as that code needs to know the meaning.&quot; When arrays are passed by IDispatch::Invoke within VARIANTARGs, they are called <I>safe arrays</I>. Safe arrays contain information about the number of dimensions and bounds within them.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 17.1. VARIANT 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>Type</I>

</FONT>

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

<I>VARIANT</I>

</FONT>

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

<I>TYPEDESC</I>

</FONT>

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

<I>OLE </I><I>Property </I><I>Set</I>

</FONT>

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

<I>Safe </I><I>Array</I>

</FONT>

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

<I>Descriptio</I><I>n</I>

</FONT>

<TR>

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

VT_EMPTY

</FONT>

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

X

</FONT>

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



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

X

</FONT>

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



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

Nothing

</FONT>

<TR>

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

VT_NULL

</FONT>

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

X

</FONT>

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



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



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



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

SQL-style null

</FONT>

<TR>

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

VT_I2

</FONT>

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

X

</FONT>

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

X

</FONT>

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

X

</FONT>

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

X

</FONT>

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

2-byte signed int

</FONT>

<TR>

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

VT_I4

</FONT>

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

X

</FONT>

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

X

</FONT>

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

X

</FONT>

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

⌨️ 快捷键说明

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