本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,分享给大家供大家参考。具体如下:

这是一个不使用COM,而是通过WIN32 API实现的示例, 它把写字板程序嵌在了自己的一个面板中。

这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做到, 以下是详细注释过的主要源代码。

我们把它封装到一个类中:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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事件中加入详细代码如下:

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
32
33
34
35
36
37
38
39
40
41
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#程序设计有所帮助。

http://www.jb51.net/article/55821.htm

C#通过WIN32 API实现嵌入程序窗体的更多相关文章

  1. 通过 WIN32 API 实现嵌入程序窗体

    写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做 ...

  2. 重温 Win32 API ----- 截屏指定窗体并打印

    朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...

  3. C# 窗体常用API函数 应用程序窗体查找

    常用的处理窗体的API函数如下(注意:API函数必须放在窗体中...): 使用C#语言,要引用DllImport,必须要添加using System.Runtime.InteropServices命名 ...

  4. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  5. VS中空项目、win32项目、控制台程序的区别(转)

    空项目,大多数想单纯创建c++工程的新同学,打开vs后很可能不知道选择创建什么工程,这时候请相信我,空项目是你最好的选择.因为空工程不包含任何的源代码文件,接下来你只需要在相应的源代码文件夹和头文件文 ...

  6. C# 使用Win32 API将1个EXE程序嵌入另1个程序中

    已经干到天快亮了,就不废话直接贴点儿代码吧 ; ; /// <summary> /// 查找窗口 ///第一个参数是窗口的标题,第二个参数可直接用 null ///通过窗口的标题查找对应的 ...

  7. WPF Win32 API 嵌入Form 窗体

    WIn32 API: public class Win32Native { [DllImport("user32.dll", SetLastError = true, CharSe ...

  8. exe程序嵌入Winform窗体

    1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...

  9. C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】

    这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题) ...

随机推荐

  1. iOS 日志管理异常捕获组件LFLogManager

    一.功能 1.分级打印保存 解决一大堆重要.不重要打印信息在控制台混为一团的尴尬局面.可设置仅打印某级别及以上的信息.分为5类打印: DDLogError(@"打印并保存特别重要信息&quo ...

  2. 简单理解SNAT回流中的概念:路由器怎么知道外网返回的数据是局域网中哪台主机的

    内网到外网用的是NAT技术(地址封装)外网到内网用的是端口映射(PNAT)计算机的端口又65535(0-65534),你说的那些有名气的端口大多都是0-1023之间的你说的这个问题很简单,但首先你要懂 ...

  3. Eclipse环境安装rust

    参考 https://rustdt.github.io/ https://github.com/RustDT/RustDT/blob/latest/documentation/UserGuide.md ...

  4. Android Parcel对象详解

    关于Parcel的使用 在分析Parcel之前,首先按照分析流程,介绍下关于Parcel的相关常规使用. 首先是关于Parcel的获取: Parcel parcle = Parcel.Obtain() ...

  5. Fragment与Activity传递数据

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  6. ios中Core Location跟Map Kit的基本使用

    地图类开发应用中,离不开地理位置跟MKMapView的使用,下面就记录下自己在使用这两个东西中学到的. 不过并不是所有苹果的设备都支持地理位置,我们在使用前应该做个判断,代码如下: BOOL loca ...

  7. RocketMQ概念整理

    DefaultMessageStore 消息的存储和提取. 相对重要的两个方法: 消息存储 PutMessageResult putMessage(MessageExtBrokerInner msg) ...

  8. 记录一个在制作Swing程序的小怪事

    在制作一个Swing程序时,出现了奇怪的事情,程序在Eclipse里执行正常,但打包后再执行那些动态执行的控件就消失不见了.截图如下: 在Eclipse里正常执行的画面: 打包执行后出现的画面: 可以 ...

  9. Notepad++的使用

    \t 制表符.  \n 新行.  . 匹配任意字符.  | 匹配表达式左边和右边的字符. 例如, "ab|bc" 匹配 "ab" 或者 "bc&quo ...

  10. Ubuntu 下的webservices

    搞 了一下午: 开发server程序.需使用gSOAP生成server端代码框架. 我们有两种做法: 编写WSDL,使用wsdl2h生成头文件,再soapcpp2生成框架代码. 编写头文件.使用soa ...