日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在winform上内嵌入其它的程序

發布時間:2023/11/29 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在winform上内嵌入其它的程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這段代碼很有意義,用于把一個程序的界面嵌入到我們自己程序的某個指定窗體上.

比如在某個項目里,我需要把基恩士的激光掃描輪廓顯示給客戶看,但是激光的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的界面包含進來的.

這樣你就可以清楚的看到嵌入的效果是怎么樣的.

?

效果如上圖所示. 但是你的程序在退出前,要自己"殺掉"計算器的進程, 否則你的程序退出它還在.

?

本文源代碼

??原創文章,出處 :?http://www.cnblogs.com/hackpig/

?

轉載于:https://www.cnblogs.com/hackpig/p/5783604.html

總結

以上是生活随笔為你收集整理的在winform上内嵌入其它的程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。