Articles in this section
Category / Section

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

5 mins read

Restrict moving the viewport outside of the model

In Diagram’s overview control, we can restrict the movement of the viewport outside the model. This 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 the viewport movement 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 moving the viewport to the right
                widthDiff = (ViewportBounds.X + ViewportBounds.Width) - (rcDisplayArea.X + rcDisplayArea.Width);
                pt.X = ViewportBounds.X - widthDiff;
            }
            else
            {
                // To prevent moving the viewport to the 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 moving the viewport to the bottom
                heightDiff = (ViewportBounds.Y + ViewportBounds.Height) - (rcDisplayArea.Y + rcDisplayArea.Height);
                pt.Y = ViewportBounds.Y - heightDiff;
            }
            else
            {
                // To prevent moving the viewport to the 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


Conclusion

I hope you enjoyed learning about customizing the OverviewControl to restrict the viewport from moving to a negative location.

You can refer to WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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