Vb.net 运行时拖动

Vb.net 运行时拖动,vb.net,winforms,Vb.net,Winforms,用户可以选择在表单周围拖动多个图片框。当他放开鼠标时,picturebox将在窗体上呈现一个新的位置。多亏了stackoverflow社区,我成功地做到了这一点 我想实施以下措施: 在mouseup上,如果picturebox的位置在一定范围内(可能是50或100),我不知道VB.net使用的是什么单位,我希望它精确地放置在定义的位置。有点像如果你在yahoo游戏上下西洋棋,你不需要把棋子精确地放在正方形上,只需要大概 请帮我在vb.net中找到一个解决方案,这会很好,我认为cellSize是控

用户可以选择在表单周围拖动多个图片框。当他放开鼠标时,picturebox将在窗体上呈现一个新的位置。多亏了stackoverflow社区,我成功地做到了这一点

我想实施以下措施:

在mouseup上,如果picturebox的位置在一定范围内(可能是50或100),我不知道VB.net使用的是什么单位,我希望它精确地放置在定义的位置。有点像如果你在yahoo游戏上下西洋棋,你不需要把棋子精确地放在正方形上,只需要大概


请帮我在vb.net中找到一个解决方案,这会很好,我认为cellSize是控件捕捉到的分辨率,假设每个单元格都是正方形

先决条件:您需要有一个PictureBox或其他控件,以便在表单上移动。将下面的示例代码放入表单的代码中,并将该控件的MouseDown、MouseMove和MouseUp事件附加到这些事件处理程序。可以使用属性网格附加事件单击事件按钮,选择事件,然后使用组合框选择相应的事件处理程序

VB.NET:

Private Sub SetControlPosition(ByVal control As Control, ByVal targetPoint As Point, ByVal cellSize As Integer)
    Dim roundedLocation As New Point(CInt((Math.Round(CSng(targetPoint.X) / cellSize) * cellSize)), CInt((Math.Round(CSng(targetPoint.Y) / cellSize) * cellSize)))
    control.Location = roundedLocation
End Sub
如果希望控件位置捕捉到某些特定的预定义位置,可以这样做,而不是\u allowedLocations定义两个允许的位置;x=50,y=50和x=500,y=500:

C中的SetControlPosition方法作为额外奖励:

private void SetControlPosition(Control control, Point targetPoint, int cellSize)
{
    Point roundedLocation = new Point(
        (int)(Math.Round((float)targetPoint.X / cellSize) * cellSize),
        (int)(Math.Round((float)targetPoint.Y / cellSize) * cellSize)
        );
    control.Location = roundedLocation;
}

@阿芙罗翰:很抱歉;o我认为代码现在还可以,没有VB.NET环境,在这里转换代码:那里,完整示例发布:oThat's奇怪,我在发布之前测试了代码的C变体。您确实将图片框中的鼠标事件连接到我的示例中的事件处理程序?在窗体设计器中选择控件,然后显示属性网格我认为F4是默认键。在属性网格的工具栏中有一个工具按钮,其图标看起来像一个小闪光灯;这是事件按钮。如果单击,属性网格将显示控件的事件列表。在该列表中选择MouseDown可将事件连接到窗体中的事件处理程序。如果将我的示例代码粘贴到窗体中,则应在列表中找到PictureBox_MouseDown。在我的示例中,它将使控件捕捉到最近的位置,其中x和y是100的倍数。我想可以修改SetControlPosition,将控件的位置与允许的位置列表进行比较,找到最接近的位置。
Private _mouseDownLocation As Point = Point.Empty
Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    If (e.Button And MouseButtons.Left) = System.Windows.Forms.MouseButtons.Left Then
        _mouseDownLocation = e.Location
    End If
End Sub

Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    If (e.Button And MouseButtons.Left) = System.Windows.Forms.MouseButtons.Left Then
        Dim target As Control = DirectCast(sender, Control)
        target.Location = New Point(target.Location.X + e.Location.X - _mouseDownLocation.X, target.Location.Y + e.Location.Y - _mouseDownLocation.Y)
    End If
End Sub

Private Sub PictureBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    If (e.Button And MouseButtons.Left) = System.Windows.Forms.MouseButtons.Left Then
        Dim target As Control = DirectCast(sender, Control)
        ' Snap the control in place, to nearest 100x100 corner '
        SetControlPosition(target, target.Location, 100)
    End If
End Sub
private void SetControlPosition(Control control, Point targetPoint, int cellSize)
{
    Point roundedLocation = new Point(
        (int)(Math.Round((float)targetPoint.X / cellSize) * cellSize),
        (int)(Math.Round((float)targetPoint.Y / cellSize) * cellSize)
        );
    control.Location = roundedLocation;
}