2013-06-30 27 views
3

我有一個無邊界的窗體,我希望用戶能夠移動。我一直未能找到任何可以讓我這樣做的事情。允許用戶移動無邊框窗口

是否可以移動一個邊框設置爲None的窗口?

回答

6

介紹一個布爾變量,它保存當前拖動窗體的狀態和保存拖動起點的變量。然後OnMove相應地移動表單。由於這已經在其他地方得到解答,我只是複製&粘貼在這裏。

Class Form1 
    Private IsFormBeingDragged As Boolean = False 
    Private MouseDownX As Integer 
    Private MouseDownY As Integer 

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown 

     If e.Button = MouseButtons.Left Then 
      IsFormBeingDragged = True 
      MouseDownX = e.X 
      MouseDownY = e.Y 
     End If 
    End Sub 

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp 

     If e.Button = MouseButtons.Left Then 
      IsFormBeingDragged = False 
     End If 
    End Sub 

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove 

     If IsFormBeingDragged Then 
      Dim temp As Point = New Point() 

      temp.X = Me.Location.X + (e.X - MouseDownX) 
      temp.Y = Me.Location.Y + (e.Y - MouseDownY) 
      Me.Location = temp 
      temp = Nothing 
     End If 
    End Sub 
End Class 

http://www.dreamincode.net/forums/topic/59643-moving-form-with-formborderstyle-none/

+0

謝謝,這正是我一直在尋找。感謝你的幫助。 –

+0

+1提到的參考。 – NeverHopeless

1
Dim offSetX As Integer 
Dim offSetY As Integer 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    Me.Location = New Point(Cursor.Position.X - offSetX, Cursor.Position.Y - offSetY) 
End Sub 

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 
    offSetX = PointToClient(Cursor.Position).X 
    offSetY = PointToClient(Cursor.Position).Y 
    Timer1.Enabled = True 
End Sub 

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp 
    Timer1.Enabled = False 
End Sub 

被盜這是做吧XD

希望它可以幫助雖然=]

1

這樣做的另一種方式是處理WM_NCHITTEST稍微邋遢方式message。這允許您讓表單的一部分響應鼠標事件,因爲標題欄,邊框等會用於帶有邊框的窗口。例如,如果您的表單上有標籤,並且您在WM_NCHITTEST處理程序中返回HTCAPTION,則可以通過拖動此標籤來移動表單,就像您可以通過拖動其標題欄來移動常規窗口一樣。示例代碼請參見此Stack Overflow question

0

所有'簡單'的VB答案讓我的表單在多個屏幕上遍地跳躍。所以我在C#中派生這個來自相同的答案,它就像一個魅力:

Public Const WM_NCLBUTTONDOWN As Integer = 161 
Public Const HT_CAPTION As Integer = 2 

然後

<DllImport("User32")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer 

End Function 

<DllImport("User32")> Private Shared Function ReleaseCapture() As Boolean 

End Function 

最後

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 
    If (e.Button = MouseButtons.Left) Then 
     ReleaseCapture() 
     SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0) 
    End If 
End Sub