📄 vcg17.htm
字号:
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
VT_CF
</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>
X
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Clipboard format
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
VT_CLSID
</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>
X
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Class ID
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
VT_VECTOR
</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>
X
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Simple counted array
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
VT_ARRAY
</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>
SAFEARRAY*
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
VT_BYREF
</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>
The object is a pointer</FONT>
</TABLE></CENTER><P>The VARIANT type works by stuffing all these types into a single union object, as shown in Listing 17.1.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 17.1. The VARIANT type.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">struct tagVARIANT{
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union
{
long lVal; /* VT_I4 */
unsigned char bVal; /* VT_UI1 */
short iVal; /* VT_I2 */
float fltVal; /* VT_R4 */
double dblVal; /* VT_R8 */
VARIANT_BOOL bool; /* VT_BOOL */
SCODE scode; /* VT_ERROR */
CY cyVal; /* VT_CY */
DATE date; /* VT_DATE */
BSTR bstrVal; /* VT_BSTR */
IUnknown *punkVal; /* VT_UNKNOWN */
IDispatch *pdispVal; /* VT_DISPATCH */
SAFEARRAY *parray; /* VT_ARRAY|* */
unsigned char *pbVal; /* VT_BYREF|VT_UI1 */
short *piVal; /* VT_BYREF|VT_I2 */
long *plVal; /* VT_BYREF|VT_I4 */
float *pfltVal; /* VT_BYREF|VT_R4 */
double *pdblVal; /* VT_BYREF|VT_R8 */
VARIANT_BOOL *pbool; /* VT_BYREF|VT_BOOL */
SCODE *pscode; /* VT_BYREF|VT_ERROR */
CY *pcyVal; /* VT_BYREF|VT_CY */
DATE *pdate; /* VT_BYREF|VT_DATE */
BSTR *pbstrVal; /* VT_BYREF|VT_BSTR */
IUnknown **ppunkVal; /* VT_BYREF|VT_UNKNOWN */
IDispatch **ppdispVal; /* VT_BYREF|VT_DISPATCH */
SAFEARRAY **pparray; /* VT_BYREF|VT_ARRAY|* */
VARIANT *pvarVal; /* VT_BYREF|VT_VARIANT */
void * byref; /* Generic ByRef */
}
#if(defined(NONAMELESSUNION))
u
#endif
;
};</FONT></PRE>
<P>Perhaps you're thinking that there must be an easier way. A VARIANT has things you've never seen before, such as DATE and BSTR. Well, there is help. The MFC class COleVariant comes to the rescue, making management of the VARIANT type easier. The COleVariant class has the following members and operations:
<BR>
<UL>
<LI>COleVariant, which constructs a COleVariant object
<BR>
<BR>
<LI>ChangeType(), which changes the variant type of this COleVariant object
<BR>
<BR>
<LI>Clear(), which clears the COleVariant object
<BR>
<BR>
<LI>Detach(), which detaches a VARIANT object from the COleVariant class and returns the VARIANT
<BR>
<BR>
<LI>The LPCVARIANT operator, which converts a COleVariant value into an LPCVARIANT
<BR>
<BR>
<LI>The LPVARIANT operator, which converts a COleVariant object into an LPVARIANT
<BR>
<BR>
<LI>The = operator, which copies a COleVariant value
<BR>
<BR>
<LI>The == operator, which compares two COleVariant values
<BR>
<BR>
<LI>The << operator, which outputs a COleVariant value to CArchive or CDumpContext
<BR>
<BR>
<LI>The >> operator, which inputs a COleVariant object from CArchive
<BR>
<BR>
</UL>
<P>For an example of how to use the COleVariant class object, refer to the Microsoft Communications OLE control example later in this chapter. The Microsoft Communications OLE control uses a bstr VARIANT object to pass characters to be written out of the communications port.
<BR>
<P>Generally, you will want to use COleVariant in your MFC applications. It's easier to use, and it will provide a more programmer-friendly application.
<BR>
<BR>
<A NAME="E68E91"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Using OLE Controls in a Visual C++ 4 Project</B></FONT></CENTER></H3>
<BR>
<P>This part of the chapter discusses the issues in using OLE controls in a Visual C++ application, describes the OLE controls supplied with Visual C++ 4, and describes using the Clock OLE control.
<BR>
<P>First, let's revel in the fact that Visual C++ 4 is the first release of Visual C++ that actually lets you use an OLE control. You've been able to develop OLE controls for some time, but a Visual C++ program hasn't been able to use OLE controls itself. You could make them, but you couldn't use them.
<BR>
<P>Now, with Visual C++ 4, you can include OLE controls on dialog boxes. When you create an MFC application's project, the OLE page has a check box that you would check if you planned to use OLE controls in your project, as shown in Figure 17.7. If by chance you forgot to check this box, or you're importing an existing project from an earlier version of Visual C++, you can add this capability by selecting Insert | Component. The dialog box shown in Figure 17.8 appears.
<BR>
<P><B><A HREF="17vcg07.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg07.gif">Figure 17.7. An MFC project's OLE properties wizard page.</A></B>
<BR>
<P><B><A HREF="17vcg08.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg08.gif">Figure 17.8. Adding OLE control support to existing projects.</A></B>
<BR>
<P>You can easily use OLE controls in Visual C++ dialog boxes and in other locations as well—typically anywhere where you might create a window. There is no hard and fast rule that says an OLE control must have a window. Later in this chapter you'll see two controls from Microsoft that don't create any window when used in execute mode.
<BR>
<BR>
<A NAME="E69E227"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Microsoft-Supplied OLE Controls</B></FONT></CENTER></H4>
<BR>
<P>Microsoft supplies with Visual C++ 4 a set of OLE controls. You will create a simple example of each of these controls using the sample program for this chapter. The supplied OLE controls include the following:
<BR>
<UL>
<LI>The Anibutton is used to display simple animation. You design bitmaps to display when various events occur.
<BR>
<BR>
<LI>The Grid control looks much like a spreadsheet, with addressable locations arranged in a grid. You can have title columns and rows.
<BR>
<BR>
<LI>The Keystate control provides feedback to the user on the state of a number of keys, such as the Caps Lock key.
<BR>
<BR>
<LI>The Microsoft Communications control is used to manipulate communications ports. Ports can be configured and I/O can be performed using this control. This control is an example of an OLE control that isn't visible in run mode.
<BR>
<BR>
<LI>The Microsoft Masked Edit control lets you accept and display formatted input in your application. A typical example is the obtaining of a telephone number.
<BR>
<BR>
<LI>The Microsoft Multimedia control lets your application display and manipulate multimedia clips, such as .AVI files.
<BR>
<BR>
<LI>The PicClip control lets your application obtain a portion of a bitmap easily. This control is an example of an OLE control that isn't visible in run mode.
<BR>
<BR>
</UL>
<P>In addition to these fully supported OLE controls, Visual C++ 4 also includes as sample applications a number of OLE controls. These controls aren't supported by Microsoft (and might not work as expected), but because they're available in source format, they can be quite valuable:
<BR>
<UL>
<LI>BUTTON: A simple button control.
<BR>
<BR>
<LI>CIRC1: Part one of the OLE control tutorial project.
<BR>
<BR>
<LI>CIRC2: Part two of the OLE control tutorial project.
<BR>
<BR>
<LI>CIRC3: Part three of the OLE control tutorial project.
<BR>
<BR>
<LI>DB: A database access control. Lets you access a single column in a table.
<BR>
<BR>
<LI>LICENSED: An example of using licensing of OLE controls.
<BR>
<BR>
<LI>LOCALIZE: An example of a localized control.
<BR>
<BR>
<LI>PAL: An example of a color palette manipulation control.
<BR>
<BR>
<LI>PUSH: An example of a customized pushbutton control.
<BR>
<BR>
<LI>SMILEY: A smiley-face pushbutton.
<BR>
<BR>
<LI>SPINDIAL: A spindial example.
<BR>
<BR>
<LI>TIME: A timer-based control. Basically, an alarm function.
<BR>
<BR>
<LI>XLIST: An OLE control that subclasses a Windows list box.
<BR>
<BR>
</UL>
<P>In this chapter you will create a project to illustrate each of the Visual C++ 4 supplied OLE controls. However, none of the sample controls is displayed in the sample program. This chapter uses a single project with seven dialog boxes (one for each of the seven OLE controls) to demonstrate the use of these OLE controls.
<BR>
<P>To include an OLE control in your project (assuming that your project supports OLE controls), choose Insert | Component. Doing so will display a dialog box that has a tab for OLE controls (usually the second tab), as shown in Figure 17.9. You will notice a few additional controls, including the digital clock control from Chapter 16, the Db sample control, and Access 7's Calendar control.
<BR>
<P><B><A HREF="17vcg09.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/17vcg09.gif">Figure 17.9. Visual C++ 4's Component Gallery dialog with OLE controls displayed.</A></B>
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Perhaps you're wondering why you can't see all those controls in your copy of Visual C++ at one time. Actually, Figure 17.9 is a composite of all the controls available. I can see only six at a time, too!</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Here is one of the fantastic things that Visual C++ 4 does: Each time you add (register) a control to your system, Visual C++ sees it and adds the control to the Component Gallery for you. Yes, you can even add that nifty Access 7 Calendar control to a Visual C++ project!
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>All controls are generally available in Visual C++ 4's Component Gallery. This doesn't mean that you can legally use all controls, however. You must abide by the licensing agreement that came with the control. This applies to the Access 7 Calendar control, for example.</NOTE>
<BR>
<HR ALIGN=CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -