How to Show a wpf window from another process












0















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 :).










share|improve this question























  • 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 2 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 2 at 14:29











  • so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

    – FastJack
    Jan 2 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 2 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 3 at 7:00
















0















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 :).










share|improve this question























  • 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 2 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 2 at 14:29











  • so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

    – FastJack
    Jan 2 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 2 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 3 at 7:00














0












0








0








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 :).










share|improve this question














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 :).







c# wpf interopservices






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 13:23









Ashish AgrawalAshish Agrawal

88




88













  • 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 2 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 2 at 14:29











  • so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

    – FastJack
    Jan 2 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 2 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 3 at 7:00



















  • 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 2 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 2 at 14:29











  • so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

    – FastJack
    Jan 2 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 2 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 3 at 7:00

















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 2 at 14:06







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 2 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 2 at 14:29





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 2 at 14:29













so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

– FastJack
Jan 2 at 14:30





so workaround would be: Show() from app2 > ShowWindow(wnd, 0) from app2 > ShowWindow(wnd, 5) from app3

– FastJack
Jan 2 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 2 at 17:44







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 2 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 3 at 7:00





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 3 at 7:00












1 Answer
1






active

oldest

votes


















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...






share|improve this answer


























  • Yes, it works. but this one looks like a workaround. Is this the only way do?

    – Ashish Agrawal
    Jan 3 at 5: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 3 at 6:56











  • Can you please add the link for the .net reference you added.

    – Ashish Agrawal
    Jan 3 at 9:23











  • I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

    – Ashish Agrawal
    Jan 3 at 9:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007165%2fhow-to-show-a-wpf-window-from-another-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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...






share|improve this answer


























  • Yes, it works. but this one looks like a workaround. Is this the only way do?

    – Ashish Agrawal
    Jan 3 at 5: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 3 at 6:56











  • Can you please add the link for the .net reference you added.

    – Ashish Agrawal
    Jan 3 at 9:23











  • I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

    – Ashish Agrawal
    Jan 3 at 9:34
















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...






share|improve this answer


























  • Yes, it works. but this one looks like a workaround. Is this the only way do?

    – Ashish Agrawal
    Jan 3 at 5: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 3 at 6:56











  • Can you please add the link for the .net reference you added.

    – Ashish Agrawal
    Jan 3 at 9:23











  • I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

    – Ashish Agrawal
    Jan 3 at 9:34














0












0








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...






share|improve this answer















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...







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 7:06

























answered Jan 2 at 14:40









FastJackFastJack

33828




33828













  • Yes, it works. but this one looks like a workaround. Is this the only way do?

    – Ashish Agrawal
    Jan 3 at 5: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 3 at 6:56











  • Can you please add the link for the .net reference you added.

    – Ashish Agrawal
    Jan 3 at 9:23











  • I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

    – Ashish Agrawal
    Jan 3 at 9:34



















  • Yes, it works. but this one looks like a workaround. Is this the only way do?

    – Ashish Agrawal
    Jan 3 at 5: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 3 at 6:56











  • Can you please add the link for the .net reference you added.

    – Ashish Agrawal
    Jan 3 at 9:23











  • I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

    – Ashish Agrawal
    Jan 3 at 9:34

















Yes, it works. but this one looks like a workaround. Is this the only way do?

– Ashish Agrawal
Jan 3 at 5:00







Yes, it works. but this one looks like a workaround. Is this the only way do?

– Ashish Agrawal
Jan 3 at 5: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 3 at 6:56





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 3 at 6:56













Can you please add the link for the .net reference you added.

– Ashish Agrawal
Jan 3 at 9:23





Can you please add the link for the .net reference you added.

– Ashish Agrawal
Jan 3 at 9:23













I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

– Ashish Agrawal
Jan 3 at 9:34





I got the link - referencesource.microsoft.com/#PresentationFramework/src/…

– Ashish Agrawal
Jan 3 at 9:34




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007165%2fhow-to-show-a-wpf-window-from-another-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'