如何在Windows窗体应用程序中嵌入记事本++以及如何控制它

本文关键字:何控制 控制 Windows 窗体 应用程序 记事本 | 更新日期: 2023-09-27 18:25:50

我的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private const int WM_SYSCOMMAND = 274; private const int SC_MAXIMIZE = 61488;
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    private void button1_Click(object sender, EventArgs e)
    {
        Process proc;
        proc = Process.Start("Notepad++.exe");
        proc.WaitForInputIdle();          
        SetParent(proc.MainWindowHandle,panel1.Handle);
        //SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
    }
}

当我执行notepad.exe而不是notepad++.exe时,它工作得很好。普通记事本位于Windows Form的面板内。但当我使用notepad++.exe时,它在面板内部看不到,而是作为一个不同的窗口打开。我不想要这个。我的偏好是notepad++嵌入我的Windows Form面板中,我想通过它来控制notepad++

如何在Windows窗体应用程序中嵌入记事本++以及如何控制它

在setParent之后添加睡眠将起作用。

SetParent(proc.MainWindowHandle,panel1.Handle);线程睡眠(500)