?? ch20.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1"> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 20 -- Using ActiveX Controls</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch19/ch19.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch21/ch21.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 20 -<BR>Using ActiveX Controls</FONT></H1><P>ActiveX controls enable you to reuse custom controls written for Windows. In thishour, I discuss ActiveX controls and how they are used. In this hour, you will learn<UL> <LI>How ActiveX controls are used to replace OLE controls and VBX controls<BR> <BR> <LI>How you can use ActiveX controls to easily add functionality to your project<BR> <BR> <LI>How to use the Microsoft FlexGrid control included with Visual C++</UL><P>A small example at the end of the hour uses one of the ActiveX controls includedwith Visual C++.<H2><FONT COLOR="#000077"><B>What Is an ActiveX Control?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>ActiveX control</I>is a reusable control that is packaged and available for use in your applications.ActiveX controls use Object Linking and Embedding (OLE) interfaces for communicationto and from the control.</P><P>ActiveX controls can be developed for both the 16-bit and 32-bit versions of Windows.In addition, they have features that make them more attractive for distribution,such as support for licensing and localization into different languages.</P><P>A wide range of ActiveX controls is available. Later in this hour, you can followthe steps required to use the Microsoft FlexGrid control that is included with VisualC++, which enables you to write simple spreadsheet applications.<H2><FONT COLOR="#000077"><B>Why Use an ActiveX Control?</B></FONT></H2><P>ActiveX controls are easy to use in your MFC-based applications because they havebeen designed for reuse. Developer Studio includes the Component and Controls Gallery,a tool that helps you easily integrate ActiveX controls into your MFC programs.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>ActiveX controls communicateover well-defined <I>interfaces</I> that are understood by ActiveX controls and theprograms that use them. These interfaces are used to pass information and eventsto and from the control.</P><P>Because ActiveX controls use a standard interface that is not specific to anyparticular programming language, ActiveX controls can be used by a variety of developmenttools. The ActiveX controls that you use today in a Visual C++ program can also beused with other tools, such as Access 95, Visual FoxPro, and Visual Basic.</P><P>ActiveX controls offer more functionality than is available with standard controlsoffered by Windows. For example, before the release of Windows 95, many VBX vendorsoffered VBX controls that were similar to tree view controls; these vendors are nowoffering ActiveX controls with features that are not available when using standardcontrols.<H2><FONT COLOR="#000077"><B>How Is an ActiveX Control Used?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An ActiveX <I>event</I> isa message that is sent from the control to the application that contains the control.</P><P>An ActiveX control always communicates with an ActiveX control container. Controlcontainers understand the ActiveX control interfaces, as shown in Figure 20.1. AnActiveX control container is responsible for providing an environment in which thecontrol can pass events to its owner and receive information from the outside world.The ActiveX control sends events to the ActiveX container when an event occurs insidethe control. Mouse clicks, pressed buttons, and expiring timers are examples of events.The ActiveX container provides information to the control such as the natural or"ambient" background color and the default font.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 20.1.</B> </A><I><BR>Messages sent to and from ActiveX controls in an MFC program.</I></P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An ActiveX <I>property</I>is an attribute that is applied to the control, such as a color or the height ofa button.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An ActiveX <I>method</I>is a function that is exposed by the control and is called by the control's container.</P><P>When an ActiveX control container must communicate with an ActiveX control, itinteracts with a set of properties and methods that are exposed by the ActiveX control.An example of an ActiveX property is the font or background color used by a control.An example of an ActiveX method is a function that sorts the items in a list control.</P><P>Every class derived from <TT>CWnd</TT> in an MFC application can be used as anActiveX control container. The MFC class <TT>COleControl</TT> is used as a base classfor all ActiveX controls created using MFC.<H3><FONT COLOR="#000077"><B>Using the Components and Controls Gallery</B></FONT></H3><P>The Developer Studio Components and Controls Gallery is used to store reusablecomponents that can be used in your MFC-based Windows projects. If you develop aclass that you would like to reuse in future projects, you can add the class to theGallery by following these steps:<DL> <DD>1. Open the ClassView in the project workspace.<BR> <BR> 2. Right-click on the class name, and select Add to Gallery from the shortcut menu.</DL><P>The most frequently used components stored in the Components and Controls Galleryare ActiveX controls. To display all the ActiveX controls available on your machine,open the Components and Controls Gallery by selecting Project | Add to Project |Components and Controls from the main menu.</P><P>After the Component Gallery dialog box is displayed, select Registered ActiveXControls from the list box; this displays all the available ActiveX controls, asshown in Figure 20.2.</P><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 20.2.</B> </A><I><BR>Displaying available ActiveX controls in the Component Gallery.</I><H3><FONT COLOR="#000077"><B>Adding an ActiveX Control to the Dialog Editor</B></FONT></H3><P>Before using an ActiveX control in a dialog box, you must insert the control intothe dialog editor's control palette. To add an ActiveX control to the dialog editor,follow these steps:<DL> <DD>1. Select one of the displayed ActiveX control icons.<BR> <BR> 2. From the Components and Controls Gallery dialog box, click the Insert button.<BR> <BR> 3. A message box will be displayed asking if you would like to insert the component. Click OK.<BR> <BR> 4. A list box containing classes that will be added to your project is displayed inside the Confirm Classes dialog box. Click OK to add the classes to your project.<BR> <BR> 5. Click the Close button to dismiss the Components and Controls Gallery dialog box.</DL><P>The ActiveX control you selected is now included in the dialog editor's controlpalette. Open a dialog box for editing, and you see the new control palette, includingthe new ActiveX control.</P><P>You can use the new ActiveX control as you would any other control. To add itto a dialog box resource, drag and drop the control on the dialog box, or selectthe ActiveX control's icon and click on the dialog box resource.<H3><FONT COLOR="#000077"><B>Using ClassWizard to Configure an ActiveX Control</B></FONT></H3><P>Before you can use the ActiveX control, it must be integrated into your project.As with any other control added to a dialog box, use ClassWizard to add message-handlingfunctions and associate the control with an MFC object.</P><P>When adding a member variable associated with an ActiveX control, you can useClassWizard as you would if the control were a button, list box, or another standardWindows control. Unlike standard Windows controls, each ActiveX control has a largenumber of variable types. In addition to the object used to interact with the control,every property exposed by the control can be associated with a variable.<H2><FONT COLOR="#000077"><B>An Example Using an ActiveX Custom Control</B></FONT></H2><P>As an example of using an ActiveX control in an MFC-based project, you will nowuse the Microsoft FlexGrid control in a dialog box-based application. The grid controlis used to create a small spreadsheet in the main dialog box.</P><P>To get started with the sample project, use AppWizard to create a dialog box-basedapplication named CustomCtrl. In contrast to most of the book's other AppWizard examples,for this project you must keep one of the default options offered by the wizard.On the second AppWizard page, make sure the ActiveX Controls check box is selected.Selecting this option causes AppWizard to configure the project to be ActiveX control-ready.<H3><FONT COLOR="#000077"><B>What Is a Grid Control?</B></FONT></H3><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>grid control</I> isa popular reusable component that is similar to a spreadsheet. Many suppliers ofVisual Basic controls offer grid controls, and Microsoft includes with DeveloperStudio an ActiveX grid control named FlexGrid.</P><P>As you can probably guess by its name, a grid control is divided into a seriesof rectangles, or grids. Vertical lines separate the controls into columns, and horizontallines divide the control into rows. The intersection of a row and column is knownas a <I>cell</I>.</P><P>A grid control can contain a mixture of images and text. In most cases, text isused. You cannot directly edit the individual cells in a grid control. The grid controlis strictly a read-only window, although there are ways to simulate cell editingthat are discussed later this hour.</P><P>The most common use for a grid control is creating a small spreadsheet. If youwant to display a small budget or other information, a grid control is ideal. Inaddition, you can use a grid control whenever you must arrange information into rowsand columns. For example, a calendar dialog box might use a grid control to provideaccess to the individual days of the month.</P><P>A grid control spares you the work of creating and maintaining a large numberof smaller controls. The grid control tracks the active cell, as well as the sizeand contents of each cell. When you need access to a particular cell, the grid controlcan provide that information through a function call. At a minimum, grid controlsenable you to do the following:<UL> <LI>Retrieve current row, cell, and column information.<BR> <BR> <LI>Set attributes for the current cell, such as font, size, and contents.<BR> <BR> <LI>Retrieve the attributes of the current cell.</UL><H3><FONT COLOR="#000077"><B>Adding a Grid ActiveX Control to the Dialog Editor</B></FONT></H3><P>To add a grid ActiveX control to the CustomCtrl project's main dialog box, youmust first add the grid control to the dialog editor's control palette by followingthese steps:<DL> <DD>1. Open the Components and Controls Gallery by selecting Project | Add to Project | Components and Controls from the main menu.<BR> <BR> 2. Display the available ActiveX controls by clicking Registered ActiveX Controls from the list box.<BR> <BR> 3. Select the Microsoft FlexGrid, version 5.0 Control icon and then click the Insert button; then click OK on the message box.<BR> <BR> 4. Accept the list of classes that will be added to the project by clicking OK.<BR> <BR> 5. Close the Components and Controls Gallery dialog box.</DL><H3><FONT COLOR="#000077"><B>Adding a Grid Control to the Main Dialog Box</B></FONT></H3><P>Before adding the grid control to the main dialog box, you must first load thedialog box resource into the dialog editor. Open the ResourceView in the projectworkspace. Open the dialog box resource folder and double-click the <TT>IDD_CUSTOMCTRL_DIALOG</TT>icon. This opens the dialog box resource inside the Developer Studio dialog editor.</P><P>To add a grid control, drag and drop the grid control from the control paletteto the dialog box resource. For this example, you must also add an edit control witha resource ID of <TT>IDC_EDIT</TT> and a pushbutton control with an ID of <TT>IDC_CALC</TT>to the dialog box. The finished main dialog box resource is shown in Figure 20.3.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 20.3.</B></A> <I><BR>The main dialog box resource for the <TT>CustomCtrl</TT> project.</I></P><P>The properties for the ActiveX grid control are provided in Table 20.1. Use thedefault properties for the edit control.<H4><FONT COLOR="#000077">Table 20.1. Properties used for the ActiveX grid control.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Property</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>ID</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_GRID</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Rows</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>5</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Cols</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>5</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>FixedRows</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>1</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>FixedCols</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>1</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>ScrollBars</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">None</TD> </TR></TABLE><H3><FONT COLOR="#000077"><B>Initializing the Grid Control</B></FONT></H3><P>Before adding the source code used to initialize the grid control, add membervariables to the <TT>CCustomCtrlDlg</TT> class associated with the grid and editcontrols. Using ClassWizard, add the member variables using the values from Table20.2.<H4><FONT COLOR="#000077">Table 20.2. Values used for the grid and edit control membervariables.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Class Name</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Resource ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CCustomCtrlDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_EDIT</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Control</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CEdit</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_edit</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CCustomCtrlDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_GRID</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Control</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CMSFlexGrid</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_grid</TT></TD> </TR></TABLE><BLOCKQUOTE>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -