Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900