Design-Drawing Home  
Drawing Program
ISSN 1441-5585

Search...

Home
Articles
Software Catalog
Book Store
About
Advertising
Newsletter

Pearls from the auld Scotsman
Few people know Visio software and communicate their knowledge as well as Dave Edson. In this regular column Dave dispenses pearls of wisdom in his own inimitable style.

ActiveX an' Visio Colors

David A Edson

Greetin’s tae ye a’!!! Have ye ever noticed how bright the colors seem in the summer? I’ve been doin’ quite a lot o’ travelin’ this spring and it looks like it will continue thruu the summer as weel. As I travel aboot, I notice how each city has its own look and feel and distinct color palette. This led me to get a grand idea about how to illustrate the use of ActiveX controls in Visio and how you can create your own custom colors with a Visio ShapeSheet function called RGB().

The accompanying Visio diagram has everything ye will need to "adaptively reuse" my code for your own solution. Let me describe a wee bit aboot what’s going on here. The illustration below shows my wee sample application in runnin’ mode:

What we have here is a Visio diagram containin’ a SmartShape symbol which is a simple rectangle. There are just a few special qualities aboot this rectangle that ye need to be aware of… Firstly the rectangle shape has been given a name: "Pallette". We do this by selectin’ Format > Special and in the Name text box we enter the name "Pallette". Now you can name your shape anything you wish, but this was my own name as it came to me… blame it on summer, aye? Additionally the shape has a User Defined Section added to the ShapeSheet. In the User Defined section there are three rows named User.R, User.G, and User.B. These will hold the Red, Green, and Blue numeric values that we will pass to Visio’s RGB() Function. In the Fill Format Section of the ShapeSheet I have placed the formula: =RGB(User.R, User.G, User.B) . As you can see the RGB() Function takes the three arguments, each with a range from 0 to 255 and passes to the Fill format cell a number equating to the unique color that will be displayed on the SmartShape symbol. A peek at the ShapeSheet would look somewhat like the following:

I’ve created several other shapes aboot the edge of this shape… These are strictly for cosmetics and are of no real consequence, except tae mak ye look lak a brilliant graphic artist in front of the boss!

Now with all of that accomplished, you are going to need to add three ActiveX Controls to your diagram. If you do not already have your Developer Toolbar visible, right-click on your Toolbar area and select the Developer Toolbar from the context menu choices. Grand! Now you will see several wee buttons on yer newly active Toolbar. One of these wee buttons looks like a Triangle, Ruler and Pencil. This is a state button; that is, it can either be depressed or raised. When it is depressed you are working in "Design Mode" in Visio. When it is raised, you are working in "Run Mode". Make sure that the button is depressed and you are in Design mode. Now click on the ActiveX Controls button. This is the wee button that looks like a VB Command Button overlappin’ a Radio Button . When you click this, a dialogue will appear with a list of all of the available ActiveX Controls you can work with. Scroll Down until you find the Microsoft 2.0 ScrollBar control. Highlight this item and click OK.

If you right-click on the control, you can set it’s properties. The properties should look like the following:

The important properties to set for this Control are the Orientation to Horizontal, the Minimum to 0, the Maximum to 255, and the large change to 10. Make these changes and then do this two more times so that you have all three controls as I have shown ye, in the illustration above.

The Code and Events
Noo here’s where things get fascinating. Just as there are Events in Visio (both in Shapes and Drawings), there are Events in ActiveX Controls as weel. The Slider Bar has two events of importance; the Change event and the Scroll event. These are what are fired each time you adjust the wee "thumb" on the slider bar or click on either of the up or down buttons at either end of the slider bar. We have told our slider bar that its upper limit is 255 and its lower limit is 0. Therefore the number that will be returned from the Value Property of the Slider Bar will always be an integer between 0 and 255.

We want to ensure that our values and the slider bar positions are initialized and accurate each time we enter the drawing or return from Design Mode to Run Mode in Visio. We also need to establish a few global variables to hold our values as long as the drawing is open. By now I assume that you all know how to enter the VBA IDE in Visio. Just as in Office, the magic key combination is Alt+F11. When you are in VBA add a new module to your current drawing’s project. In that new module declare the following Global variables:

Global inRValue As Integer
Global inGValue As Integer
Global inBValue As Integer
Global shPalletteShape As Visio.Shape
Global ceUserR As Visio.Cell
Global ceUserG As Visio.Cell
Global ceUserB As Visio.Cell

Note that we have three typed as Integer to hold our R, G, and B values, one types as Visio Shape to hold the reference to the Pallette shape, and three typed as Visio Cells to hold the references to the User.R, User.G, and User.B cells.

The Document has a RunModeEntered() Event. This is triggered every time the document opens or is placed in Run mode. This is where we will initialize all of the values we need and set the proper position for the thumbs on the ActiveX Controls. The code should look like the following:

Private Sub Document_RunModeEntered(ByVal doc As Visio.IVDocument)
Set shPalletteShape = Visio.ActivePage.Shapes.Item("Pallette")
Set ceUserR = shPalletteShape.Cells("User.R")
Set ceUserG = shPalletteShape.Cells("User.G")
Set ceUserB = shPalletteShape.Cells("User.B")
inRValue = ceUserR.Result(visNumber)
inGValue = ceUserG.Result(visNumber)
inBValue = ceUserB.Result(visNumber)
ScrollBar1.Value = inRValue
ScrollBar2.Value = inGValue
ScrollBar3.Value = inBValue
End Sub

Here we are obtaining the reference to the Pallette Shape and each of the User cells. Additionally we are retrieving the values stored in those cells and passing those values to the Value Property of each of the ActiveX Slider Bar Controls to properly position the thumbs.

When the user interacts with any of the three slider bars, either the Change or the Scroll Event is triggered for that control. To save us from entering the same function twice for each event for each control, we are going to create 3 subroutines called R_Change(), G_Change(), and B_Change. We will call each of these routines via the proper and associated events for the controls as follows:

Private Sub ScrollBar1_Change()
R_Change
End Sub

Private Sub ScrollBar1_Scroll()
R_Change
End Sub

Private Sub ScrollBar2_Change()
G_Change
End Sub

Private Sub ScrollBar2_Scroll()
G_Change
End Sub

Private Sub ScrollBar3_Change()
B_Change
End Sub

Private Sub ScrollBar3_Scroll()
B_Change
End Sub

Private Sub R_Change()
inRValue = ScrollBar1.Value
ceUserR.FormulaForce = inRValue
End Sub

Private Sub G_Change()
inGValue = ScrollBar2.Value
ceUserG.FormulaForce = inGValue
End Sub

Private Sub B_Change()
inBValue = ScrollBar2.Value
ceUserB.FormulaForce = inBValue
End Sub

The thrust of any one of these three routines is that when called, it reads the new value from the control’s thumb position and passes it to the Formula property of the proper User Defined cell in the Pallette Shape’s ShapeSheet.

It truly is no more difficult than that folks!

Conclusion
The important concepts here are that ActiveX Controls, like Visio Drawings and Shapes are Objects. As such, they contain Properties, Methods and Events. These are all accessible through VB and VBA. By manipulating the Controls and having your code manipulate the Shapes you set up a brilliant interaction that an make visually appealing, highly intelligent solutions to your Enterprise challenges.

Ha’e a grand July, dinnae burn yersel’ tae much an’…….

"Haste ye back."

Dave "The Auld Scotsman" Edson

 
Rate this article...
Hmmm  OK  Good  Yes! Brilliant
Your a friend about this article.

Copyright © 1998-2007 DBM & others | Disclaimer | Privacy | Re-publication | Trademarks | Webmaster | Home