Click here to Skip to main content
12,067,928 members (43,328 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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 9-Nov-13 16:51pm
qbndl8403

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 9 Nov 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100