栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将窗口停靠在另一个窗口中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将窗口停靠在另一个窗口中

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号