2009-07-16 8 views
0

彼がマウスを離れると、ピクチャボックスはフォーム上の新しい位置を占めます。これは私はstackoverflowコミュニティのおかげでやったことがあります。をドラッグすると、ユーザはフォームの周りに複数のピクチャボックスをドラッグするオプションがあります。

私は次のことを実現したいと思います:のmouseup上

、ピクチャボックスの位置はある程度の量多分50か100の範囲内であれば(私は何台VB.netを知らないが)使用しています、私はそれをドロップすることを希望正確に定義された位置にある。あなたがヤフーゲームでチェッカーをプレイする場合のような並べ替えのように、正確に正方形に作品を正確に配置する必要はありません。

これがうまく機能vb.net

答えて

2

内の溶液で私を助けてください、私は(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が2許可された場所を定義します。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)の私は、コードは今OKだと思います(、周りVB.NET環境を持って変換されていませんコード例:http://www.developerfusion.com/tools/convert/csharp-to-vb/) –

+0

投稿例:o) –

+0

これは奇妙なことですが、投稿する前にコード(C#バリアント)をテストしました。あなたは私のサンプルのイベントハンドラにあなたの画像ボックスからマウスイベントを接続しましたか? –

関連する問題