C#的窗体控件调用.exe窗体

本文详细介绍了如何在C#窗体控件中调用并嵌入.exe应用程序,步骤包括添加命名空间、用户控件构造函数加载启动路径、Main函数传入参数、使用DllImport动态库设置父窗口以及在Form窗体中处理传入参数以调整子窗口位置。
摘要由CSDN通过智能技术生成

1 在窗体控件中添加命名空间

using System.Diagnostics;
using System.Runtime.InteropServices;

 

2. 在用户控件的构造函数中加载启动路径

Process.Start(@"d:\WindowsFormsApp.exe", this.Handle.ToString());

 

3. 在创建Form窗体时会出现Program.cs类文件,在其Main函数中传入参数string[] args:

        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args));
        }

在Application.Run(new Form1()) 中加入闯入的参数args;

 

4. 在创建的Form窗体中加入动态库

        [DllImport("user32.dll")]
        public extern static int SetParent(int child, int parent);

 

5.  传入参数与调用父窗口的函数,如下:

       public Form1(string[] args)
        {

            if (args.Length > 0)
            {
                this.Location = new Point(0, 0);
                SetParent((int)this.Handle, int.Parse(args[0]));
                //MessageBox.Show(args[0]);               
            }    
            InitializeComponent();
        }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值