C#通过WIN32 API实现嵌入程序窗体
作者:shichen2014 发布时间:2021-08-13 04:53:10
标签:C#,WIN32,窗体
本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,分享给大家供大家参考。具体如下:
这是一个不使用COM,而是通过WIN32 API实现的示例, 它把写字板程序嵌在了自己的一个面板中。
这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做到, 以下是详细注释过的主要源代码。
我们把它封装到一个类中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace System.Windows.Forms
{
class InsertWindow
{
/// <summary>
/// 将程序嵌入窗体
/// </summary>
/// <param name="pW">容器</param>
/// <param name="appname">程序名</param>
public InsertWindow(Panel pW,string appname)
{
this.pan = pW;
this.LoadEvent(appname);
pane();
}
~InsertWindow()
{
if (m_innerProcess!=null)
{
m_innerProcess.Dispose();
}
}
#region 函数和变量声明
/*
* 声明 Win32 API
*/
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild,
IntPtr hWndNewParent
);
[DllImport("user32.dll")]
static extern Int32 GetWindowLong(IntPtr hWnd,
Int32 nIndex
);
[DllImport("user32.dll")]
static extern Int32 SetWindowLong(IntPtr hWnd,
Int32 nIndex,
Int32 dwNewLong
);
[DllImport("user32.dll")]
static extern Int32 SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
Int32 X,
Int32 Y,
Int32 cx,
Int32 cy,
UInt32 uFlags
);
/*
* 定义 Win32 常数
*/
const Int32 GWL_STYLE = -16;
const Int32 WS_BORDER = (Int32)0x00800000L;
const Int32 WS_THICKFRAME = (Int32)0x00040000L;
const Int32 SWP_NOMOVE = 0x0002;
const Int32 SWP_NOSIZE = 0x0001;
const Int32 SWP_NOZORDER = 0x0004;
const Int32 SWP_FRAMECHANGED = 0x0020;
const Int32 SW_MAXIMIZE = 3;
IntPtr HWND_NOTOPMOST = new IntPtr(-2);
// 目标应用程序的进程.
Process m_innerProcess = null;
#endregion
#region 容器
private Panel pan = null;
public Panel panel1
{
set { pan = value; }
get { return pan; }
}
private void pane()
{
panel1.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
panel1.Resize += new EventHandler(panel1_Resize);
}
private void panel1_Resize(object sender, EventArgs e)
{
// 设置目标应用程序的窗体样式.
IntPtr innerWnd = m_innerProcess.MainWindowHandle;
SetWindowPos(innerWnd, IntPtr.Zero, 0, 0,
panel1.ClientSize.Width, panel1.ClientSize.Height,
SWP_NOZORDER);
}
#endregion
#region 相应事件
private void LoadEvent(string appFile)
{
// 启动目标应用程序.
m_innerProcess = Process.Start(appFile);
m_innerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏
// 等待, 直到那个程序已经完全启动.
m_innerProcess.WaitForInputIdle();
// 目标应用程序的主窗体.
IntPtr innerWnd = m_innerProcess.MainWindowHandle;
// 设置目标应用程序的主窗体的父亲(为我们的窗体).
SetParent(innerWnd, panel1.Handle);
// 除去窗体边框.
Int32 wndStyle = GetWindowLong(innerWnd, GWL_STYLE);
wndStyle &= ~WS_BORDER;
wndStyle &= ~WS_THICKFRAME;
SetWindowLong(innerWnd, GWL_STYLE, wndStyle);
SetWindowPos(innerWnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 在Resize事件中更新目标应用程序的窗体尺寸.
panel1_Resize(panel1, null);
}
#endregion
}
}
然后在窗口的load事件中加入详细代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace 将程序窗口嵌入到任务栏中
{
public partial class Form1 : Form
{
private System.Windows.Forms.Panel panel1;
public Form1()
{
InitializeComponent();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(292, 273);
this.panel1.TabIndex = 0;
this.Controls.Add(this.panel1);
Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
//string sPath = Environment.GetEnvironmentVariable("windir");//获取系统变量 windir(windows)
const string appFile =
"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
InsertWindow insertwin = new InsertWindow(panel1, appFile);
}
}
}
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindServ
- java8 Stream list to Map key 重复 value合并到Collectio关于把list转换成key value的m
- ofType和javaType的区别JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型
- 一、前言知识补充:Arrays.copyOf函数:public static int[] copyOf(int[] original, in
- 引言在高并发的场景下,异步是一个极其重要的优化方向。前段时间,生产环境发生一次事故,笔者认为事故的场景非常具备典型性 。写这篇文章,笔者想和
- 一. 简介 SQLite数据库是一个轻量级的DBMS(数据库管理系统)。SQLite使用单个文件存储数据,Android标准库包含SQLit
- 概念JavaBean在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有
- Spring Rest接口路径参数可选我有一个 Spring Rest 服务,其中有一个路径参数是可选的(实际情况是我原来将参数放到路径中,
- 1、背景我有一堆学生数据,其中湖北省的学生需要排在所有数据的最前面。其余省正序排序,对于同一个省的数据,按照年龄倒序排序。2、分析对于上方的
- 没人会喜欢空指针异常!有什么方法可以避免它们吗?或许吧。。本文将讨论到以下几种技术1.Optional类型(Java 8中新引入的)2.Ob
- 目录多开理论基础多开实现原理解析代码实现:多开包名代码实现:多用户总结多开理论基础app多开常用于做一些不合法的事情,如高羊毛,黑灰产,甚至
- 定义: SharedPreferences
- Java 字节码以二进制的形式存储在 .class 文件中,每一个 .class 文件包含一个 Java 类或接口。Javaassist 就
- 本文实例讲述了C#调用SQLite的方法。分享给大家供大家参考。具体分析如下:一、SQLite简介:当我们用到海量数据时一般会用Oracle
- 一、MyBatis背景介绍MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码
- 本文实例为大家分享了javafx实现时钟效果的具体代码,供大家参考,具体内容如下核心为三个函数:第一个为 public void dials
- 在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来
- idea中ssm框架的编码问题介绍在idea中编码问题分为几个部分:1 tomcat服务器编码2 页面编码3 控制台编码4 操作系统编码在实
- 传值(by value)与传址(by reference)分别为普通传递参数方式与ref声明方式,传址方式在使用前需要ref关键词修饰;ou
- 1.查找数据库中表的列名<pre name="code" class="html">St