1

I have created 3 different application

Application 1: It is a WPF application it has 1 Window(MainWindow) which display "Hello Word".

Application 2: It is a WPF Application This application will create an instance of MainWindow of Application 1. like below

MainWindow window = new MainWindow();
//And it will store it's window handle to some file
string filePath = @"c:\windowHandle.txt";
var windowInteropHelper = new WindowInteropHelper(window);
File.WriteAllText(filePath, windowInteropHelper.EnsureHandle().ToString());

Application 3: This is again an WPF application which has 2 buttons "Show Application 1" and "Hide Application 1"

private void show_Click(object sender, RoutedEventArgs e)
{
    ShowWindow(GetWindowHandle(), 5);            
}        

private void hide_Click(object sender, RoutedEventArgs e)
{
    ShowWindow(GetWindowHandle(), 0);
}

private int GetWindowHandle()
{
    string handle = File.ReadAllText(@"C:\windowHandle.txt");
    return Convert.ToInt32(handle);
}

[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int nCmdShow);

Now I will launch Application 2 and Application 3. Once I click on "Show Application 1" button from Application 3, The window(Application 1) is coming with the black background. it is not showing "Hello world". It shows the window title but rest of the window is black.

If anyone has any idea how to fix it? Please let me know.

Please let me know if you have any query regarding my query :).

  • Just a quick guess... you havent initialized the controls on the Window..just try to show the window and hide it again from application 2 if I'm right, that should do the trick – FastJack Jan 02 '19 at 14:06
  • okay, got it working.. kind of... it works only if you shwo the window in app2 and hide it from app3 ... after that, the show from app3 works... if you hide from app2 id doesnt... seems like the hide() from wpf does more than just a simple hide – FastJack Jan 02 '19 at 14:29
  • so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3 – FastJack Jan 02 '19 at 14:30
  • Thanks @FastJack3. I also observed this that if we call show in app2 then it will work. I was thinking that is this the only way. Can't we do it without showing in app2. – Ashish Agrawal Jan 02 '19 at 17:44
  • As WPF seems to do a lot of initializing stuff in its own Show() function, no. As this code is not executed if you just do a simple WinAPI Show call – FastJack Jan 03 '19 at 07:00

1 Answers1

0

confirmed working

App2:

MainWindow window = new MainWindow();
window.Show();
//And it will store it's window handle to some file
string filePath = @"c:\windowHandle.txt";
var windowInteropHelper = new WindowInteropHelper(window);
File.WriteAllText(filePath, windowInteropHelper.EnsureHandle().ToString());
ShowWindow(windowInteropHelper.Handle.ToInt32(), 0);

App3 as-is

EDIT:

from .net ReferenceSource:

// RootVisual is not set until Show.
// Only set RootVisual when we are going to show the window.
if (!HwndCreatedButNotShown)
{
    SetRootVisualAndUpdateSTC();
}

the comment says it all.. ;) if you just use winapi, no RootVisual is set...

FastJack
  • 866
  • 1
  • 10
  • 11
  • Yes, it works. but this one looks like a workaround. Is this the only way do? – Ashish Agrawal Jan 03 '19 at 05:00
  • if you want to show the window with ShowWindow, yes. It seems that WPF is doing more than just a simple hide on Hide(). This looks like some controls are destroyed or at least the Renderer for these Controls. The only other way that comes to my mind is to develop some kind of API that communicates with app2 and lets app2 show the window. – FastJack Jan 03 '19 at 06:56
  • Can you please add the link for the .net reference you added. – Ashish Agrawal Jan 03 '19 at 09:23
  • I got the link - https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Window.cs,ede3223d03bf1be0 – Ashish Agrawal Jan 03 '19 at 09:34