Vb.net 如何在Scrollable控件中居中放置PictureBox,然后在调整大小时自动滚动

Vb.net 如何在Scrollable控件中居中放置PictureBox,然后在调整大小时自动滚动,vb.net,winforms,user-controls,scroll,Vb.net,Winforms,User Controls,Scroll,试图将视图居中放置在ScrollableControl中元素的中心时遇到一些问题 我在ScrollableControl内的控件居中没有问题,但是每当我尝试设置AutoScrollPosition时,scroll容器内的元素就会停靠到ScrollableControl最里面的容器的右下角 下面是我正在使用的代码(我很确定数学是正确的): 项目的类型实际上是一个UserControl,而不是一个类,因此这本身不是一个“继承的”ScrollableControl。它是一个用户控件类型 我发现最奇怪的

试图将视图居中放置在ScrollableControl中元素的中心时遇到一些问题

我在ScrollableControl内的控件居中没有问题,但是每当我尝试设置AutoScrollPosition时,scroll容器内的元素就会停靠到ScrollableControl最里面的容器的右下角

下面是我正在使用的代码(我很确定数学是正确的):

项目的类型实际上是一个UserControl,而不是一个类,因此这本身不是一个“继承的”ScrollableControl。它是一个用户控件类型

我发现最奇怪的是,这里似乎没有ScrollToX,只有ScrollControlIntoView,诸如此类,它们不能实现我的愿望

基本上,最终结果是可视区域的中心必须是包含在可滚动虚拟区域内的控件的中心。我把控件放在中间,所以这实际上只是把它滚动到视图中,这样内部控件死点的X,Y就在可视区域的死点上

整个类的当前代码

Board.vb
导入System.ComponentModel
公共班级委员会
Private _initas Boolean=False
私钥作为KeyEventArgs=Nothing
''' 
''' 
''' 3
''' 
_
阴影属性背景图像作为图像
得到
返回MyBase.BackgroundImage
结束
设置(值为图像)
MyBase.BackgroundImage=值
端集
端属性
_
阴影属性自动滚动为布尔值
得到
返回MyBase.AutoScroll
结束
设置(值为布尔值)
MyBase.AutoScroll=值
端集
端属性
_
阴影特性自动CrollMinSize为System.Drawing.Size
得到
返回MyBase.AutoScrollMinSize
结束
设置(值为系统.图纸.尺寸)
MyBase.AutoScrollMinSize=值
端集
端属性
作为布尔值的公共属性棋盘格
得到
返回画布1.CheckeredBackground
结束
设置(值为布尔值)
Canvas1.CheckeredBackground=值
端集
端属性
阴影特性边框样式为Windows.Forms.BorderStyle
得到
回访画布1.2风格
结束
设置(值为Windows.Forms.BorderStyle)
Canvas1.BorderStyle=值
端集
端属性
私有子Canvas1\u MouseEnter(发送者作为对象,e作为事件参数)处理Canvas1.MouseEnter
我
端接头
公共子添加(ByVal图像作为System.Drawing.Image)
将l标注为新图层(“图层”和Canvas1.Layers.Count+1)
l、 添加(新图形(图像,新点(10,10)))
画布1.图层。添加(l)
端接头
公共属性CanvasSize()作为System.Drawing.Size
得到
返回画布1.尺寸
结束
设置(值为系统.图纸.尺寸)
Canvas1.大小=值
Me.CenterCanvas()
端集
端属性
公共属性BoardSize()作为System.Drawing.Size
得到
返回我。AutoScrollMinSize
结束
设置(值为系统.图纸.尺寸)
Me.AutoScrollMinSize=值
Me.CenterCanvas()
端集
端属性
作为布尔值的公共函数Remove(ByVal索引为整数)
尝试
Canvas1.Layers.RemoveAt(索引)
返回真值
特例
返回错误
结束尝试
端函数
公共子目录()
游说使无效
端接头
公共子中心画布()
Canvas1.Location=New System.Drawing.Point((MyBase.DisplayRectangle.Width-Canvas1.Size.Width)/2,(MyBase.DisplayRectangle.Height-Canvas1.Size.Height)/2)
'Debug.Print(Canvas1.Top&“,”和Canvas1.Left)
'Dim y As Integer=(Me.Height-Canvas1.Height)/2
'Dim x As Integer=(Me.Width-Canvas1.Width)/2
'x=画布1.左-x
'y=画布1.顶部-y
'Me.AutoScrollPosition=新系统.Drawing.Point(x,y)
端接头
公共分新()
'设计器需要此调用。
初始化组件()
_init=True
'在InitializeComponent()调用之后添加任何初始化。
Me.CenterCanvas()
端接头
私有子板_KeyDown(发送方作为对象,e作为KeyEventArgs)处理Me.KeyDown
_键=e
端接头
私有子板_KeyUp(发送方作为对象,e作为KeyEventArgs)处理Me.KeyUp
_钥匙=没有
端接头
专用子板_mouseweel(发送方作为对象,e作为MouseEventArgs)处理我。mouseweel
暗层
l=Canvas1.SelectedLayer
如果IsNothing(l)=False,则
调试打印(“轮子”)
如果结束
尺寸MW为手持鼠标EEVENTARGS=e
MW.Handled=False
端接头
私有子板_Resize(发送者作为对象,e作为事件参数)处理我。Resize
'如果_initthen
'Dim x As Integer=Canvas1.Left-((MyBase.Width-Canvas1.Size.Width)/2)
'Dim y As Integer=Canvas1.Top-((MyBase.Height-Canvas1.Size.Height)/2)
'MyBase.AutoScrollPosition=New System.Drawing.Point(Canvas1.Left-((MyBase.Width-Canvas1.Size.Width)/2),Canvas1.Top-((MyBase.Height-Canvas1.Size.Height)/2))
"完"
MyBase.AutoScrollPosition=新系统.Drawing.Point(_
Canvas1.Left-MyBase.AutoScrollPosition.X-((MyBase.ClientSize.Width-Canvas1.Size.Width)\2)_
Canvas1.Top-MyBase.AutoScrollPosition.Y-((MyBase.ClientSize.Height-Canvas1.Size.Height)\2))
端接头
私有子板_SizeChanged(发送方作为对象,e作为事件参数)处理Me.SizeChanged
端接头
末级
<
MyBase.AutoScrollPosition = New System.Drawing.Point(Canvas1.Left - ((MyBase.Width - Canvas1.Size.Width) / 2), Canvas1.Top - ((MyBase.Height - Canvas1.Size.Height) / 2))
Imports System.ComponentModel

Public Class Board

    Private _init As Boolean = False

    Private _keys As KeyEventArgs = Nothing

    ''' <summary>
    ''' </summary>
    ''' <filterpriority>3</filterpriority>
    ''' <remarks></remarks>
    <Browsable(False), EditorBrowsable(False)> _
    Shadows Property BackgroundImage As Image
        Get
            Return MyBase.BackgroundImage
        End Get
        Set(value As Image)
            MyBase.BackgroundImage = value
        End Set
    End Property

    <Browsable(False), EditorBrowsable(False)> _
    Shadows Property AutoScroll As Boolean
        Get
            Return MyBase.AutoScroll
        End Get
        Set(value As Boolean)
            MyBase.AutoScroll = value
        End Set
    End Property

    <Browsable(False), EditorBrowsable(False)> _
    Shadows Property AutoScrollMinSize As System.Drawing.Size
        Get
            Return MyBase.AutoScrollMinSize
        End Get
        Set(value As System.Drawing.Size)
            MyBase.AutoScrollMinSize = value
        End Set
    End Property

    Public Property Checkerboard As Boolean
        Get
            Return Canvas1.CheckeredBackground
        End Get
        Set(value As Boolean)
            Canvas1.CheckeredBackground = value
        End Set
    End Property

    Shadows Property BorderStyle As Windows.Forms.BorderStyle
        Get
            Return Canvas1.BorderStyle
        End Get
        Set(value As Windows.Forms.BorderStyle)
            Canvas1.BorderStyle = value
        End Set
    End Property

    Private Sub Canvas1_MouseEnter(sender As Object, e As EventArgs) Handles Canvas1.MouseEnter
        Me.Focus()
    End Sub

    Public Sub Add(ByVal Image As System.Drawing.Image)
        Dim l As New Layer("Layer " & Canvas1.Layers.Count + 1)
        l.Graphics.Add(New Graphic(Image, New Point(10, 10)))
        Canvas1.Layers.Add(l)
    End Sub

    Public Property CanvasSize() As System.Drawing.Size
        Get
            Return Canvas1.Size
        End Get
        Set(value As System.Drawing.Size)
            Canvas1.Size = value
            Me.CenterCanvas()
        End Set
    End Property

    Public Property BoardSize() As System.Drawing.Size
        Get
            Return Me.AutoScrollMinSize
        End Get
        Set(value As System.Drawing.Size)
            Me.AutoScrollMinSize = value
            Me.CenterCanvas()
        End Set
    End Property

    Public Function Remove(ByVal Index As Integer) As Boolean
        Try
            Canvas1.Layers.RemoveAt(Index)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Sub RefreshCanvas()
        Canvas1.Invalidate()
    End Sub

    Public Sub CenterCanvas()
        Canvas1.Location = New System.Drawing.Point((MyBase.DisplayRectangle.Width - Canvas1.Size.Width) / 2, (MyBase.DisplayRectangle.Height - Canvas1.Size.Height) / 2)


        'Debug.Print(Canvas1.Top & ", " & Canvas1.Left)

        '        Dim y As Integer = (Me.Height - Canvas1.Height) / 2
        '        Dim x As Integer = (Me.Width - Canvas1.Width) / 2
        '        x = Canvas1.Left - x
        '        y = Canvas1.Top - y


        '        Me.AutoScrollPosition = New System.Drawing.Point(x, y)
    End Sub

    Public Sub New()

        ' This call is required by the designer.

        InitializeComponent()
        _init = True
        ' Add any initialization after the InitializeComponent() call.
        Me.CenterCanvas()
    End Sub

    Private Sub Board_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        _keys = e
    End Sub

    Private Sub Board_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        _keys = Nothing
    End Sub

    Private Sub Board_MouseWheel(sender As Object, e As MouseEventArgs) Handles Me.MouseWheel
        Dim l As Layer

        l = Canvas1.SelectedLayer
        If IsNothing(l) = False Then
            Debug.Print("Wheels")
        End If

        Dim MW As HandledMouseEventArgs = e
        MW.Handled = False
    End Sub

    Private Sub Board_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        '       If _init Then
        ' Dim x As Integer = Canvas1.Left - ((MyBase.Width - Canvas1.Size.Width) / 2)
        'Dim y As Integer = Canvas1.Top - ((MyBase.Height - Canvas1.Size.Height) / 2)
        '           MyBase.AutoScrollPosition = New System.Drawing.Point(Canvas1.Left - ((MyBase.Width - Canvas1.Size.Width) / 2), Canvas1.Top - ((MyBase.Height - Canvas1.Size.Height) / 2))
        'End If

        MyBase.AutoScrollPosition = New System.Drawing.Point( _
                Canvas1.Left - MyBase.AutoScrollPosition.X - ((MyBase.ClientSize.Width - Canvas1.Size.Width) \ 2), _
                Canvas1.Top - MyBase.AutoScrollPosition.Y - ((MyBase.ClientSize.Height - Canvas1.Size.Height) \ 2))
    End Sub

    Private Sub Board_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged

    End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Board
    Inherits System.Windows.Forms.UserControl

    'UserControl1 overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Canvas1 = New Artisto.Canvas()
        CType(Me.Canvas1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Canvas1
        '
        Me.Canvas1.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.Canvas1.CheckeredBackground = True
        Me.Canvas1.Location = New System.Drawing.Point(0, 0)
        Me.Canvas1.Name = "Canvas1"
        Me.Canvas1.SelectedLayer = Nothing
        Me.Canvas1.Size = New System.Drawing.Size(619, 317)
        Me.Canvas1.TabIndex = 0
        Me.Canvas1.TabStop = False
        '
        'Board
        '
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
        Me.AutoScroll = True
        Me.AutoScrollMinSize = New System.Drawing.Size(4096, 2160)
        Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.Controls.Add(Me.Canvas1)
        Me.DoubleBuffered = True
        Me.Name = "Board"
        Me.Size = New System.Drawing.Size(1266, 523)
        CType(Me.Canvas1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Canvas1 As Artisto.Canvas

End Class