简体   繁体   English

单击并拖动面板时移动表单

[英]Move form when panel is clicked and dragged

I'm trying to make my form move (like you move windows on other applications) when I click and hold my panel element (which serves as top of the window cause I used custom made GUI). 当我单击并按住我的面板元素(由于使用自定义GUI时,它作为窗口的顶部)时,我试图使表单移动(就像您在其他应用程序上移动窗口一样)。

Anyways, I managed to make it move but the problem is, the form moves to my current cursor position when I click on it instead of moving like a regular window (cause it reads the cursor's position and sets the form's location to that position, thus moving it unnecessary). 无论如何,我设法使它移动,但是问题是,当我单击它时,窗体移动到了当前光标位置,而不是像常规窗口一样移动(因为它读取了光标的位置并将窗体的位置设置为该位置,因此无需移动)。

This is my code, what can I do to get it to work properly? 这是我的代码,如何使它正常工作?

Dim Moving As Boolean = False


 Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
    Moving = True
    MouseMoveTimer.Start()
End Sub

Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
    Moving = False
    MouseMoveTimer.Stop()
End Sub

Private Sub MouseMoveTimer_Tick(sender As Object, e As EventArgs) Handles MouseMoveTimer.Tick
    Me.Location = Cursor.Position
End Sub

New code 新密码

Dim diff As Point = Cursor.Position - curpoint
        Me.Location -= diff
        curpoint = Cursor.Position

You can save the location of the cursor in each timer tick, and see how much it changed in the next tick. 您可以在每个计时器刻度中保存光标的位置,并在下一个刻度中查看其变化量。 Then add that change to the Form's position. 然后将该更改添加到窗体的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM