首页 > 学院 > 开发设计 > 正文

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

2019-11-17 02:51:40
字体:
来源:转载
供稿:网友
通过 WIN32 API 实现嵌入程序窗体

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

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

我把它封装到一个类中:

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Diagnostics;
  6. usingSystem.Runtime.InteropServices;
  7. usingSystem.Windows.Forms;
  8. namespaceSystem.Windows.Forms
  9. {
  10. classInsertWindow
  11. {
  12. ///<summary>
  13. ///将程序嵌入窗体
  14. ///</summary>
  15. ///<paramname="pW">容器</param>
  16. ///<paramname="appname">程序名</param>
  17. publicInsertWindow(PanelpW,stringappname)
  18. {
  19. this.pan=pW;
  20. this.LoadEvent(appname);
  21. pane();
  22. }
  23. ~InsertWindow()
  24. {
  25. if(m_innerPRocess!=null)
  26. {
  27. m_innerProcess.Dispose();
  28. }
  29. }
  30. #region函数和变量声明
  31. /*
  32. *声明Win32API
  33. */
  34. [DllImport("user32.dll")]
  35. staticexternIntPtrSetParent(IntPtrhWndChild,
  36. IntPtrhWndNewParent
  37. );
  38. [DllImport("user32.dll")]
  39. staticexternInt32GetWindowLong(IntPtrhWnd,
  40. Int32nIndex
  41. );
  42. [DllImport("user32.dll")]
  43. staticexternInt32SetWindowLong(IntPtrhWnd,
  44. Int32nIndex,
  45. Int32dwNewLong
  46. );
  47. [DllImport("user32.dll")]
  48. staticexternInt32SetWindowPos(IntPtrhWnd,
  49. IntPtrhWndInsertAfter,
  50. Int32X,
  51. Int32Y,
  52. Int32cx,
  53. Int32cy,
  54. UInt32uFlags
  55. );
  56. /*
  57. *定义Win32常数
  58. */
  59. constInt32GWL_STYLE=-16;
  60. constInt32WS_BORDER=(Int32)0x00800000L;
  61. constInt32WS_THICKFRAME=(Int32)0x00040000L;
  62. constInt32SWP_NOMOVE=0x0002;
  63. constInt32SWP_NOSIZE=0x0001;
  64. constInt32SWP_NOZORDER=0x0004;
  65. constInt32SWP_FRAMECHANGED=0x0020;
  66. constInt32SW_MAXIMIZE=3;
  67. IntPtrHWND_NOTOPMOST=newIntPtr(-2);
  68. //目标应用程序的进程.
  69. Processm_innerProcess=null;
  70. #endregion
  71. #region容器
  72. privatePanelpan=null;
  73. publicPanelpanel1
  74. {
  75. set{pan=value;}
  76. get{returnpan;}
  77. }
  78. privatevoidpane()
  79. {
  80. panel1.Anchor=AnchorStyles.Left|AnchorStyles.Top|
  81. AnchorStyles.Right|AnchorStyles.Bottom;
  82. panel1.Resize+=newEventHandler(panel1_Resize);
  83. }
  84. privatevoidpanel1_Resize(objectsender,EventArgse)
  85. {
  86. //设置目标应用程序的窗体样式.
  87. IntPtrinnerWnd=m_innerProcess.MainWindowHandle;
  88. SetWindowPos(innerWnd,IntPtr.Zero,0,0,
  89. panel1.ClientSize.Width,panel1.ClientSize.Height,
  90. SWP_NOZORDER);
  91. }
  92. #endregion
  93. #region相应事件
  94. privatevoidLoadEvent(stringappFile)
  95. {
  96. //启动目标应用程序.
  97. m_innerProcess=Process.Start(appFile);
  98. m_innerProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;//隐藏
  99. //等待,直到那个程序已经完全启动.
  100. m_innerProcess.WaitForInputIdle();
  101. //目标应用程序的主窗体.
  102. IntPtrinnerWnd=m_innerProcess.MainWindowHandle;
  103. //设置目标应用程序的主窗体的父亲(为我们的窗体).
  104. SetParent(innerWnd,panel1.Handle);
  105. //除去窗体边框.
  106. Int32wndStyle=GetWindowLong(innerWnd,GWL_STYLE);
  107. wndStyle&=~WS_BORDER;
  108. wndStyle&=~WS_THICKFRAME;
  109. SetWindowLong(innerWnd,GWL_STYLE,wndStyle);
  110. SetWindowPos(innerWnd,IntPtr.Zero,0,0,0,0,
  111. SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
  112. //在Resize事件中更新目标应用程序的窗体尺寸.
  113. panel1_Resize(panel1,null);
  114. }
  115. #endregion
  116. }
  117. }

然后在 窗口的 load事件中 加入

详细代码 如下:

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. usingSystem.Runtime;
  10. usingSystem.Runtime.InteropServices;
  11. usingSystem.Diagnostics;
  12. namespace将程序窗口嵌入到任务栏中
  13. {
  14. publicpartialclassForm1:Form
  15. {
  16. privateSystem.Windows.Forms.Panelpanel1;
  17. publicForm1()
  18. {
  19. InitializeComponent();
  20. this.panel1=newSystem.Windows.Forms.Panel();
  21. this.SuspendLayout();
  22. //
  23. //panel1
  24. //
  25. this.panel1.Dock=System.Windows.Forms.DockStyle.Fill;
  26. this.panel1.Location=newSystem.Drawing.Point(0,0);
  27. this.panel1.Name="panel1";
  28. this.panel1.Size=newSystem.Drawing.Size(292,273);
  29. this.panel1.TabIndex=0;
  30. this.Controls.Add(this.panel1);
  31. Load+=newEventHandler(Form1_Load);
  32. }
  33. privatevoidForm1_Load(objectsender,EventArgse)
  34. {
  35. //stringsPath=Environment.GetEnvironmentVariable("windir");//获取系统变量windir(windows)
  36. conststringappFile=
  37. "C://ProgramFiles//WindowsNT//accessories//Wordpad.exe";
  38. InsertWindowinsertwin=newInsertWindow(panel1,appFile);
  39. }
  40. }
  41. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表