Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید







Automation Clients and Components


A clearly defined "master-slave" relationship is always present in an Automation communication dialog. The master is the Automation client and the slave is the Automation component (server). The client initiates the interaction by constructing a component object (it might have to load the component program) or by attaching to an existing object in a component program that is already running. The client then calls interface functions in the component and releases those interfaces when it's finished.

Here are some interaction scenarios:



    A C++ Automation client uses a Microsoft or third-party application as a component. The interaction might trigger the execution of VBA code in the component application.

    A C++ Automation component is used from inside a Microsoft application (or a Visual Basic application), which acts as the Automation client. VBA code can thus construct and use C++ objects.

    A C++ Automation client uses a C++ Automation component.

    A Visual Basic program uses an Automation-aware application such as Excel. In this case, Visual Basic is the client and Excel is the component.



Excel: A Better Visual Basic Than Visual Basic


When the first three editions of this book were written, Visual Basic worked as an Automation client but you couldn't use it to create an Automation component. Since version 5.0, Visual Basic has let you write components, too—even ActiveX controls. The book originally used Excel instead of Visual Basic because Excel was the first Microsoft application to support VBA syntax and could serve as both a client and a component. Here, we'll stick with Excel because C++ programmers who look down their noses at Visual Basic might be inclined to buy Excel (if only to track their software royalties).

I strongly recommend that you get the latest version of Excel, which is a true 32-bit application and is a part of the Microsoft Office suite. With this version of Excel, you can write VBA code in a separate location that accesses worksheet cells in an object-oriented manner. Adding visual programming elements—such as buttons—is easy. Forget all you ever knew about the old spreadsheet programs that forced you to wedge macro code inside cells.

This chapter isn't meant to be an Excel tutorial, but it includes a simple Excel workbook. (A workbook is a file that can contain multiple worksheets plus separate VBA code.) This workbook demonstrates a VBA macro that executes from a button. You can use Excel to load

Demo.xls from the \vcppnet\Ex23a subdirectory, or you can key in the example from scratch. Figure 23-1 shows the actual spreadsheet with the button and sample data.


Figure 23-1: An Excel spreadsheet that uses VBA code.

In this spreadsheet, you highlight cells A4 through A9 and click the Process Col button. A VBA program iterates down the column and draws a hatched pattern on cells with numeric values greater than 10.

Figure 23-2 shows the macro code itself, which is "behind" the worksheet. In Excel, choose Macro from the Tools menu, and then choose Visual Basic Editor. (Alt+F11 is the shortcut.) As you can see, you're working in the standard VBA environment at this point.


Figure 23-2: The VBA code for the Excel spreadsheet.

If you want to create the example yourself, follow these steps:



    Start Excel with a new workbook, press Alt+F11, and then double-click Sheet1 in the top left window.

    Type in the macro code shown in Figure 23-2.

    Return to the Excel window by choosing Close And Return To Microsoft Excel from the File menu. Choose Toolbars from the View menu. Select Forms to display the Forms toolbar. (You can also access the list of toolbars by right-clicking on any existing toolbar.)

    Click the Button control, and then create the button by dragging the mouse in the upper left corner of the worksheet. Assign the button to the Sheet1.ProcessColumn macro.

    Size the button, and type the caption Process Col (as shown in Figure 23-1).

    Type some numbers in the column starting at cell A4. Select the cells containing these numbers, and then click the button to test the program.

    Pretty easy, isn't it?

    Let's analyze an Excel VBA statement from the macro above:

    Selection.Offset(1, 0).Range("A1").Select


The first element, Selection, is a property of an implied object, the Excel application. The Selection property in this case is assumed to be a Range object that represents a rectangular array of cells. The second element, Offset, is a property of the Range object that returns another Range object based on the two parameters. In this case, the returned Range object is the one-cell range that begins one row down from the original range. The third element, Range, is a property of the Range object that returns yet another range. This time it's the upper left cell in the second range. Finally, the Select method causes Excel to highlight the selected cell and makes it the new Selection property of the application.

As the program iterates through the loop, the preceding statement moves the selected cell down the worksheet one row at a time. This style of programming takes some getting used to, but it's fairly common—especially in Office environments that usually deal with lots of documents. The real value here is that you have all the capabilities of the Excel spreadsheet and graphics engine available to you in a seamless programming environment.


Properties, Methods, and Collections


The distinction between a property and a method is somewhat artificial. Basically, a property is a value that can be both set and retrieved. You can, for example, set and get the Selection property for an Excel application. Another example is Excel's Width property, which applies to many object types. Some Excel properties are read-only, but most are read/write.

Properties don't officially have parameters, but some properties are indexed. The property index acts a lot like a parameter. It doesn't have to be an integer, and it can have more than one element (a row and a column, for example). You'll find many indexed properties in Excel's object model, and Excel VBA can handle indexed properties in Automation components.

Methods are more flexible than properties. They can have zero or many parameters, and they can either set or retrieve object data. Most frequently, they perform some action, such as showing a window. Excel's Select method is an example of an action method.

The Excel object model supports collection objects. For example, if you use the Worksheets property of the Application object, you get back a Sheets collection object, which represents all the worksheets in the active workbook. You can use the Item property (with an integer index) to get a specific Worksheet object from a Sheets collection, or you can use an integer index directly on the collection.

/ 319