This article describes the method of C# to embed program forms through WIN32 API, and is shared with you for your reference. The details are as follows:
This is an example that does not use COM but implements it through the WIN32 API, which embeds the WordPad program in one of its own panels.
This may not make any sense, because the two programs did not have valuable interactions before, and this is just for demonstration purposes. Here are the main source codes that have been commented in detail.
We encapsulate it into a class:
using System; using ; using ; using ; using ; using ; using ; namespace { class InsertWindow { /// <summary> /// Embed the program into the form /// </summary> /// <param name="pW">Container</param> /// <param name="appname">Program name</param> public InsertWindow(Panel pW,string appname) { = pW; (appname); pane(); } ~InsertWindow() { if (m_innerProcess!=null) { m_innerProcess.Dispose(); } } #region function and variable declaration /* * Statement Win32 API */ [DllImport("")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent ); [DllImport("")] static extern Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex ); [DllImport("")] static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong ); [DllImport("")] static extern Int32 SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, Int32 X, Int32 Y, Int32 cx, Int32 cy, UInt32 uFlags ); /* * Define Win32 constant */ 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); // The process of the target application. Process m_innerProcess = null; #endregion #region container private Panel pan = null; public Panel panel1 { set { pan = value; } get { return pan; } } private void pane() { = | | | ; += new EventHandler(panel1_Resize); } private void panel1_Resize(object sender, EventArgs e) { // Set the form style of the target application. IntPtr innerWnd = m_innerProcess.MainWindowHandle; SetWindowPos(innerWnd, , 0, 0, , , SWP_NOZORDER); } #endregion #region Corresponding events private void LoadEvent(string appFile) { // Launch the target application. m_innerProcess = (appFile); m_innerProcess. = ; //hide // Wait until that program has fully started. m_innerProcess.WaitForInputIdle(); // The main form of the target application. IntPtr innerWnd = m_innerProcess.MainWindowHandle; // Set the main form of the target application (for our form). SetParent(innerWnd, ); // Remove the form border. Int32 wndStyle = GetWindowLong(innerWnd, GWL_STYLE); wndStyle &= ~WS_BORDER; wndStyle &= ~WS_THICKFRAME; SetWindowLong(innerWnd, GWL_STYLE, wndStyle); SetWindowPos(innerWnd, , 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); // Update the form size of the target application in the Resize event. panel1_Resize(panel1, null); } #endregion } }
Then add the detailed code to the load event of the window as follows:
using System; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; namespace Embed the program window into the taskbar { public partial class Form1 : Form { private panel1; public Form1() { InitializeComponent(); this.panel1 = new (); (); // // panel1 // this. = ; this. = new (0, 0); this. = "panel1"; this. = new (292, 273); this. = 0; (this.panel1); Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { //string sPath = ("windir");//get system variable windir(windows) const string appFile = "C:\\Program Files\\Windows NT\\Accessories\\"; InsertWindow insertwin = new InsertWindow(panel1, appFile); } } }
I hope this article will be helpful to everyone's C# programming.