簡體   English   中英

如何從另一個進程顯示WPF窗口

[英]How to Show a wpf window from another process

我創建了3個不同的應用程序

應用程序1:這是一個WPF應用程序,它具有1個顯示“ Hello Word”的窗口(MainWindow)。

應用程序2:它是WPF應用程序。此應用程序將創建應用程序1的MainWindow的實例。

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());

應用程序3:這又是一個WPF應用程序,具有2個按鈕“顯示應用程序1”和“隱藏應用程序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);

現在,我將啟動應用程序2和應用程序3。單擊應用程序3中的“顯示應用程序1”按鈕后,窗口(應用程序1)將帶有黑色背景。 它沒有顯示“ Hello world”。 它顯示了窗口標題,但窗口的其余部分為黑色。

如果有人知道如何解決? 請告訴我。

如果您對我的查詢有任何疑問,請告訴我:)。

確認工作

應用2:

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原樣

編輯:

來自.net ReferenceSource:

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

注釋說明了一切.. ;;)如果僅使用winapi,則不會設置RootVisual ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM