이 내가 생각하기에, 잘 작동의 솔루션으로 저를 도와주세요.
전제 조건 : 양식에서 이동하려는 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 _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;
}
@avrohom : 그것에 대해 죄송합니다; O) I 코드는 이제 OK라고 생각합니다 (주변 VB.NET 환경이 변환되지 않습니다 코드 예 : http://www.developerfusion.com/tools/convert/csharp-to-vb/) –
게시 된 전체 예제 : o) –
이상하게도, 게시하기 전에 코드 (C# 변형)를 테스트했습니다. . 그림 상자의 마우스 이벤트를 샘플의 이벤트 처리기에 연결 했습니까? –