2017-10-14 176 views
-1

您可以在winform中使用此代碼打開記事本:打開記事本

public partial class Form1 : Form 
    { 
     [DllImport("user32.dll")] 
     static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 
     public Form1() 
     { 
      InitializeComponent(); 

      Process p = Process.Start("notepad.exe"); 
      p.WaitForInputIdle(); // Allow the process to open it's window 
      SetParent(p.MainWindowHandle, this.Handle); 
     } 
    } 

在WPF this.handle無法識別。什麼是這個.Handle的WPF版本?

而如何在WPF屏幕中沒有關閉按鈕的情況下全屏打開記事本?

+0

WPF的版本是'新System.Windows.Interop.WindowInteropHelper(本).Handle'其中'this'是WPF'Window'的實例。 – Evk

+0

@Evk確實多數民衆贊成在它,但嗯猜猜它不會在WPF中工作..記事本打開了WPF窗口。 – user7849697

+0

對於附加問題,您可以參考[使用c#刪除外部應用程序的標題欄](https://stackoverflow.com/questions/2825528/removing-the-title-bar-of-external-application-using- C-尖銳)。 – Iron

回答

2

當寡婦擁有LoadedSourceInitialized時,您可以使用WindowInteropHelper獲得WPF窗口的句柄。在構造函數方法中,句柄尚未創建,它返回一個零句柄。只要有一個嘗試:

public MainWindow() 
{ 
    InitializeComponent(); 

    Loaded += (s, e) => 
    { 
     Process p = Process.Start("notepad.exe"); 
     p.WaitForInputIdle(); // Allow the process to open it's window 
     SetParent(p.MainWindowHandle, new System.Windows.Interop.WindowInteropHelper(this).Handle); 
    }; 
} 

另外,

protected override void OnSourceInitialized(EventArgs e) 
{ 
    base.OnSourceInitialized(e); 

    Process p = Process.Start("notepad.exe"); 
    p.WaitForInputIdle(); // Allow the process to open it's window 
    SetParent(p.MainWindowHandle, new System.Windows.Interop.WindowInteropHelper(this).Handle); 
} 
相關問題