Programatically changing the color of a shape in Visio 2007.

J

jader3rd

I'm writing a program in C# which is laying out a series of shapes based on
the input given to the program. I'm at the point where I want to change the
color of the shapes and following the pattern of changing the cell value for
that shape isn't working. For example, this bit of code isn't changing the
color of the shape.

Document adStencil = vApp.Documents.Add(@"ADS_U.VSS");
Master domainMaster = adStencil.Masters.get_ItemU(@"Domain 2D");
Shape domain = page.Drop(domainMaster, startX, startY);
domain.get_CellsU("FillForegnd").ResultIUForce = 2;

The interesting thing is when I go to change the color of the shape through
Visio after having set it this way the color is still white, but in the color
selection window the color is gray, but it won't set the color to gray.
I would prefer it if I didn't have to set it to a color on the color
chooser, but could pass in an RGB value. What step am I missing?
 
P

Paul Herber

I'm writing a program in C# which is laying out a series of shapes based on
the input given to the program. I'm at the point where I want to change the
color of the shapes and following the pattern of changing the cell value for
that shape isn't working. For example, this bit of code isn't changing the
color of the shape.

Document adStencil = vApp.Documents.Add(@"ADS_U.VSS");
Master domainMaster = adStencil.Masters.get_ItemU(@"Domain 2D");
Shape domain = page.Drop(domainMaster, startX, startY);
domain.get_CellsU("FillForegnd").ResultIUForce = 2;

The interesting thing is when I go to change the color of the shape through
Visio after having set it this way the color is still white, but in the color
selection window the color is gray, but it won't set the color to gray.
I would prefer it if I didn't have to set it to a color on the color
chooser, but could pass in an RGB value. What step am I missing?

domain.CellsSRC[visSectionObject, visRowFill, visFillForegnd].FormulaU
= an RGB string as used in the shapesheet

C# syntax may be slightly different
 
J

jader3rd

The C# syntax for that is

domain.get_CellsSRC((short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowFill,
(short)VisCellIndices.visFillForegnd).FormulaU = "RGB(254,245,204)";

and it should have worked. After I run the program the color of the object
is still white when I open the saved file in Visio. I then right click on the
object to change the color and the Fill window shows that the color is the
color I set it to. I can press okay and it will then change the color. So the
color is being set, but whatever is responsible for painting the shape
doesn't get the update that the color has been changed. Is there a way to
tell the shape to repaint itself?
 
P

Paul Herber

What's the formula set to? Is is the RGB formula you've programmed ?
Anyway, I think you need an '=' in front of the RGB for the formula. I
can't check now as it's Saturday night pub time.


The C# syntax for that is

domain.get_CellsSRC((short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowFill,
(short)VisCellIndices.visFillForegnd).FormulaU = "RGB(254,245,204)";

and it should have worked. After I run the program the color of the object
is still white when I open the saved file in Visio. I then right click on the
object to change the color and the Fill window shows that the color is the
color I set it to. I can press okay and it will then change the color. So the
color is being set, but whatever is responsible for painting the shape
doesn't get the update that the color has been changed. Is there a way to
tell the shape to repaint itself?
domain.CellsSRC[visSectionObject, visRowFill, visFillForegnd].FormulaU
= an RGB string as used in the shapesheet

C# syntax may be slightly different
 
J

jader3rd

I found a post on the internet which mentioned how when Visio changes the
color of an object, it recurses and changes the child shapes as well. For my
purposes I only need one level of children changed. So I wrote this method
which will change the color for all of the object I'm dealing with.

private void applyFormulaToShapeAndChildren(Shape shape, string cell, string
formula)
{
shape.get_CellsU(cell).FormulaU = formula;
foreach (Shape subShape in shape.Shapes)
{
if (0 == subShape.get_CellExistsU(cell,
(short)VisExistsFlags.visExistsLocally))
{
subShape.get_CellsU(cell).FormulaForceU = formula;
}
}
}

Where a typical usuage would be

applyFormulaToShapeAndChildren(domain, @"FillForegnd", @"RGB(254,245,204)");

So far this has matched the behavior of when I change the color in Visio.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top