You need to enable JavaScript to run this app. Winforms自定义设计器控件缩放后平移时选择框位置偏移问题求解
优惠活动
大模型
产品
解决方案
定价
更多

Winforms自定义设计器控件缩放后平移时选择框位置偏移问题求解

基于Winforms的可视化设计器画布缩放平移后选择框偏移问题

需求背景

  • 开发一款基于Winforms的可视化设计器,提供所见即所得的Canvas画布
  • 支持用户拖拽放置图片、文本、形状类元素
  • 可通过带操作柄的选择框对元素执行选中、移动、调整大小操作,支持多选,需要实现多选择框逻辑
  • 画布需支持缩放和平移功能

实现方案

项目完全基于Winforms实现,采用的技术方案如下:

  • 所有元素在Paint事件中绘制到作为Canvas的单个PictureBox控件上
  • 选择框使用叠加在画布上方的控件作为拖拽柄角点的引导载体,后续会将该控件设为不可见
  • 自定义了支持缩放的ScaledPictureBox控件,放置在开启自动滚动的Panel容器内,缩放和平移基础功能运行正常
  • 在画布ScaledPictureBox上方叠加了另一个同类型控件作为选择框载体

问题现象

100%缩放比例下平移画布运行正常,缩放后再平移时,选择框位置会出现偏移。

相关代码

自定义ScaledPictureBox控件代码

Imports System.Drawing.Drawing2D

Partial Public Class ScaledPictureBox
    Inherits PictureBox

    Public Property ScaleM As Matrix
    Private Property Zoom As Single
    Private Property OriginalSize As Size

    Public Sub New()
        ScaleM = New Matrix()
        SizeMode = PictureBoxSizeMode.Zoom
    End Sub

    Public Sub InitImage()
        OriginalSize = Me.Size
        Size = OriginalSize
        SetZoom(100)
    End Sub

    Public Sub SetZoom(ByVal zoomfactor As Single)
        If zoomfactor <= 0 Then Throw New Exception("Zoom must be positive")
        Dim oldZoom As Single = Zoom
        Zoom = zoomfactor / 100.0F
        ScaleM.Reset()
        ScaleM.Scale(Zoom, Zoom)
        If OriginalSize <> Size.Empty Then Size = New Size(CInt((OriginalSize.Width * Zoom)), CInt((OriginalSize.Height * Zoom)))
    End Sub

    Public Function ScalePoint(ByVal pt As PointF) As PointF
        Return New PointF(pt.X / Zoom, pt.Y / Zoom)
    End Function

End Class

测试窗体代码

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Canvas.InitImage()
        Selector.InitImage()
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll

        Canvas.SetZoom(CInt(TrackBar1.Value))
        Selector.SetZoom(CInt(TrackBar1.Value))

        Canvas.Invalidate()
        Selector.Invalidate()

    End Sub

    Dim moving As Boolean = False
    Dim CanvasClickPoint As New Point
    Dim SelectorLocation As New Point

    Private Sub Canvas_MouseDown(sender As Object, e As MouseEventArgs) Handles Canvas.MouseDown

        If (ModifierKeys And Keys.Control) = Keys.Control Then
            moving = True
            CanvasClickPoint = e.Location
            SelectorLocation = Selector.Location
        End If

    End Sub

    Private Sub Canvas_MouseMove(sender As Object, e As MouseEventArgs) Handles Canvas.MouseMove

        If moving Then

            Dim CanvasNewPoint As Point = New Point(Canvas.Left + (e.Location.X - CanvasClickPoint.X),
                                                      Canvas.Top + (e.Location.Y - CanvasClickPoint.Y))

            SelectorLocation.X = SelectorLocation.X + (e.Location.X - CanvasClickPoint.X)
            SelectorLocation.Y = SelectorLocation.Y + (e.Location.Y - CanvasClickPoint.Y)

            Dim SelectorLocationPointf As PointF = SelectorLocation
            SelectorLocation = Point.Round(Selector.ScalePoint(SelectorLocationPointf))
            Selector.Location = SelectorLocation

            Canvas.Location = CanvasNewPoint

        End If
    End Sub

    Private Sub Canvas_MouseUp(sender As Object, e As MouseEventArgs) Handles Canvas.MouseUp
        moving = False
    End Sub

End Class

问题排查说明

猜测问题出在Selector.ScalePoint方法的使用上,选择框的位置应该需要做对应的缩放适配,否则非100%比例下就会偏移,但对这部分的数学计算逻辑不太熟悉,ScaledPictureBox的相关代码是参考实现的,大致能理解ScalePoint的设计思路但无法定位具体问题。


内容的提问来源于stack exchange,提问作者stigzler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 16:15:02