在移动事件时形成闪烁 [英] form flickers when on move event
问题描述
我编写了以下程序来移动和停靠无边框窗口:
I wrote the following procedure to move and dock a borderless window:
Public Class frmNavigation
Inherits Form
'Declarations to allow form movement on mouse down
Private IsFormBeingDragged As Boolean = False
Private MouseDownX As Integer
Private MouseDownY As Integer
Dim Xs As Integer
Dim Ys As Integer
Dim DockScale As Integer
Private Sub frmNavigation_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
'This procedure allows the user to move the form when the
'mouse button is down. The form does not have borders, so it
'needs to be coded to move.
If e.Button = MouseButtons.Left Then
IsFormBeingDragged = True
MouseDownX = e.X
MouseDownY = e.Y
End If
End Sub
Private Sub frmNavigation_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
'This procedure allows the user to move the form when the
'mouse button is up. The form does not have borders, so it
'needs to be coded to move.
If e.Button = MouseButtons.Left Then
IsFormBeingDragged = False
End If
End Sub
Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
'This procedure allows the user to move the form when the
'mouse button is dragging the form. The form does not have borders, so it
'needs to be coded to move.
Dim curScreen As Screen
curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
Dim height As Integer = curScreen.Bounds.Height
Dim width As Integer = curScreen.Bounds.Width
width = curScreen.WorkingArea.Width
height = curScreen.WorkingArea.Height
If IsFormBeingDragged Then
Dim temp As System.Drawing.Point = New System.Drawing.Point()
Xs = MouseDownX
Ys = MouseDownY
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
到目前为止,这是按设计工作的,它移动表格没有任何问题。当我添加代码以将表单停靠在鼠标移动事件下时,问题就开始了:
So far, this works as designed, it moves the form without any issue whatsoever. The issue starts when I add code to dock the form under the mouse move event as:
Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
'This procedure allows the user to move the form when the
'mouse button is dragging the form. The form does not have borders, so it
'needs to be coded to move.
Dim curScreen As Screen
curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
Dim height As Integer = curScreen.Bounds.Height
Dim width As Integer = curScreen.Bounds.Width
width = curScreen.WorkingArea.Width
height = curScreen.WorkingArea.Height
If IsFormBeingDragged Then
Dim temp As System.Drawing.Point = New System.Drawing.Point()
Xs = MouseDownX
Ys = MouseDownY
temp.X = Me.Location.X + (e.X - MouseDownX)
temp.Y = Me.Location.Y + (e.Y - MouseDownY)
Me.Location = temp
temp = Nothing
End If
If IsFormBeingDragged = True And e.Button = MouseButtons.Left Then
'if the drag flag is true and left mouse button is pressed...
'set Top side docking
If Me.Top + (MouseDownY - Ys) < DockScale Then
Me.Top = 0
Exit Sub
End If
'set bottom side docking
If Me.Top + (MouseDownY - Ys) + Me.Height > (height - DockScale) Then
Me.Top = height - Me.Height
Exit Sub
End If
'move the form finally
Me.Left = Me.Left + (MouseDownX - Xs)
Me.Top = Me.Top + (e.Y - Ys)
End If
End Sub
当我添加对接代码并尝试移动表单时,它会移动并停靠,但是当它按住时它会像疯了一样闪烁鼠标向下移动。我不明白为什么会发生这种情况,这是我第一次涉足这样的事情,所以我不确定我哪里出错。
When I add the code for docking and I try to move the form, it moves and it docks, but it flickers like crazy when holding the mouse down and moving. I can't see why this happens, is the first time I dabble with something like this, so I am not sure where I am going wrong.
推荐答案
一个好的防止闪烁的方法是使用双缓冲: http://en.wikipedia.org/wiki/ Double_buffering#Double_buffering [ ^ ]。 />
以下是您可以使用的属性: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered%28v=vs.110%29.aspx [ ^ ]。
如果你设置if为true,双缓冲将是应用,一切都已经实现。
-SA
A good way to prevent flicker is using double buffering: http://en.wikipedia.org/wiki/Double_buffering#Double_buffering[^].
Here is the property you can use: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered%28v=vs.110%29.aspx[^].
If you set if to true, double buffering will be applied, everything is already implemented for you.
—SA
这篇关于在移动事件时形成闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!