창을 이동시키는 방법
Form의 FormBorderStyle의 속성값이 None일 경우 테두리가 없습니다. 테두리가 없는 창은 이동을 시킬 수 없는데, 테두리 없는 창이 이동시키는 코드입니다.
폼을 이동시키는 클래스를 생성합니다. 이 클래스를 생성할 때는 이동시킬 Form 개체와 이동시킬 때 사용할 패널을 인자로 전달합니다.
Public Class MoveForm
Private WithEvents frm As Form
Private WithEvents control As Control
Private windowPoint As New Point
Private mousePoint As New Point
Private WithEvents timer As New Timer
Public Sub New(frm As Form, Optional ByVal sender As Control = Nothing)
Me.frm = frm
If (sender IsNot Nothing) Then
Me.control = sender
AddHandler Me.control.MouseDown, AddressOf StartMoveWindow
AddHandler Me.control.MouseUp, AddressOf EndMoveWindow
Else
AddHandler Me.frm.MouseDown, AddressOf StartMoveWindow
AddHandler Me.frm.MouseUp, AddressOf EndMoveWindow
End If
End Sub
' 창을 움직이기 시작합니다.
Private Sub StartMoveWindow(sender As Object, e As MouseEventArgs)
Me.windowPoint.X = Me.frm.Left
Me.windowPoint.Y = Me.frm.Top
Me.mousePoint.X = e.X
Me.mousePoint.Y = e.Y
timer.Interval = 10
timer.Start()
AddHandler timer.Tick, AddressOf MoveWindow
End Sub
' 창을 이동시킵니다.
Private Sub MoveWindow()
Me.frm.Left = System.Windows.Forms.Control.MousePosition.X - mousePoint.X
Me.frm.Top = System.Windows.Forms.Control.MousePosition.Y - mousePoint.Y
End Sub
' 창 이동을 중지합니다.
Private Sub EndMoveWindow()
timer.Stop()
End Sub
End Class
이 클래스를 사용하면 쉽게 움직이는 창을 구현할 수 있습니다. 움직이는 창을 사용하고자 하는 곳에서 다음과 같이 이 클래스를 호출하면서 Form 개체와 패널 개체를 전달해 주면 됩니다.
Dim move As New MoveForm(Me, Panel1)