主要有兩種方法,分別是:
1. SetParent - 這是來自 user32.dll 中所提供的方法。
2. Process - 這次來自 System.Diagnostics 底下提供的方法。
那麼接下來就開始解說這次的練習範例吧!
首先是整體視窗內容說明。
本次內容利用 TabControl 來將功能做區分,
首先看到的是 SetParent 功能的 Tabpage,
這裡還有幾個元件,分別是:
Add - 將外部程式加入到 panel 中
Hide - 將程式隱藏
Panel - 位於右手邊的黑色外框即為 panel
這裡有幾個元件,分別是:
Set Parent + Process Start - 將外部程式從主程式開啟並加入到 panel 中
Panel - 位於右手邊的黑色外框即為 panel
接下來我們就來看但執行的結果吧!
首先是 Set Parent 的結果,
沒有意外的他就是這樣加入到 panel 中,
但這裡要先說明一下,
這裡使用的是 SetParent 沒錯,但來源呢並不能算是外部程式,
這裡居裡貓使用的是 MainForm 的類別,
取得這個變數的 handle 之後利用 SetParent 加入到 panel 中,
這裡大家需要特別注意一下。
接下來是 Process Start 的結果,
是的,Process Start 就是這樣的把一個指定的外部程式給開啟來,
這樣就是百分百的把你想要用的程式好好的開啟,提供使用。
那個在某些特殊的情況下,真的是要開啟一個外部的程式,
然後又不是主程式自帶的類別,又要加入到主程式視窗中,
那該怎麼辦呢?
那就讓我們來看看第三部份 Set Parent + Process Start 的結果,
真實的外部程式,並且要加入到主視窗中顯示。
不過這裡有個有趣的部份在於,當使用 Process Start 啟動外部程式的時候,
會有短暫的時間看到外部程式獨立的視窗出現,
之後才會被加入到 panel 中,這個大家要注意一下。
那個接下來就是展示程式碼的部份,
今天居裡貓就不太別針對程式碼的部份做說明了,
請大家自行研究一下下吧!
-----------------------------------------------------------------------------------------------------------------------
MainForm.cs 的部份
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace NewTestFormApp
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- #region Set Parent
- [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]
- public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
- Form fm = new Form();
- private void btnAddWindow_Click(object sender, EventArgs e)
- {
- fm = new MainForm();
- SetParent(fm.Handle, palNewWindow.Handle);
- fm.Location = new Point(0, 0);
- fm.Size = palNewWindow.Size;
- fm.Show();
- }
- private void btnHideWindow_Click(object sender, EventArgs e)
- {
- fm.Hide();
- }
- #endregion // Set Parent
- #region Process Start
- System.Diagnostics.Process m_Process = new System.Diagnostics.Process();
- private void btnProcessStart_Click(object sender, EventArgs e)
- {
- m_Process = new System.Diagnostics.Process()
- {
- SynchronizingObject = this,
- EnableRaisingEvents = true,
- StartInfo = new System.Diagnostics.ProcessStartInfo()
- {
- FileName = Application.StartupPath + @"\NewTestFormApp.exe",
- CreateNoWindow = true,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
- }
- };
- m_Process.Start();
- }
- #endregion // Process Start
- #region Set Parent + Process Start
- private void btnSetParentProcessStart_Click(object sender, EventArgs e)
- {
- m_Process = new System.Diagnostics.Process()
- {
- SynchronizingObject = this,
- EnableRaisingEvents = true,
- StartInfo = new System.Diagnostics.ProcessStartInfo()
- {
- FileName = Application.StartupPath + @"\NewTestFormApp.exe",
- CreateNoWindow = true,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
- }
- };
- m_Process.Start();
- System.Threading.Thread.Sleep(1000);
- SetParent(m_Process.MainWindowHandle, this.palSetParentProcessStart.Handle);
- }
- #endregion // Set Parent + Process Start
- }
- }
MainForm.Designer.cs 的部份
- namespace NewTestFormApp
- {
- partial class MainForm
- {
- /// <summary>
- /// 設計工具所需的變數。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清除任何使用中的資源。
- /// </summary>
- /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form 設計工具產生的程式碼
- /// <summary>
- /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改
- /// 這個方法的內容。
- /// </summary>
- private void InitializeComponent()
- {
- this.tabAddWindow = new System.Windows.Forms.TabControl();
- this.tabPage1 = new System.Windows.Forms.TabPage();
- this.txtMessageSetParent = new System.Windows.Forms.TextBox();
- this.palNewWindow = new System.Windows.Forms.Panel();
- this.btnHideWindow = new System.Windows.Forms.Button();
- this.btnAddWindow = new System.Windows.Forms.Button();
- this.tabPage2 = new System.Windows.Forms.TabPage();
- this.txtMessageProcess = new System.Windows.Forms.TextBox();
- this.btnProcessStart = new System.Windows.Forms.Button();
- this.tabPage3 = new System.Windows.Forms.TabPage();
- this.btnSetParentProcessStart = new System.Windows.Forms.Button();
- this.palSetParentProcessStart = new System.Windows.Forms.Panel();
- this.tabAddWindow.SuspendLayout();
- this.tabPage1.SuspendLayout();
- this.tabPage2.SuspendLayout();
- this.tabPage3.SuspendLayout();
- this.SuspendLayout();
- //
- // tabAddWindow
- //
- this.tabAddWindow.Controls.Add(this.tabPage1);
- this.tabAddWindow.Controls.Add(this.tabPage2);
- this.tabAddWindow.Controls.Add(this.tabPage3);
- this.tabAddWindow.Location = new System.Drawing.Point(12, 12);
- this.tabAddWindow.Name = "tabAddWindow";
- this.tabAddWindow.SelectedIndex = 0;
- this.tabAddWindow.Size = new System.Drawing.Size(824, 435);
- this.tabAddWindow.TabIndex = 0;
- //
- // tabPage1
- //
- this.tabPage1.Controls.Add(this.txtMessageSetParent);
- this.tabPage1.Controls.Add(this.palNewWindow);
- this.tabPage1.Controls.Add(this.btnHideWindow);
- this.tabPage1.Controls.Add(this.btnAddWindow);
- this.tabPage1.Location = new System.Drawing.Point(4, 22);
- this.tabPage1.Name = "tabPage1";
- this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
- this.tabPage1.Size = new System.Drawing.Size(816, 409);
- this.tabPage1.TabIndex = 0;
- this.tabPage1.Text = "Set Parent";
- this.tabPage1.UseVisualStyleBackColor = true;
- //
- // txtMessageSetParent
- //
- this.txtMessageSetParent.Location = new System.Drawing.Point(25, 111);
- this.txtMessageSetParent.Name = "txtMessageSetParent";
- this.txtMessageSetParent.Size = new System.Drawing.Size(265, 22);
- this.txtMessageSetParent.TabIndex = 3;
- this.txtMessageSetParent.Visible = false;
- //
- // palNewWindow
- //
- this.palNewWindow.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.palNewWindow.Location = new System.Drawing.Point(296, 6);
- this.palNewWindow.Name = "palNewWindow";
- this.palNewWindow.Size = new System.Drawing.Size(514, 363);
- this.palNewWindow.TabIndex = 2;
- //
- // btnHideWindow
- //
- this.btnHideWindow.Location = new System.Drawing.Point(25, 70);
- this.btnHideWindow.Name = "btnHideWindow";
- this.btnHideWindow.Size = new System.Drawing.Size(75, 23);
- this.btnHideWindow.TabIndex = 1;
- this.btnHideWindow.Text = "Hide";
- this.btnHideWindow.UseVisualStyleBackColor = true;
- this.btnHideWindow.Click += new System.EventHandler(this.btnHideWindow_Click);
- //
- // btnAddWindow
- //
- this.btnAddWindow.Location = new System.Drawing.Point(25, 23);
- this.btnAddWindow.Name = "btnAddWindow";
- this.btnAddWindow.Size = new System.Drawing.Size(75, 23);
- this.btnAddWindow.TabIndex = 0;
- this.btnAddWindow.Text = "Add";
- this.btnAddWindow.UseVisualStyleBackColor = true;
- this.btnAddWindow.Click += new System.EventHandler(this.btnAddWindow_Click);
- //
- // tabPage2
- //
- this.tabPage2.Controls.Add(this.txtMessageProcess);
- this.tabPage2.Controls.Add(this.btnProcessStart);
- this.tabPage2.Location = new System.Drawing.Point(4, 22);
- this.tabPage2.Name = "tabPage2";
- this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
- this.tabPage2.Size = new System.Drawing.Size(816, 409);
- this.tabPage2.TabIndex = 1;
- this.tabPage2.Text = "Process Start";
- this.tabPage2.UseVisualStyleBackColor = true;
- //
- // txtMessageProcess
- //
- this.txtMessageProcess.Location = new System.Drawing.Point(27, 64);
- this.txtMessageProcess.Name = "txtMessageProcess";
- this.txtMessageProcess.Size = new System.Drawing.Size(401, 22);
- this.txtMessageProcess.TabIndex = 2;
- this.txtMessageProcess.Visible = false;
- //
- // btnProcessStart
- //
- this.btnProcessStart.Location = new System.Drawing.Point(27, 24);
- this.btnProcessStart.Name = "btnProcessStart";
- this.btnProcessStart.Size = new System.Drawing.Size(75, 23);
- this.btnProcessStart.TabIndex = 1;
- this.btnProcessStart.Text = "Process Start";
- this.btnProcessStart.UseVisualStyleBackColor = true;
- this.btnProcessStart.Click += new System.EventHandler(this.btnProcessStart_Click);
- //
- // tabPage3
- //
- this.tabPage3.Controls.Add(this.palSetParentProcessStart);
- this.tabPage3.Controls.Add(this.btnSetParentProcessStart);
- this.tabPage3.Location = new System.Drawing.Point(4, 22);
- this.tabPage3.Name = "tabPage3";
- this.tabPage3.Size = new System.Drawing.Size(816, 409);
- this.tabPage3.TabIndex = 2;
- this.tabPage3.Text = "Set Parent + Process Start";
- this.tabPage3.UseVisualStyleBackColor = true;
- //
- // btnSetParentProcessStart
- //
- this.btnSetParentProcessStart.Location = new System.Drawing.Point(29, 30);
- this.btnSetParentProcessStart.Name = "btnSetParentProcessStart";
- this.btnSetParentProcessStart.Size = new System.Drawing.Size(140, 23);
- this.btnSetParentProcessStart.TabIndex = 0;
- this.btnSetParentProcessStart.Text = "Set Parent + Process Start";
- this.btnSetParentProcessStart.UseVisualStyleBackColor = true;
- this.btnSetParentProcessStart.Click += new System.EventHandler(this.btnSetParentProcessStart_Click);
- //
- // palSetParentProcessStart
- //
- this.palSetParentProcessStart.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.palSetParentProcessStart.Location = new System.Drawing.Point(299, 8);
- this.palSetParentProcessStart.Name = "palSetParentProcessStart";
- this.palSetParentProcessStart.Size = new System.Drawing.Size(514, 363);
- this.palSetParentProcessStart.TabIndex = 3;
- //
- // MainForm
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(848, 459);
- this.Controls.Add(this.tabAddWindow);
- this.Name = "MainForm";
- this.Text = "Main Form";
- this.tabAddWindow.ResumeLayout(false);
- this.tabPage1.ResumeLayout(false);
- this.tabPage1.PerformLayout();
- this.tabPage2.ResumeLayout(false);
- this.tabPage2.PerformLayout();
- this.tabPage3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.TabControl tabAddWindow;
- private System.Windows.Forms.TabPage tabPage1;
- private System.Windows.Forms.Panel palNewWindow;
- private System.Windows.Forms.Button btnHideWindow;
- private System.Windows.Forms.Button btnAddWindow;
- private System.Windows.Forms.TabPage tabPage2;
- private System.Windows.Forms.Button btnProcessStart;
- private System.Windows.Forms.TextBox txtMessageProcess;
- private System.Windows.Forms.TextBox txtMessageSetParent;
- private System.Windows.Forms.TabPage tabPage3;
- private System.Windows.Forms.Button btnSetParentProcessStart;
- private System.Windows.Forms.Panel palSetParentProcessStart;
- }
- }
沒有留言:
張貼留言