I have this code on a form class so that to prevent it to be movable out of the bounds of the working area during runtime. I don't know why, but when I move the form vertically I get a flickering. This form is TeachingFrm. I also have a central form which is the enviroment with a strech background image layout picture, it's not mdi parent, but it is always maximized and unmovable as I want it. The TeachingFrm changes at height every time I click on the next exercise only if the text is bigger than the previous exercise. Anyway, are there any suggestions how to prevent the vertical flickering? Any help will be much appreciated. Thank you so much in advanced.
Private Const WM_MOVING As Integer = &H216
Private Const [TRUE] As Integer = 1
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
Public Overrides Function ToString() As String
Return String.Format("{{Left={0},Top={1},Right={2},Bottom={3}}}",
Me.Left, Me.Top, Me.Right, Me.Bottom)
End Function
End Structure
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_MOVING Then
'The current bounds of the form.
Dim currentFormBounds As Rectangle = Me.Bounds
'The proposed new bounds of the form after the move.
Dim newFormBounds = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
'The bounds of the primary monitor.
Dim screenBounds As Rectangle = Screen.PrimaryScreen.WorkingArea
screenBounds.Y = CentralFrm.Location.Y + TeachingFrmToolStrip.Height
screenBounds.Height = screenBounds.Height - screenBounds.Y
'screenBounds.Height = CentralFrmHeight
'screenBounds.Width = CentralFrmWdth
'Don't let the form move beyond the bounds of the screen in a horizontal direction.
If newFormBounds.Left < screenBounds.Left OrElse
newFormBounds.Right > screenBounds.Right Then
newFormBounds.Left = currentFormBounds.Left
newFormBounds.Right = currentFormBounds.Right
End If
'Don't let the form move beyond the bounds of the screen in a vertical direction.
If newFormBounds.Top < screenBounds.Top OrElse
newFormBounds.Bottom > screenBounds.Bottom Then
newFormBounds.Top = currentFormBounds.Top
newFormBounds.Bottom = currentFormBounds.Bottom + Me.Height
End If
'Replace the proposed bounds with the calculated bounds.
Marshal.StructureToPtr(newFormBounds,
m.LParam,
False)
m.Result = New IntPtr([TRUE])
End If
'Pass the message on.
MyBase.WndProc(m)
End Sub
Private Const WM_MOVING As Integer = &H216
Private Const [TRUE] As Integer = 1
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
Public Overrides Function ToString() As String
Return String.Format("{{Left={0},Top={1},Right={2},Bottom={3}}}",
Me.Left, Me.Top, Me.Right, Me.Bottom)
End Function
End Structure
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_MOVING Then
'The current bounds of the form.
Dim currentFormBounds As Rectangle = Me.Bounds
'The proposed new bounds of the form after the move.
Dim newFormBounds = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
'The bounds of the primary monitor.
Dim screenBounds As Rectangle = Screen.PrimaryScreen.WorkingArea
screenBounds.Y = CentralFrm.Location.Y + TeachingFrmToolStrip.Height
screenBounds.Height = screenBounds.Height - screenBounds.Y
'screenBounds.Height = CentralFrmHeight
'screenBounds.Width = CentralFrmWdth
'Don't let the form move beyond the bounds of the screen in a horizontal direction.
If newFormBounds.Left < screenBounds.Left OrElse
newFormBounds.Right > screenBounds.Right Then
newFormBounds.Left = currentFormBounds.Left
newFormBounds.Right = currentFormBounds.Right
End If
'Don't let the form move beyond the bounds of the screen in a vertical direction.
If newFormBounds.Top < screenBounds.Top OrElse
newFormBounds.Bottom > screenBounds.Bottom Then
newFormBounds.Top = currentFormBounds.Top
newFormBounds.Bottom = currentFormBounds.Bottom + Me.Height
End If
'Replace the proposed bounds with the calculated bounds.
Marshal.StructureToPtr(newFormBounds,
m.LParam,
False)
m.Result = New IntPtr([TRUE])
End If
'Pass the message on.
MyBase.WndProc(m)
End Sub