'Copy main menu from one window to other

I am trying to set a new parent window (host window) for the another window (client window) that belongs to other process. Also, the is a requirement to duplicate main menu of the client window so that the host window should have the same working main menu as the calient window has. I managed to set the client window menu to the host window, but the host menu does not work. It is possible to navigate through menu, but is does not work (f.e. it not possible to open/create new file).

The question is if it possible to do this at all and if it is, what did I miss?

There is a code snippet:

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

    [DllImport("user32.dll")]
    private static extern IntPtr GetMenu(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern bool SetMenu(IntPtr hWnd, IntPtr hMenu);

    private Process notepadProcess;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        ProcessStart();
        Thread.Sleep(100);
        EmbedNotepadWindow();
    }

    private void ProcessStart()
    {
        notepadProcess = new Process();
        notepadProcess.StartInfo.FileName = "notepad.exe";
        notepadProcess.Start();
    }

    private void EmbedNotepadWindow()
    {
        SetParent(notepadProcess.MainWindowHandle, panel1.Handle);
        var menuHandle = GetMenu(notepadProcess.MainWindowHandle);
        SetMenu(this.Handle, menuHandle);
    }
}

Copy main menu from Client to Host window



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source