用戶可以選擇在表單上拖動幾個圖片框。當他放開鼠標時,圖片框將在表格上佔據一個新的位置。這我已經成功完成了感謝stackoverflow社區。在運行期間拖動
我想實現如下:
上的mouseup,如果PictureBox的位置是一定量內大概有50或100(我不知道是什麼單位VB.net)使用,我想它被丟棄完全處於確定的位置。有點像在雅虎遊戲中使用跳棋,你不必將棋子恰好放在廣場上。
請幫我在vb.net
用戶可以選擇在表單上拖動幾個圖片框。當他放開鼠標時,圖片框將在表格上佔據一個新的位置。這我已經成功完成了感謝stackoverflow社區。在運行期間拖動
我想實現如下:
上的mouseup,如果PictureBox的位置是一定量內大概有50或100(我不知道是什麼單位VB.net)使用,我想它被丟棄完全處於確定的位置。有點像在雅虎遊戲中使用跳棋,你不必將棋子恰好放在廣場上。
請幫我在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
如果你想控制的位置捕捉到一些具體的預定義位置,你可以這樣做,而不是(_allowedLocations
定義了兩個允許位置; x=50, y=50
和x=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;
}
@avrohom:我們對此深感抱歉; O)我覺得現在該代碼行(沒有VB.NET的環境周圍,轉換代碼在這裏:http://www.developerfusion.com/tools/convert/csharp-to-vb/) – 2009-07-16 12:20:35