I have created WinForm application which has webBrowser control docked with Fill property and redirects the user to the URL user provides.I am executing the winForm process from chrome Extension and getting the handle of chrome and changing the parent of my child process(WinForm) to chrome browser process.
I am losing the focus when I Minimize the chrome and restore again.
But on switching the tab or either from Alt+Tab to locate my winForm .exe the focus comes back.
How to set the focus of my .exe whenever the user comes to my active tab.
I have written the application in C# and used the Unmanaged code SetParent(child handle, parent handle) API to attach my .exe.
Some links I already tried :
https://www.codeproject.com/Articles/101367/Code-to-Host-a-Third-Party-Application-in-our-Proc
Docking Window inside another Window
Hosting external app in WPF window
Code Snippet :
Listening the below C# code snippet in a thread in an infinite while loop to get the messages from my main.js
private void SetAsParent()
{
try
{
Process[] p = Process.GetProcessesByName("chrome");
foreach (Process item in p)
{
if (!String.IsNullOrEmpty(item.MainWindowTitle))
{
if (item.MainWindowTitle.Contains("Some Title to match my condition to show the exe...."))
{
this.Top = 78;
this.Left = 0;
SetParent(this.Handle, item.MainWindowHandle);
this.Visible = true;
this.Show();
this.Focus();
webBrowser.Navigate(some URL);
webBrowser.Size = new Size(1366, 979);//dynamic values coming from chrome main.js message via postMessage()
}
}
else
{
this.Hide();
}
}
this.Size = new Size(1366, 979);
}
catch (Exception ex)
{
Debug.WriteLine("Inside Form Load : {0}", ex.Message.ToString());
}
}
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Chrome Extenion main.js :
chrome.windows.onFocusChanged.addListener(function() {
chrome.windows.getCurrent(function(window){ console.log(window.state); if(window.state == "normal") { Some message to port... } else if(window.state == "maximized") { Focus message to port... } else if(window.state == "minimized") { Minimised message to port.... } });
});
Expected Output :
On switching the tab the focus is retained but on minimizing and restored the focus is lost.