WPF 使用SetParent嵌套窗口

有点类似与Winform的MDI窗口。

使用函数为SetParent和MoveWindow(经常配合)。

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

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

 

给个小例子,例如嵌套TIM的聊天窗口

其中window1 就是新建的窗口 里面什么都没有写,默认

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

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

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr IntPtrGetDesktopWindow();

        public MainWindow()
        {
            InitializeComponent();
          
        }
        int baseSize =700;
        bool IsOpen = false;
        int Num = 1;
        Window1 window;
        private void MainWindow_Closed(object sender, EventArgs e)
        {
           // var childhwnd = FindWindow("TXGuiFoundation", "唐大人的 iPhone");
            var d = SetParent(childhwnd, IntPtrGetDesktopWindow());
            MoveWindow(childhwnd, 0, 0, baseSize + Num+10, baseSize + Num+10, true);
        }
        IntPtr childhwnd;
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            IntPtr hwnd = new WindowInteropHelper(window).Handle;
             childhwnd = FindWindow("TXGuiFoundation", "唐大人的 iPhone");
                //(IntPtr)Convert.ToInt32("000307BA", 16);
            var d = SetParent(childhwnd, hwnd);
            MoveWindow(childhwnd, 0, 0, 600, 600, true);
        }
       
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!IsOpen)
            {
                window = new Window1();
                window.Loaded += MainWindow_Loaded;
                window.Closed += MainWindow_Closed;
                window.Show();
                IsOpen = true;
                Num++;
                return;
            }
            Num++;
            window.Close();
            IsOpen = false;
        }

        
    }

 

截图

 

SetParent的问题:

1 使用API后,子窗口在父窗口中不显示但是可以显示鼠标的拖拉动作(窗口调整)

2 使用API后,子窗口在父窗口中不再具有焦点,也不可以点击,看起来和图片差不多

3 使用API后,子窗口在原桌面留下空白且可以在空白操控父窗口内的子窗口而在父窗口内则是不可以操作子窗口(针对firefox浏览器[官方发行版])

 

相对来说嵌套的问题不少,可以说有很多问题。 本文中的代码是可以解决问题1(就是修改窗口的大小,嵌套时窗口大小要大于原先,释放时也要大于嵌套时,如果对释放和嵌套时对大小有要求可以多次修改窗口大小)。其他问题则是需要考虑windows消息机制(个人看法)了。

至于如何释放子窗口则是将子窗口的父窗口重新设置回去就好了。

posted @ 2019-11-29 10:28  ARM830  阅读(2534)  评论(0编辑  收藏  举报