永不言弃!
不忘初心:感恩的心!上进的心!
随笔 - 134,  文章 - 26,  评论 - 15,  阅读 - 140万

 

第一:新建一个类

类文件名称为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   永不言弃!  阅读(2042)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2017-03-09 HTTP 错误 404.0 - Not Found
2017-03-09 SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)
2017-03-09 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
2017-03-09 win10 HTTP 错误 500.21 - Internal Server Error

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示