0

I have a small application that allows an external app to behave like a child form and open up in another form.

the code to do so is as follows

 Dim myfile As String = (Application.StartupPath & "\MyHelp.exe")
                MyClickedHelpProcess.StartInfo.FileName = (Application.StartupPath & "\MyHelp.exe")

MyClickedHelpProcess.Start()

                Await Task.Run(Sub()
                                   Dim timeout As DateTime = DateTime.Now.AddSeconds(10)
                                   While timeout > DateTime.Now AndAlso MyClickedHelpProcess.MainWindowHandle.Equals(IntPtr.Zero)
                                       Threading.Thread.Sleep(10)
                                   End While
                               End Sub)

                If (Not MyClickedHelpProcess.MainWindowHandle.Equals(IntPtr.Zero)) Then
                    SetParent(MyClickedHelpProcess.MainWindowHandle, Me.Handle)

end if

The code even works when I've added a Web browser control to the MyHelp.exe however if I add the following code to MyHelp.exe form load event the MyHelp.exe opens outside of the "parent like form". I cant figure out why or how to resolve this?

If My.Computer.FileSystem.FileExists("c:\MyHelp.html") Then
                
WebBrowser1.Navigate(New Uri("File:///c:/MyHelp.html"))

End If

If though I add a button to the MyHelp.exe form and place the following code in the button click event it dose work inside the makeshift "Parent form"...

WebBrowser1.Navigate(New Uri("File:///c:/MyHelp.html"))
LabRat
  • 1,996
  • 11
  • 56
  • 91
  • Cannot reproduce (not using the code presented here, since I doubt it can work - that `Await Task.Run()` thing is useless, try `[Process].WaitForInputIdle() Thread.Sleep(100)` instead). Anyway, post the .Net and VB.Net versions in use. – Jimi Sep 21 '21 at 18:53
  • + See the notes here: [Unhook Window into its original State](https://stackoverflow.com/a/65847818/7444103) and here: [Print an image of the content of a Panel excluding any external overlapping Window](https://stackoverflow.com/a/68895127/7444103) -- Also, not clear why, since it appears that you own the app represented by `MyHelp.exe`, you can modify it and it also looks like it's a WinForm app, you need to start that app as a Process instead of just importing the Form in this other app. – Jimi Sep 21 '21 at 19:00
  • Of course you can use, e.g., `Await Task.Delay(10)` instead of `Thread.Sleep()` -- Also add `WindowStyle = ProcessWindowStyle.Minimized` to the ProcessStartInfo settings. You may want to use `MoveWindow()` or `SetWindowPos()` to relocate the foreign Window inside your container. – Jimi Sep 21 '21 at 19:39
  • these are all great tips which I'm curious to try out. however my issue is with the as you better describe the foreign window. I am able to load this quite well I use this exmple on numerous other projects. however the issue this time round is why if i add a URL for it to navigate to during form load dose it not appear inside but in contrast to what I want outside of its host form/ window (the same happens btw if i use a message box. I could add a button to load the url once the foreign form has been created but I wanted this to happen during the load event of the foreign form? – LabRat Sep 21 '21 at 21:26
  • 1
    Because the Window takes more time to show up, even if the handle is already created, if you add code that is executed in the Load event. If you make the changes I described, remove that `Task.Run()` thing (which is useless) and navigate in `OnShown()` (or in the `Shown` event handler), it doesn't happen. Also, as described, add a call to `MoveWindow()` or `SetWindowPos()`. – Jimi Sep 21 '21 at 21:37
  • I'll try the onshow() through I had perviously also tried triggering the web brouwser by giving it a threading.sleep(5000) before continuing after the load event bur even then it appears outside of its parents/ host form, then, surely waiting for 5 seconds a form should of loaded before the web-brouwsers? However I will have to try your suggestion after I come back from work seeing as I'm a welder not a programmer :) – LabRat Sep 22 '21 at 06:34
  • The form shown event worked a treat... Didn't even know it existed Prior to I thought application startup was the first but there's all sorts like form activated and so on... thanks for the help now to test out that Task.run() that you mention ;) – LabRat Sep 22 '21 at 15:44

0 Answers0