vb.net 允许用户移动无边框窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17392088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:05:38  来源:igfitidea点击:

Allow a user to move a borderless window

vb.net

提问by Jose M.

I have a form without borders that I would like the user to be able to move. I have not been able to find anything that would allow me to do so.

我有一个没有边框的表单,我希望用户能够移动它。我一直无法找到任何可以让我这样做的东西。

Is it possible to move a window with the border set to None?

是否可以移动边框设置为无的窗口?

回答by

Introduce a Boolean variable which holds the state if the form is currently dragged and variables which hold the starting point of the drag. Then OnMove move the form accordingly. As this has already been answered elsewhere, I just copy&paste it here.

引入一个布尔变量,该变量保存当前拖动表单时的状态,以及保存拖动起点的变量。然后 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

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

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

回答by Stephan VDH

All 'simple' VB answers made my form jump all over the place with multiple screens. So i derived this from same answer in C# and it works like a charm :

所有“简单”的 VB 答案都使我的表单在多个屏幕上到处跳跃。所以我从 C# 中的相同答案中得出了这个,它就像一个魅力:

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

then

然后

 <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

and finally

最后

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

回答by user959631

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

This is slightly scruffy way of doing it xD

这种做法有点邋遢 xD

Hope it helps though =]

希望它有帮助=]

回答by Ken Keenan

Another way of doing this is to handle the WM_NCHITTESTmessage. This allows you to have parts of your form respond to mouse events as the title bar, borders, etc. would for a window with borders. For example, if you have a label on your form and you return HTCAPTIONin the WM_NCHITTESThandler, you will be able to move the form by dragging this label just as you can move a regular window by dragging on its title bar. See this Stack Overflow questionfor example code.

另一种方法是处理WM_NCHITTEST消息。这允许您让表单的一部分响应鼠标事件,就像带有边框的窗口的标题栏、边框等一样。例如,如果您的表单上有一个标签并且您HTCAPTIONWM_NCHITTEST处理程序中返回,您将能够通过拖动此标签来移动表单,就像您可以通过拖动标题栏来移动常规窗口一样。有关示例代码,请参阅此堆栈溢出问题