2009-07-16 5 views
0

사용자는 양식 주위에 여러 그림 상자를 끌 수 있습니다. 그가 마우스를 놓을 때, picturebox는 양식의 새로운 위치를 차지할 것입니다. 이 stackoverflow 커뮤니티 덕분에 성공적으로 한.런타임 중에 끌기

난 다음 구현하고자 다음에 PictureBox 위치가 내가 그것을 삭제할 수 싶습니다 사용 (내가 단위 VB.net가 무엇을 잘 모릅니다) 어쩌면 일부 금액 내에서 50 또는 100 인 경우와 mouseUp에

정확하게 정의 된 위치에 있어야합니다. yahoo 게임에서 체커를 사용하는 것과 같은 일종의 경우, 사각형에 정확히 조각을 배치하지 않아도됩니다.

은 ( cellSize는 "해결 방법"입니다 제어 것 "스냅", 각 셀은 정사각형이라고 가정) vb.net

답변

2

이 내가 생각하기에, 잘 작동의 솔루션으로 저를 도와주세요.

전제 조건 : 양식에서 이동하려는 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 _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 
01,235 (마우스로 제어를 이동 포함) 메소드를 호출
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 

예 코드 16,

(추가 보너스 등)의 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) I 코드는 이제 OK라고 생각합니다 (주변 VB.NET 환경이 변환되지 않습니다 코드 예 : http://www.developerfusion.com/tools/convert/csharp-to-vb/) –

+0

게시 된 전체 예제 : o) –

+0

이상하게도, 게시하기 전에 코드 (C# 변형)를 테스트했습니다. . 그림 상자의 마우스 이벤트를 샘플의 이벤트 처리기에 연결 했습니까? –

관련 문제