Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Module MDIUtil
#Region " MDI Activate Child "
<System.Security.SuppressUnmanagedCodeSecurity>
<System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)>
Public Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
Public Const WM_MDINEXT As Integer = &H224
''' <summary>
''' Attiva la form MDI child specificata eviatando i problemi di flicker/flashing
''' che si avrebbero usando semplicemente il metodo <see cref="Form.Activate">Activate</see>.
''' Vedi post http://www.codeproject.com/Articles/19524/Workaround-for-flicker-flashing-when-programmatica
''' </summary>
''' <param name="form">Form MDI partent</param>
''' <param name="childToActivate">Form MDI child da attivre</param>
<System.Runtime.CompilerServices.Extension>
Public Sub MyActivateMdiChild(form As Form, childToActivate As Form)
If Not childToActivate Is form.ActiveMdiChild Then
Dim mdiClient As MdiClient = GetMDIClient(form)
If mdiClient Is Nothing Then Return
Dim count As Integer = form.MdiChildren.Length
Dim childForm As Control = Nothing
' next or previous MDIChild form
Dim pos As Integer = mdiClient.Controls.IndexOf(childToActivate)
If pos < 0 Then Return 'Throw New InvalidOperationException("MDIChild form not found")
If mdiClient.Controls.Count <= 1 Then
' C'è un solo child, quindi attiva quello in modo classico
childToActivate.Focus()
Return
End If
If pos = 0 Then
childForm = mdiClient.Controls(1)
Else
' get next and activate previous
childForm = mdiClient.Controls(pos - 1)
End If
' get previous and activate next
' flag indicating whether to activate previous or next MDIChild
Dim direction As New IntPtr(If(pos = 0, 1, 0))
' bada bing, bada boom
SendMessage(mdiClient.Handle, WM_MDINEXT, childForm.Handle, direction)
End If
End Sub
Public Function GetMDIClient(form As Form) As MdiClient
For Each c As Control In form.Controls
If TypeOf c Is MdiClient Then
Return DirectCast(c, MdiClient)
End If
Next
Return Nothing 'Throw New InvalidOperationException("No MDIClient")
End Function
#End Region
End Module