在另一个窗口内停靠窗口

J.H*_*rix 14 c# docking dllimport .net-2.0 winforms

我有一个winform应用程序(.NET 2.0 C#).从这个应用程序,我想启动另一个进程(另一个winform应用程序)并将其停靠到我的窗口(或至少让它看起来像停靠).到目前为止,我只能找到有关对接控件的信息,而不是单独进程中的窗口.我的第一个想法是获取窗口的句柄并使用非托管系统调用来设置窗口的高度/宽度和位置到我的停靠区域.但在我开始之前,我想检查一下你们中是否有人做过类似的事情.我可以访问我想要停靠的应用程序的源代码,但如果可以避免它,我宁愿不做任何更改.我对父应用程序的完整编程控制.有什么建议?提前致谢!

小智 26

我之前使用的解决方案是将应用程序窗口设置为要将其停靠的控件的子窗口.

using System.Diagnostics;
using System.Runtime.InteropServices;

private Process pDocked;
private IntPtr hWndOriginalParent;
private IntPtr hWndDocked;

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

private void dockIt()
{
    if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
        return;
    hWndParent = IntPtr.Zero;

    pDocked = Process.Start(@"notepad");
    while (hWndDocked == IntPtr.Zero)
    {
        pDocked.WaitForInputIdle(1000); //wait for the window to be ready for input;
        pDocked.Refresh();              //update process info
        if (pDocked.HasExited)
        {
            return; //abort if the process finished before we got a handle.
        }
        hWndDocked = pDocked.MainWindowHandle;  //cache the window handle
    }
    //Windows API call to change the parent of the target window.
    //It returns the hWnd of the window's parent prior to this call.
    hWndOriginalParent = SetParent(hWndDocked, Panel1.Handle);

    //Wire up the event to keep the window sized to match the control
    Panel1.SizeChanged += new EventHandler(Panel1_Resize);
    //Perform an initial call to set the size.
    Panel1_Resize(new Object(), new EventArgs());
}

private void undockIt()
{
    //Restores the application to it's original parent.
    SetParent(hWndDocked, hWndOriginalParent);
}

private void Panel1_Resize(object sender, EventArgs e)
{
    //Change the docked windows size to match its parent's size. 
    MoveWindow(hWndDocked, 0, 0, Panel1.Width, Panel1.Height, true);
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!''while(hWndDocked == IntPtr.Zero){}''循环也解决了我的一些问题. (2认同)

Sau*_*oot 5

*在答案中添加一些解决方案.. **

这段代码帮助我在Windows窗体中停靠了一些可执行文件.像NotePad,Excel,word,Acrobat reader等等......

但它不适用于某些应用程序.有时当你开始某个应用程序的进程....等待空闲时间...并尝试获取其mainWindowHandle ....直到主窗口句柄变为空的时间.....

所以我做了一个解决这个问题的技巧

如果你将主窗口句柄作为null ...然后搜索sytem上的所有运行进程并找到你的进程...然后获取进程的主要进程并将set面板作为其父进程.

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "xxxxxxxxxxxx.exe";
            info.Arguments = "yyyyyyyyyy";
            info.UseShellExecute = true;
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Maximized;
            info.RedirectStandardInput = false;
            info.RedirectStandardOutput = false;
            info.RedirectStandardError = false;

            System.Diagnostics.Process p = System.Diagnostics.Process.Start(info); 

            p.WaitForInputIdle();
            Thread.Sleep(3000);

            Process[] p1 ;
        if(p.MainWindowHandle == null)
        {
            List<String> arrString = new List<String>();
            foreach (Process p1 in Process.GetProcesses())
            {
                // Console.WriteLine(p1.MainWindowHandle);
                arrString.Add(Convert.ToString(p1.ProcessName));
            }
            p1 = Process.GetProcessesByName("xxxxxxxxxxxx");
            //p.WaitForInputIdle();
            Thread.Sleep(5000);
           SetParent(p1[0].MainWindowHandle, this.panel2.Handle);
        }
         else
        {
           SetParent(p.MainWindowHandle, this.panel2.Handle);
        }
Run Code Online (Sandbox Code Playgroud)