VB.NET 2008/Visual Basic 2008

창을 이동시키는 방법

본클라쓰 2012. 6. 5. 11:42

 

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)

'VB.NET 2008 > Visual Basic 2008' 카테고리의 다른 글

열거형의 사용  (0) 2012.06.05
ReportViewer 배포 방법  (0) 2012.06.01
마우스 커서의 모양 변경  (0) 2012.05.30
HashTable의 키 기준 정렬  (0) 2012.05.29
스레딩 - 스레드 동기화  (0) 2011.06.18