Articles in this section
Category / Section

Customizing the OverviewControl to restricting to change the viewport as negative location

2 mins read

Restrict to move the viewport as outside of the model

In Diagram’s overview control, we can restrict the movement of viewport outside the model. It can be achieved by customizing the Diagram’s “OverviewControl” class. From the customized class, override the draw method and draw the “ViewPort” logically based on the model area.

Here is the code example for restricting to move the viewport as outside of the model.

[C#]

protected override void DrawViewport(Graphics grfx)
{
if (Diagram.View != null)
{
if ((ViewportBounds.X + ViewportBounds.Width > rcDisplayArea.X + rcDisplayArea.Width || ViewportBounds.X < rcDisplayArea.X)
|| (ViewportBounds.Y + ViewportBounds.Height > rcDisplayArea.Y + rcDisplayArea.Height || ViewportBounds.Y < rcDisplayArea.Y))
{
float heightDiff = 0;
float widthDiff = 0;
PointF pt = PointF.Empty;
                    
if (ViewportBounds.X + ViewportBounds.Width > rcDisplayArea.X + rcDisplayArea.Width)
{
//to Prevent move the viewport in right
widthDiff = (ViewportBounds.X + ViewportBounds.Width) - (rcDisplayArea.X + rcDisplayArea.Width);
pt.X = ViewportBounds.X - widthDiff;
}
else
{
//To prevent move the viewport in top
if (ViewportBounds.X < rcDisplayArea.X)
{
widthDiff = rcDisplayArea.X - ViewportBounds.X;
}
pt.X = ViewportBounds.X + widthDiff;
}
 
                    
if (ViewportBounds.Y + ViewportBounds.Height > rcDisplayArea.Y + rcDisplayArea.Height)
{
//to prevent move the viewport in bottom
heightDiff = (ViewportBounds.Y + ViewportBounds.Height) - (rcDisplayArea.Y + rcDisplayArea.Height);
pt.Y = ViewportBounds.Y - heightDiff;
} 
else
{
//To prevent move the viewport in left
if (ViewportBounds.Y < rcDisplayArea.Y)
{
heightDiff = rcDisplayArea.Y - ViewportBounds.Y;
}
pt.Y = ViewportBounds.Y + heightDiff;
}
 
SizeF size = ViewportBounds.Size;
if (size.Width > rcDisplayArea.Width)
size.Width = rcDisplayArea.Width;
if (size.Height > rcDisplayArea.Height)
size.Height = rcDisplayArea.Height;
 
ViewportBounds = new RectangleF(pt.X, pt.Y, size.Width, size.Height);
if (!(this.Diagram.Size.Width == 0 || this.Diagram.Size.Height == 0))
{
this.vpRenderer.DrawViewport(grfx, this.ForeColor);
}
}
else
{
base.DrawViewport(grfx);
}
}
}

 

[VB]

Protected Overrides Sub DrawViewport(ByVal grfx As Graphics)
If Diagram.View IsNot Nothing Then
If (ViewportBounds.X + ViewportBounds.Width > rcDisplayArea.X + rcDisplayArea.Width OrElse ViewportBounds.X < rcDisplayArea.X) OrElse (ViewportBounds.Y + ViewportBounds.Height > rcDisplayArea.Y + rcDisplayArea.Height OrElse ViewportBounds.Y < rcDisplayArea.Y) Then
Dim heightDiff As Single = 0
Dim widthDiff As Single = 0
Dim pt As PointF = PointF.Empty
 
If ViewportBounds.X + ViewportBounds.Width > rcDisplayArea.X + rcDisplayArea.Width Then
'to Prevent move the viewport in right
widthDiff = (ViewportBounds.X + ViewportBounds.Width) - (rcDisplayArea.X + rcDisplayArea.Width)
pt.X = ViewportBounds.X - widthDiff
Else
'To prevent move the viewport in top
If ViewportBounds.X < rcDisplayArea.X Then
widthDiff = rcDisplayArea.X - ViewportBounds.X
End If
pt.X = ViewportBounds.X + widthDiff
End If
 
 
If ViewportBounds.Y + ViewportBounds.Height > rcDisplayArea.Y + rcDisplayArea.Height Then
'to prevent move the viewport in bottom
heightDiff = (ViewportBounds.Y + ViewportBounds.Height) - (rcDisplayArea.Y + rcDisplayArea.Height)
pt.Y = ViewportBounds.Y - heightDiff
Else
'To prevent move the viewport in left
If ViewportBounds.Y < rcDisplayArea.Y Then
heightDiff = rcDisplayArea.Y - ViewportBounds.Y
End If
pt.Y = ViewportBounds.Y + heightDiff
End If
 
Dim size As SizeF = ViewportBounds.Size
If size.Width > rcDisplayArea.Width Then
size.Width = rcDisplayArea.Width
End If
If size.Height > rcDisplayArea.Height Then
size.Height = rcDisplayArea.Height
End If
 
ViewportBounds = New RectangleF(pt.X, pt.Y, size.Width, size.Height)
If Not(Me.Diagram.Size.Width = 0 OrElse Me.Diagram.Size.Height = 0) Then
Me.vpRenderer.DrawViewport(grfx, Me.ForeColor)
End If
Else
MyBase.DrawViewport(grfx)
End If
End If
End Sub
 

 

Here is the sample for restricting to move the viewport as outside of the model.

Sample

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied