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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

通過(guò) WIN32 API 實(shí)現(xiàn)嵌入程序窗體

2019-11-17 02:51:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
通過(guò) WIN32 API 實(shí)現(xiàn)嵌入程序窗體

寫(xiě)了一個(gè)不使用 COM, 而是通過(guò) WIN32 API 實(shí)現(xiàn)的示例, 它把寫(xiě)字板程序嵌在了自己的一個(gè)面板中.

這么做可能沒(méi)有實(shí)際意義, 因?yàn)閮蓚€(gè)程序之前沒(méi)有進(jìn)行有價(jià)值的交互, 這里僅僅是為了演示這么做到, 以下是詳細(xì)注釋過(guò)的主要源代碼.

我把它封裝到一個(gè)類(lèi)中:

[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函數(shù)和變量聲明
  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常數(shù)
  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. //目標(biāo)應(yīng)用程序的進(jìn)程.
  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. //設(shè)置目標(biāo)應(yīng)用程序的窗體樣式.
  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相應(yīng)事件
  94. privatevoidLoadEvent(stringappFile)
  95. {
  96. //啟動(dòng)目標(biāo)應(yīng)用程序.
  97. m_innerProcess=Process.Start(appFile);
  98. m_innerProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;//隱藏
  99. //等待,直到那個(gè)程序已經(jīng)完全啟動(dòng).
  100. m_innerProcess.WaitForInputIdle();
  101. //目標(biāo)應(yīng)用程序的主窗體.
  102. IntPtrinnerWnd=m_innerProcess.MainWindowHandle;
  103. //設(shè)置目標(biāo)應(yīng)用程序的主窗體的父親(為我們的窗體).
  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事件中更新目標(biāo)應(yīng)用程序的窗體尺寸.
  113. panel1_Resize(panel1,null);
  114. }
  115. #endregion
  116. }
  117. }

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

詳細(xì)代碼 如下:

[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將程序窗口嵌入到任務(wù)欄中
  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");//獲取系統(tǒng)變量windir(windows)
  36. conststringappFile=
  37. "C://ProgramFiles//WindowsNT//accessories//Wordpad.exe";
  38. InsertWindowinsertwin=newInsertWindow(panel1,appFile);
  39. }
  40. }
  41. }

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临夏市| 克山县| 资源县| 五大连池市| 吴堡县| 金平| 中山市| 永康市| 农安县| 长丰县| 抚州市| 瑞金市| 乡城县| 仁布县| 安图县| 石林| 外汇| 拉孜县| 紫金县| 响水县| 五家渠市| 连平县| 新郑市| 资溪县| 新乡县| 广平县| 巴马| 大新县| 阿克陶县| 四子王旗| 日喀则市| 土默特右旗| 兰州市| 吉林省| 安康市| 沂水县| 子长县| 柳河县| 临洮县| 新源县| 南安市|