2009-07-16 50 views
0

用户可以选择在表单上拖动几个图片框。当他放开鼠标时,图片框将在表格上占据一个新的位置。这我已经成功完成了感谢stackoverflow社区。在运行期间拖动

我想实现如下:

上的mouseup,如果PictureBox的位置是一定量内大概有50或100(我不知道是什么单位VB.net)使用,我想它被丢弃完全处于确定的位置。有点像在雅虎游戏中使用跳棋,你不必将棋子恰好放在广场上。

请帮我在vb.net

回答

2

这会工作得很好,我认为一个解决方案(cellSize是“解决方案”,其控制会“啪”,假设每个小区广场)。

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

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 

如果你想控制的位置捕捉​​到一些具体的预定义位置,你可以这样做,而不是(_allowedLocations定义了两个允许位置; x=50, y=50x=500, y=500):

Private _allowedLocations As Point() = {New Point(50, 50), New Point(500, 500), New Point(700, 100)} 
Private Sub SetControlPosition(ByVal control As Control, ByVal targetPoint As Point, ByVal cellSize As Integer) 
    Dim shortestDistance As Integer = Integer.MaxValue 
    Dim nearestLocationIndex As Integer = -1 
    
    For i As Integer = 0 To _allowedLocations.Length - 1 
        Dim width As Integer = targetPoint.X - _allowedLocations(i).X 
        Dim height As Integer = targetPoint.Y - _allowedLocations(i).Y 
        Dim distance As Integer = CInt(Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2))) 
        If distance < shortestDistance Then 
            shortestDistance = distance 
            nearestLocationIndex = i 
        End If 
    Next 
    control.Location = _allowedLocations(nearestLocationIndex) 
End Sub 

实施例的代码调用方法(包括移动用鼠标控制):

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 

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; 
} 
+0

@avrohom:我们对此深感抱歉; O)我觉得现在该代码行(没有VB.NET的环境周围,转换代码在这里:http://www.developerfusion.com/tools/convert/csharp-to-vb/) – 2009-07-16 12:20:35