C#通過(guò)WIN32 API實(shí)現(xiàn)嵌入程序窗體
本文實(shí)例講述了C#通過(guò)WIN32 API實(shí)現(xiàn)嵌入程序窗體的方法,分享給大家供大家參考。具體如下:
這是一個(gè)不使用COM,而是通過(guò)WIN32 API實(shí)現(xiàn)的示例, 它把寫字板程序嵌在了自己的一個(gè)面板中。
這么做可能沒(méi)有實(shí)際意義, 因?yàn)閮蓚€(gè)程序之前沒(méi)有進(jìn)行有價(jià)值的交互, 這里僅僅是為了演示這么做到, 以下是詳細(xì)注釋過(guò)的主要源代碼。
我們把它封裝到一個(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 { /// <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 函數(shù)和變量聲明 /* * 聲明 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 常數(shù) */ 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); // 目標(biāo)應(yīng)用程序的進(jìn)程. 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) { // 設(shè)置目標(biāo)應(yīng)用程序的窗體樣式. 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) { // 啟動(dòng)目標(biāo)應(yīng)用程序. m_innerProcess = Process.Start(appFile); m_innerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //隱藏 // 等待, 直到那個(gè)程序已經(jīng)完全啟動(dòng). m_innerProcess.WaitForInputIdle(); // 目標(biāo)應(yīng)用程序的主窗體. IntPtr innerWnd = m_innerProcess.MainWindowHandle; // 設(shè)置目標(biāo)應(yīng)用程序的主窗體的父親(為我們的窗體). 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事件中更新目標(biāo)應(yīng)用程序的窗體尺寸. 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(); // // 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");//獲取系統(tǒng)變量 windir(windows) const string appFile = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"; InsertWindow insertwin = new InsertWindow(panel1, appFile); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# web api返回類型設(shè)置為json的兩種方法
- C#中通過(guò)API實(shí)現(xiàn)的打印類 實(shí)例代碼
- C#實(shí)現(xiàn)快遞api接口調(diào)用方法
- 使用C#調(diào)用系統(tǒng)API實(shí)現(xiàn)內(nèi)存注入的代碼
- ASP.NET(C#) Web Api通過(guò)文件流下載文件的實(shí)例
- c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷)
- C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)
- C#獲取USB事件API實(shí)例分析
- c#之利用API函數(shù)實(shí)現(xiàn)動(dòng)畫窗體的方法詳解
- C# API中模型與它們的接口設(shè)計(jì)詳解
相關(guān)文章
C# null 合并運(yùn)算符??(雙問(wèn)號(hào))使用示例
這篇文章主要介紹了C# null 合并運(yùn)算符??(雙問(wèn)號(hào))使用示例,?? 運(yùn)算符稱作 null 合并運(yùn)算符。如果此運(yùn)算符的左操作數(shù)不為 null,則此運(yùn)算符將返回左操作數(shù);否則返回右操作數(shù),需要的朋友可以參考下2014-06-06WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件
這篇文章主要介紹了WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)微信公眾號(hào)會(huì)員卡管理的示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)微信公眾號(hào)會(huì)員卡管理的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06C#中l(wèi)abel內(nèi)容顯示不全、不完整的解決方法
這篇文章主要介紹了C#中l(wèi)abel內(nèi)容顯示不全、不完整的解決方法,只需要把兩個(gè)屬性設(shè)置一下即可解決這個(gè)問(wèn)題,需要的朋友可以參考下2015-06-06