国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#通過WIN32 API實現嵌入程序窗體

2020-01-24 02:23:09
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 固始县| 济南市| 雅江县| 会理县| 永和县| 新闻| 读书| 保靖县| 青川县| 博客| 康乐县| 和政县| 陕西省| 温泉县| 永仁县| 苗栗市| 旌德县| 锦州市| 伽师县| 宝鸡市| 嵊州市| 永善县| 阳信县| 中西区| 阿克苏市| 白水县| 尖扎县| 彰化市| 临夏市| 盐津县| 衡阳市| 芜湖市| 汶上县| 泰来县| 闵行区| 错那县| 迁西县| 板桥市| 克什克腾旗| 屏东市| 本溪|