Csharp/C#教程:在WinForm项目选项卡中运行exe分享


在WinForm项目选项卡中运行exe

我对使用winform应用程序执行以下操作感兴趣,我正在编写,以在Visual Studio 2010 IDE中的c#中的.exe中打开。

我现在可以通过使用以下代码在所需选项卡中单击按钮来打开程序:

string str = @"-INSERT FILEPATH HERE-";//took file path out as i have a few exes i'm wanting to add. Process process = new Process(); process.StartInfo.FileName = str; process.Start(); 

现在我怎么能这样做,所以这个可执行文件作为选项卡打开或在选项卡中,在我的winform中? 我愿意接受任何一种情况的建议。

解决了:

 using System.Runtime.InteropServices; using System.Threading; [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); public GUI() { // Initialize form fieds InitializeComponent(); openProgram() } private void openProgram() { process.StartInfo.FileName = "-filepathhere-"; process.Start(); IntPtr ptr = IntPtr.Zero; while ((ptr = process.MainWindowHandle) == IntPtr.Zero) ; SetParent(process.MainWindowHandle, trackerPanel.Handle); MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true); } 

您可以使用SetParent api设置可执行文件窗口的父级。 将面板添加到TabControl并使用下面的代码将可执行窗口的父级分配给面板的父级。

 [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); private void button2_Click(object sender, EventArgs e) { var process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.Start(); SetParent(process.MainWindowHandle, panel1.Handle); } 

要从面板中删除窗口,请使用相同的代码,但将父句柄设置为IntPtr.Zero

上述就是C#学习教程:在WinForm项目选项卡中运行exe分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 SetParent(process.MainWindowHandle, IntPtr.Zero); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1006872.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐