0

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
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Eric van Loon
  • 103
  • 1
  • 9

2 Answers2

0

For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setparent

You change styles with https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptrw. Normally you hide the window, change the styles, then unhide the window.

Here is some sample code

https://winsourcecode.blogspot.com/2021/04/this-uses-inbuilt-compilers-in-windows.html

Public Declare Function SetWindowLongPtrW Lib "user32" (ByVal hwnd As IntPtr, ByVal Index As Integer, ByVal NewValue As Integer) As Integer
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer


Public Const GWL_EXSTYLE = -20
Public Const GWL_STYLE = -16

Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40
Public Const SWP_HIDEWINDOW = &H80
Public Const SWP_NOOWNERZORDER = &H200      '  Don't do owner Z ordering
Public Const SWP_NOREDRAW = &H8
Public Const SWP_NOREPOSITION = &H200
Public Const SWP_NOZORDER = &H4



            Ret = SetWindowLongPtrW(hWindows, GWL_EXSTYLE, ExStyle)
            If (Ret = 0 And err.LastDLLError <> 0) Then MsgBox("SetWindowLongPtrW is " & Err.LastDllError)
            err.clear
            Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER + SWP_SHOWWINDOW)
Lundt
  • 142
  • 1
  • 1
  • 3
  • Hi @Lundt, Thank you very much for your answer! I'm not a very experienced code guy, so can you please show me how I should do that in my code example above? I guess in my case, I'm starting notepad and move it into the new parent from the desktop, so I need to clear WS_POPUP and set the WS_CHILD style, but I don't know how to do that. Thanks you very much for your help! Kind regards, Eric – Eric van Loon Sep 17 '22 at 08:49
  • You change styles with https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptrw – Lundt Sep 17 '22 at 18:24
  • Can somebody please show me _how_ to clear the WS_POPUP style and set the WS_CHILD style? I have never used this and I'm really no VB expert. Thanks for any help! Kind regards, Eric – Eric van Loon Sep 28 '22 at 14:21
0

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.

Eric van Loon
  • 103
  • 1
  • 9