c# - 将窗口停靠在另一个窗口中

标签 c# winforms .net-2.0 dllimport docking

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

最佳答案

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

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

关于c# - 将窗口停靠在另一个窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836176/

相关文章:

c# ms 图表 : how do i count how many data points i have?

c# - 从 DataGridView 中删除行时出现意外行为

c# - 如何让 PropertyGrid 显示 SaveFileDialog?

c# - 在没有 Excel 和 OLEDB 的情况下读取 .xlsx

.net - 面向 .NET 2.0 的 VS2008 并没有阻止我使用 C# 3 功能

.net - 如何使用反射调用 .NET 中的重载方法

c# - Cassandra 与 C#

c# - 匹配行尾或其他字符

c# - Web API - 在 DbContext 类中访问 HttpContext

c# - Windows 窗体计算增量鼠标移动负值