AutoZoom & Center

G

Gilgil0886

Hi!

I was looking for this code for a long time, and couldn't find it.
Basically what I wanted to do is make all the shapes in a drawing
properly visible & centered.

Here's the code:

public void AutoZoom()
{
if (m_bFirstTime)
return;

double dMaxTop, dMinLeft, dMinBottom, dMaxRight;
double dViewLeft, dViewTop, dViewWidth, dViewHeight;

VisWindow.GetViewRect(out dViewLeft, out dViewTop, out dViewWidth,
out dViewHeight);

dMaxTop = dMinBottom = dViewTop - dViewHeight / 2;
dMaxRight = dMinLeft = dViewLeft + dViewWidth / 2;

foreach (Visio.Shape shape in VisPage.Shapes)
{
double dPinX = shape.get_CellsU("PinX").get_Result("inches");
double dPinY = shape.get_CellsU("PinY").get_Result("inches");
double dWidth = shape.get_CellsU("Width").get_Result("inches");
double dHeight =
shape.get_CellsU("Height").get_Result("inches");

double dLeft = dPinX - dWidth / 2;
double dRight = dPinX + dWidth / 2;
double dTop = dPinY + dHeight / 2;
double dBottom = dPinY - dHeight / 2;

if (dMaxTop < dTop)
dMaxTop = dTop;

if (dMaxRight < dRight)
dMaxRight = dRight;

if (dMinLeft > dLeft)
dMinLeft = dLeft;

if (dMinBottom > dBottom)
dMinBottom = dBottom;
}

// Center drawing
VisWindow.ScrollViewTo((dMinLeft + dMaxRight) / 2, (dMaxTop +
dMinBottom) / 2);

// Fit all shapes into view
double dFitY = (dMaxTop - dMinBottom) * 1.1 / dViewHeight; // Take
10% more...
double dFitX = (dMaxRight - dMinLeft) * 1.1 / dViewWidth;

VisWindow.ZoomBehavior = Visio.VisZoomBehavior.visZoomVisioExact;
double dZoom = VisWindow.Zoom / System.Math.Max(dFitY, dFitX);

// Don't zoom in too much...
if (dZoom < 1)
VisWindow.Zoom = dZoom;
}

Your comments are welcome...
Enjoy!
 
N

news.microsoft.com

I use the Page.BoundingBox method to get the extents of shapes on a page.
Then, you can get the window proportions using Window.GetViewRect. Then,
compare the maximum dimensions of the bounding box to the view rectangle,
and use Window.SetViewRect to "zoom to extents"

There's also a Window.ViewFit property, but this is more concerned with the
paper/page size, and not where the actual shapes are on the page.


--
Hope this helps,

Chris
Visio MVP

www.wanderkind.com/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