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

王朝学院·作者佚名  2016-05-17
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

我把它封装到一个类中:

[csharp]view plaincopyusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;namespaceSystem.Windows.Forms{classInsertWindow{///<summary>///将程序嵌入窗体///</summary>///<paramname="pW">容器</param>///<paramname="appname">程序名</param>publicInsertWindow(PanelpW,stringappname){this.pan=pW;this.LoadEvent(appname);pane();}~InsertWindow(){if(m_innerPRocess!=null){m_innerProcess.Dispose();}}#region函数和变量声明/**声明Win32API*/[DllImport("user32.dll")]staticexternIntPtrSetParent(IntPtrhWndChild,IntPtrhWndNewParent);[DllImport("user32.dll")]staticexternInt32GetWindowLong(IntPtrhWnd,Int32nIndex);[DllImport("user32.dll")]staticexternInt32SetWindowLong(IntPtrhWnd,Int32nIndex,Int32dwNewLong);[DllImport("user32.dll")]staticexternInt32SetWindowPos(IntPtrhWnd,IntPtrhWndInsertAfter,Int32X,Int32Y,Int32cx,Int32cy,UInt32uFlags);/**定义Win32常数*/constInt32GWL_STYLE=-16;constInt32WS_BORDER=(Int32)0x00800000L;constInt32WS_THICKFRAME=(Int32)0x00040000L;constInt32SWP_NOMOVE=0x0002;constInt32SWP_NOSIZE=0x0001;constInt32SWP_NOZORDER=0x0004;constInt32SWP_FRAMECHANGED=0x0020;constInt32SW_MAXIMIZE=3;IntPtrHWND_NOTOPMOST=newIntPtr(-2);//目标应用程序的进程.Processm_innerProcess=null;#endregion#region容器privatePanelpan=null;publicPanelpanel1{set{pan=value;}get{returnpan;}}privatevoidpane(){panel1.Anchor=AnchorStyles.Left|AnchorStyles.Top|AnchorStyles.Right|AnchorStyles.Bottom;panel1.Resize+=newEventHandler(panel1_Resize);}privatevoidpanel1_Resize(objectsender,EventArgse){//设置目标应用程序的窗体样式.IntPtrinnerWnd=m_innerProcess.MainWindowHandle;SetWindowPos(innerWnd,IntPtr.Zero,0,0,panel1.ClientSize.Width,panel1.ClientSize.Height,SWP_NOZORDER);}#endregion#region相应事件privatevoidLoadEvent(stringappFile){//启动目标应用程序.m_innerProcess=Process.Start(appFile);m_innerProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;//隐藏//等待,直到那个程序已经完全启动.m_innerProcess.WaitForInputIdle();//目标应用程序的主窗体.IntPtrinnerWnd=m_innerProcess.MainWindowHandle;//设置目标应用程序的主窗体的父亲(为我们的窗体).SetParent(innerWnd,panel1.Handle);//除去窗体边框.Int32wndStyle=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事件中 加入

详细代码 如下:

[csharp]view plaincopyusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime;usingSystem.Runtime.InteropServices;usingSystem.Diagnostics;namespace将程序窗口嵌入到任务栏中{publicpartialclassForm1:Form{privateSystem.Windows.Forms.Panelpanel1;publicForm1(){InitializeComponent();this.panel1=newSystem.Windows.Forms.Panel();this.SuspendLayout();////panel1//this.panel1.Dock=System.Windows.Forms.DockStyle.Fill;this.panel1.Location=newSystem.Drawing.Point(0,0);this.panel1.Name="panel1";this.panel1.Size=newSystem.Drawing.Size(292,273);this.panel1.TabIndex=0;this.Controls.Add(this.panel1);Load+=newEventHandler(Form1_Load);}privatevoidForm1_Load(objectsender,EventArgse){//stringsPath=Environment.GetEnvironmentVariable("windir");//获取系统变量windir(windows)conststringappFile="C:\\ProgramFiles\\WindowsNT\\accessories\\Wordpad.exe";InsertWindowinsertwin=newInsertWindow(panel1,appFile);}}}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航