這段代碼很有意義,用於把一個程式的界面嵌入到我們自己程式的某個指定窗體上. 比如在某個項目里,我需要把基恩士的激光掃描輪廓顯示給客戶看,但是激光的DLL中並沒有這種功能提供. 於是我想先啟動激光的官方程式用以顯示輪廓, 然後再把這種顯示界面嵌入到我自己程式的界面上指定的位置上. 在筆者構想的PLC仿 ...
這段代碼很有意義,用於把一個程式的界面嵌入到我們自己程式的某個指定窗體上.
比如在某個項目里,我需要把基恩士的激光掃描輪廓顯示給客戶看,但是激光的DLL中並沒有這種功能提供. 於是我想先啟動激光的官方程式用以顯示輪廓, 然後再把這種顯示界面嵌入到我自己程式的界面上指定的位置上.
在筆者構想的PLC模擬器由梯形圖編輯器, 3D模擬組態環境兩部分組成, 這兩部分就可以考慮開發成獨立的軟體,然後嵌入到我需要的另外的一款PLC模擬教學軟體中去.
源代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 using System.Diagnostics; 11 using System.Threading; 12 13 namespace WindowsFormsApplication1 14 { 15 public partial class Form1 : Form 16 { 17 Process p; 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 #region API 25 [DllImport("user32.dll")] 26 private static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent); 27 28 [DllImport("user32.dll")] 29 private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 30 31 [DllImport("user32.dll", SetLastError = true)] 32 private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); 33 34 [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 35 private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, 36 int X, int Y, int cx, int cy, uint uFlags); 37 38 [DllImport("user32.dll")] 39 private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); 40 41 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 42 private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong); 43 44 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 45 private static extern uint GetWindowLong(IntPtr hwnd, int nIndex); 46 47 [DllImport("user32.dll", CharSet = CharSet.Auto)] 48 private static extern bool ShowWindow(IntPtr hWnd, short State); 49 50 private const int HWND_TOP = 0x0; 51 private const int WM_COMMAND = 0x0112; 52 private const int WM_QT_PAINT = 0xC2DC; 53 private const int WM_PAINT = 0x000F; 54 private const int WM_SIZE = 0x0005; 55 private const int SWP_FRAMECHANGED = 0x0020; 56 public enum ShowWindowStyles : short 57 { 58 SW_HIDE = 0, 59 SW_SHOWNORMAL = 1, 60 SW_NORMAL = 1, 61 SW_SHOWMINIMIZED = 2, 62 SW_SHOWMAXIMIZED = 3, 63 SW_MAXIMIZE = 3, 64 SW_SHOWNOACTIVATE = 4, 65 SW_SHOW = 5, 66 SW_MINIMIZE = 6, 67 SW_SHOWMINNOACTIVE = 7, 68 SW_SHOWNA = 8, 69 SW_RESTORE = 9, 70 SW_SHOWDEFAULT = 10, 71 SW_FORCEMINIMIZE = 11, 72 SW_MAX = 11 73 } 74 #endregion 75 76 private void Form1_Load(object sender, EventArgs e) 77 { 78 p = new Process(); 79 //需要啟動的程式 80 p.StartInfo.FileName = @"calc.exe"; 81 //為了美觀,啟動的時候最小化程式 82 p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; 83 //啟動 84 p.Start(); 85 86 //這裡必須等待,否則啟動程式的句柄還沒有創建,不能控製程序 87 Thread.Sleep(10000); 88 //最大化啟動的程式 89 ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_MAXIMIZE); 90 //設置被綁架程式的父視窗 91 SetParent(p.MainWindowHandle, this.Handle); 92 //改變尺寸 93 ResizeControl(); 94 } 95 96 //控制嵌入程式的位置和尺寸 97 private void ResizeControl() 98 { 99 SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0); 100 PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0); 101 102 SetWindowPos( 103 p.MainWindowHandle, 104 HWND_TOP, 105 0 - 10,//設置偏移量,把原來視窗的菜單遮住 106 0 - 32, 107 (int)this.Width + 32, 108 (int)this.Height + 32, 109 SWP_FRAMECHANGED); 110 111 SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0); 112 } 113 114 private void Form1_SizeChanged(object sender, EventArgs e) 115 { 116 ResizeControl(); 117 } 118 119 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 120 { 121 p.Kill(); 122 p.Dispose(); 123 } 124 } 125 }
核心功能利用了windows API中的SetParent()
程式運行後, 先啟動calc.exe(windows計算器), 然後等待10秒後再顯示本程式自己的窗體, 這時你會發現這個窗體已經把calc.exe的界麵包含進來的.
這樣你就可以清楚的看到嵌入的效果是怎麼樣的.
效果如上圖所示. 但是你的程式在退出前,要自己"殺掉"計算器的進程, 否則你的程式退出它還在.