vb.netwinformsfocusmdi

How to detect a mouse click on an MDI child?


I use the code down below to start notepad and move it into panel1 on my form. When another application (running outside my project) is positioned in front of my form, I can click my form's title bar to move it into the foreground. But when I click the MDI child area where notepad is moved to, nothing happens. Is there a way to detect a click on the MDI child, so I can change the focus to my form too? Thanks for any help in advance!

Kind regards, Eric

Public Class Form1
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488
    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim proc As Process
        proc = Process.Start("notepad.exe")
        proc.WaitForInputIdle()
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
End Class

Solution

  • From the following post:

    The problem is that a hosted (re-parented) Window, when activated, doesn't cause the hosing Form to also activate, so it's brought to the foreground. The hosted Window is not exactly a child Window and the hosting Form doesn't receive any message from it.

    Follow the instructions specified in this post to fix the issue.