Volume 3: Programming with the Visio Object Model disclaimer


Lab 3.4: Calculate the Area of a Shape



Download 0.78 Mb.
Page18/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   14   15   16   17   18   19   20   21   ...   36

Lab 3.4: Calculate the Area of a Shape



Points to Consider


Area based on width and height (parameters of shape alignment box) will be larger than area based on geometry.

Look for a Shape property that calculates area based on shape geometry.


Review: Getting Data from Drawings



Questions


  1. What would be an advantage of using the ActivePage property? What would be a disadvantage?

  2. What is the difference between Open and OpenEx?

  3. Under what circumstances would you use CellsSRC?

  4. How would you find out whether a Shape object represents a group shape?

Answers


  1. The ActivePage property is a quick way of getting a page without going through a document object. However, it only works if the active window is a drawing window.

  2. The Open method opens a document as the original. The OpenEx method has an additional parameter that is use to specify how the document is to be opened, e.g., as read-only.

  3. Use CellsSRC when a section has an non-fixed number of rows or when the number of cells in a row can vary, such as in the geometry section.

  4. Use the shape’s Type property to test whether it is a group.

11.

Managing Events





Learning Objective


Even though you can respond to user events through the ShapeSheet, Visio has a more robust, programmable event mechanism. This Event object model allows the programmer to set up event handling for a larger set of events.

In this section you will see how your solution can respond to ShapeSheet events. You will also get an introduction to the Visio event object model and learn how to use Visual Basic for Applications to create code behind events.You will first look at handling basic events through the management of ShapeSheet cells. When something happens to a shape, such as when it is moved or resized, some cell values change. You can detect these changes in the ShapeSheet and cause another action to occur as a result.

More directly, the ShapeSheet has an Event section that has event cells (TheText, EventDblCick, EventXFMod, and EventDrop) in which formulas can be placed. These cells evaluate whenever the event occurs.

Beyond the ShapeSheet, events can be handled through Visual Basic for Applications as code behind events. This uses the underlying Visio object model to create and respond to events.

Introduction


Before diving into code behind events, you should be clear about what is an event.

A dictionary definition of an event is “an occurrence or incident, especially one of significance.” As a programmer, you want to know when these “events” occur and do something in response to the event.

Keep in mind that events can occur as a result of a user action or as a result of a program being run that controls Visio.


Cell Dependencies


Within the formula of a cell can be references to other cells. This creates intercell relationships or cell dependencies.

These dependencies are implied by the cell names used in the formulas. In the slide example, PinY depends on BeginY and EndY. EndY, in turn, depends on a connection point in Sheet.2.

Because of the dependency that has been established, when the value of the connection point changes, the value of the EndY cell is recalculated. If the value of the EndY cell changes, the PinY cell is recalculated.



Note:

The Visio product engine does not recalculate all cells whenever a shape changes. Instead it establishes a dependency network so that it only recalculates cells that have the potential to change.



Force Dependencies with DependsOn


In addition to creating intercell dependencies through the use of another cell’s name, you can force cell dependencies by using the DependsOn function. DependsOn creates a cell reference dependency on the cells specified in the list of trigger cells. The function always returns FALSE (the value 0) and thus has no effect on the final value of the cell when used in this fashion.

A typical use of the DependsOn function is to cause some action to occur whenever a cell’s value changes. In the slide, the first example runs an add-on whenever the trigger cell changes. The second example will reset “Cell” to some formula or value whenever a trigger cell changes.

In these examples the final result of the User-defined or Scratch cell is irrelevant. However, the developer’s use of DependsOn is not limited to these types of examples.


DependsOn Example


Here is a more practical example. Suppose you want to set a shape’s color from the shape data dialog, but don’t want to interfere with the user’s ability to change a shape’s color using the normal user interface tools. In addition, if the user does change the shape’s color through the user interface, you would like the shape data field to hold the current value.

A shape’s color is kept in its FillForegnd cell. Assume that a shape data cell, Prop.Color, has been created to hold the value to be displayed in the shape data dialog. It is not sufficient to just display the FillForegnd cell in the shape data dialog by referencing it from Prop.Color. A change to Prop.Color would overwrite this formula.

The solution lies in creating two User-defined cells. In these cells place the formulas:

=DependsOn(Prop.Color)+Setf(“FillForegnd”,Prop.Color)

=DependsOn(FillForegnd)+Setf(“Prop.Color”,FillForegnd)

The first resets FillForegnd whenever the user changes color though the shape data dialog.

The second changes the value displayed in the shape data dialog whenever the user changes the shape’s color using other user interface tools.

Using this method insures the information in each place is the most current.

Tryit! DependsOn

  1. From your student CD, open \Demos\Vol3\Managing Events\DependsOn Example.vsd.

  2. Display the shape data window. Data > Shape Data Window.

  3. Change the shapes fill color through the user interface. Note that the shape data field is updated.

  4. Change the shape data value. Note that the shape’s fill color updates.

Note:

The Visio product engine allows circular references in situations like this and prevents the cells from being recalculated infinitely.





Download 0.78 Mb.

Share with your friends:
1   ...   14   15   16   17   18   19   20   21   ...   36




The database is protected by copyright ©ininet.org 2024
send message

    Main page