I am working on a project which contains MDI child forms. I have make my MDI child forms stop moving when user is trying to drag them out of MDI parent borderless form's edges. But, if user insists, MDI child form flickering like crazy!!!
Note that I have already set my forms to DoubleBuffered = True
and I have also add me.Refresh()
and me.parentform.Refresh()
at the end of event.
Any idea what else can I do?
Here is an edited example of my code...
First we have the custom title bar control and this is the needed part of it's code:
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class cmosTitleBar
Region "Custom events."
Public Event FormTitleBar_DoubleClick_Plus(sender As Object, e As EventArgs)
Public Event FormTitleBar_MouseDown_Plus(sender As Object, e As EventArgs)
Public Event FormTitleBar_MouseMove_Plus(sender As Object, e As EventArgs)
Public Event FormTitleBar_MouseEnter_Plus(sender As Object, e As EventArgs)
Public Event FormTitleBar_MouseLeave_Plus(sender As Object, e As EventArgs)
End Region
Region "Form Code."
Dim NewPoint As New System.Drawing.Point
Dim X, Y As Integer
Region "FormTitleBar Events."
Private Sub FormTitleBar_DoubleClick(sender As Object, e As EventArgs) Handles FormTitleBar.DoubleClick
RaiseEvent FormTitleBar_DoubleClick_Plus(sender, e)
Call PreventFlickering()
End Sub
Private Sub FormTitleBar_MouseDown(sender As Object, e As MouseEventArgs) Handles FormTitleBar.MouseDown, FormIcon.MouseDown, MyBase.MouseDown
If Not ParentForm.WindowState = FormWindowState.Maximized Then
X = Control.MousePosition.X - ParentForm.Location.X
Y = Control.MousePosition.Y - ParentForm.Location.Y
End If
RaiseEvent FormTitleBar_MouseDown_Plus(sender, e)
End Sub
Private Sub FormTitleBar_MouseMove(sender As Object, e As MouseEventArgs) Handles FormTitleBar.MouseMove, FormIcon.MouseMove, MyBase.MouseMove
If Not ParentForm.WindowState = FormWindowState.Maximized Then
If e.Button = Windows.Forms.MouseButtons.Left Then
NewPoint = Control.MousePosition
NewPoint.X -= (X)
NewPoint.Y -= (Y)
ParentForm.Location = NewPoint
End If
End If
Call PreventChildMoveOut()
RaiseEvent FormTitleBar_MouseMove_Plus(sender, e)
RaiseEvent FormIcon_MouseMove_Plus(sender, e)
Call PreventFlickering()
End Sub
Private Sub FormTitleBar_MouseEnter(sender As Object, e As EventArgs) Handles FormTitleBar.MouseEnter, FormIcon.MouseEnter
If ParentForm.WindowState = FormWindowState.Normal Then
FormTitleBar.Cursor = Cursors.NoMove2D
FormIcon.Cursor = Cursors.NoMove2D
Else
FormTitleBar.Cursor = Cursors.Default
FormIcon.Cursor = Cursors.Default
End If
RaiseEvent FormTitleBar_MouseEnter_Plus(sender, e)
End Sub
Private Sub FormTitleBar_MouseLeave(sender As Object, e As EventArgs) Handles FormTitleBar.MouseLeave
RaiseEvent FormTitleBar_MouseLeave_Plus(sender, e)
End Sub
End Region
End Region
Region "Custom subs."
Private Sub PreventFlickering()
If Me.ParentForm.IsMdiChild = True Then
Me.ParentForm.Refresh()
Me.ParentForm.ParentForm.Refresh()
Else
Me.ParentForm.Refresh()
End If
End Sub
Private Sub PreventChildMoveOut()
If ParentForm.IsMdiChild = True Then
If ParentForm.Left < ParentForm.MdiParent.ClientRectangle.Left Then
ParentForm.Left = ParentForm.MdiParent.ClientRectangle.Left
If ParentForm.Top < ParentForm.MdiParent.ClientRectangle.Top Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Top
ElseIf ParentForm.Bottom > ParentForm.MdiParent.ClientRectangle.Height - 98 Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Bottom - ParentForm.Height - 98
End If
ElseIf ParentForm.Right > ParentForm.MdiParent.ClientRectangle.Width Then
ParentForm.Left = ParentForm.MdiParent.ClientRectangle.Right - ParentForm.Width
If ParentForm.Top < ParentForm.MdiParent.ClientRectangle.Top Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Top
ElseIf ParentForm.Bottom > ParentForm.MdiParent.ClientRectangle.Height - 98 Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Bottom - ParentForm.Height - 98
End If
ElseIf ParentForm.Top < ParentForm.MdiParent.ClientRectangle.Top Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Top
ElseIf ParentForm.Bottom > ParentForm.MdiParent.ClientRectangle.Height - 98 Then
ParentForm.Top = ParentForm.MdiParent.ClientRectangle.Bottom - ParentForm.Height - 98
End If
End If
End Sub
End Region
End Class
Here is the needed code of my borderless MDI parent form which contains my custom title bar control:
Public Class MainForm
Region "Form Code."
Private Sub SettingsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SettingsToolStripMenuItem.Click
SettingsForm.MdiParent = Me
SettingsForm.Show()
End Sub
End Region
End Class
Into my borderless MDI child forms which also contains my custom title bar control there is no code for this action cause everything (until now) happens into custom title bar control's code.