Hello again.
Ok, it’s in VB.NET, so you’ll have to translate, but here’s what I think works.
Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
Dim dlg As New OpenFileDialog()
dlg.DefaultExt = "shp"
If dlg.ShowDialog() = DialogResult.OK Then
Dim fullpath As String = dlg.FileName
AxMap1.AddLayerFromFilename(fullpath, tkFileOpenStrategy.fosAutoDetect, True)
End If
' save maximum extents
maxExtents = AxMap1.Extents
End Sub
Private bChangingExtents As Boolean = False
Private maxExtents As Extents = Nothing
Private Sub AxMap1_ExtentsChanged(sender As Object, e As EventArgs) Handles AxMap1.ExtentsChanged
If bChangingExtents Then Return
' new extents to set (overriding current extents)
Dim newExtents As New Extents()
Dim needsUpdate As Boolean = False
If maxExtents IsNot Nothing Then
' get the current extents
Dim currentExtents As Extents = AxMap1.Extents
' if zoomed out beyond max extents (allowing for error), put it back
If CInt(currentExtents.Width) > CInt(maxExtents.Width) OrElse CInt(currentExtents.Height) > CInt(maxExtents.Height) Then
newExtents = maxExtents
needsUpdate = True
Else
' if anywhere within the max extents, we only have to worry about panning outside of max extents
With currentExtents
' initialize to current extents
newExtents.SetBounds(.xMin, .yMin, 0, .xMax, .yMax, 0)
End With
' if panned to the right
If currentExtents.xMin < maxExtents.xMin Then
' take current extents and push it to the left, but maintain width
newExtents.SetBounds(maxExtents.xMin, newExtents.yMin, 0, maxExtents.xMin + newExtents.Width, newExtents.yMax, 0)
needsUpdate = True
End If
' if panned to the left
If currentExtents.xMax > maxExtents.xMax Then
' take current extents and push it to the right, but maintain width
newExtents.SetBounds(maxExtents.xMax - newExtents.Width, currentExtents.yMin, 0, maxExtents.xMax, newExtents.yMax, 0)
needsUpdate = True
End If
' if panned up
If currentExtents.yMin < maxExtents.yMin Then
' take current extents and push it back down, but maintain height
newExtents.SetBounds(newExtents.xMin, maxExtents.yMin, 0, newExtents.xMax, newExtents.yMin + newExtents.Height, 0)
needsUpdate = True
End If
' if panned down
If currentExtents.yMax > maxExtents.yMax Then
' take current extents and push it back up, but maintain height
newExtents.SetBounds(newExtents.xMin, maxExtents.yMax - newExtents.Height, 0, newExtents.xMax, maxExtents.yMax, 0)
needsUpdate = True
End If
End If
' do we need to update the extents?
If needsUpdate Then
bChangingExtents = True
AxMap1.Extents = newExtents
bChangingExtents = False
End If
End If
End Sub
- I start by loading a layer (in
ButtonLoad_Click
) and set my max extents based on that layer. You can set your max extents as you see fit for your application.
- In the
ExtentsChanged
event, I first check for being zoomed out too much, and if so, bring it back to the max extents. If it’s zoomed in, then we never have to change the zoom - we will just maintain the current width and height, and only adjust the position if they pan outside of the max extents.
- I start by copying the current map extents to my so-called
newExtents
. This creates my initial viewport.
- I then check for each of the 4 conditions, panning left, right, up, and down. If they have moved the view outside of the max extents, we adjust the position, maintaining the width and height, but making sure the view stays inside of the max extents.
The formatting of the code above seems to get confused by the VB comment character ’ (single quote). You can just translate this into your c# code, and it should work for you.
Let me know how it goes.
Regards,
Jerry.