永不言弃!
不忘初心:感恩的心!上进的心!

 

第一:新建一个类

类文件名称为Win32Native.cs, 类的代码如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace WpfApplication1 
{ 
    public class Win32Native 
    { 
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")] 
        public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr);  
    } 
} 

 

第二:新建两个窗体

        窗体1:Window1.xaml

        窗体2:Window2.xaml

 

第三:添加引用

Window1.xaml.cs 中添加引用 "using System.Windows.Interop;"

 

第四:添加事件

在Window1窗体中放上一个Button1, 其事件如下:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Window2 w2 = new Window2(); 
    w2.Show();   
 
    WindowInteropHelper parentHelper = new WindowInteropHelper(this); 
    WindowInteropHelper childHelper = new WindowInteropHelper(w2);  
 
    Win32Native.SetParent(childHelper.Handle, parentHelper.Handle); 
 
    w2.WindowState = WindowState.Maximized; //窗口最大化
} 

 

第五:运行效果

父窗体:点击按钮

实现的效果:父窗体中打开子窗体

 

 

本文引自:https://blog.csdn.net/lassewang/article/details/7041855

 

posted on 2022-03-09 11:20  永不言弃!  阅读(1698)  评论(0编辑  收藏  举报