【问题标题】:Open notepad in WPF window [duplicate]在WPF窗口中打开记事本[重复]
【发布时间】:2018-03-26 12:11:09
【问题描述】:

您可以使用以下代码在 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 无法识别。 this.Handle 的 WPF 版本是什么?

如何在 WPF 屏幕中没有关闭按钮的情况下全屏打开记事本?

【问题讨论】:

  • WPF 版本是 new System.Windows.Interop.WindowInteropHelper(this).Handle,其中 this 是 WPF Window 的实例。
  • @Evk 确实是这样,但嗯猜它不会在 WPF 中工作。记事本从 wpf 窗口中打开。
  • 你达到你想要的了吗? @user7849697

标签: c# wpf


【解决方案1】:

您可以使用 WindowInteropHelper 获取 WPF 窗口的句柄,而寡妇使用 LoadedSourceInitialized。在构造方法中,句柄还没有被创建,它返回的句柄为零。试一试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多