寫了一個(gè)不使用 COM, 而是通過 WIN32 API 實(shí)現(xiàn)的示例, 它把寫字板程序嵌在了自己的一個(gè)面板中.
這么做可能沒有實(shí)際意義, 因?yàn)閮蓚€(gè)程序之前沒有進(jìn)行有價(jià)值的交互, 這里僅僅是為了演示這么做到, 以下是詳細(xì)注釋過的主要源代碼.
我把它封裝到一個(gè)類中:
- 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
- {
-
-
-
-
-
- public InsertWindow(Panel pW,string appname)
- {
- this.pan = pW;
- this.LoadEvent(appname);
- pane();
- }
-
- ~InsertWindow()
- {
- if (m_innerProcess!=null)
- {
- m_innerProcess.Dispose();
- }
- }
-
- #region 函數(shù)和變量聲明
-
-
-
-
- [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
- );
-
-
-
-
- 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 相應(yīng)事件
- 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);
-
-
- panel1_Resize(panel1, null);
- }
- #endregion
- }
-
-
- }
然后在 窗口的 load事件中 加入
詳細(xì)代碼 如下:
- 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 將程序窗口嵌入到任務(wù)欄中
- {
- public partial class Form1 : Form
- {
- private System.Windows.Forms.Panel panel1;
- public Form1()
- {
- InitializeComponent();
- this.panel1 = new System.Windows.Forms.Panel();
- this.SuspendLayout();
-
-
-
- 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)
- {
-
- const string appFile =
- "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
- InsertWindow insertwin = new InsertWindow(panel1, appFile);
-
- }
-
-
- }
- }