C# 以嵌入到窗体的方式打开外部exe
using System;
using System.Collections.Generic;
using System.Text; using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms; namespace War3Screen
{
/// <summary>
/// 以嵌入到窗体的方式打开外部exe--kongfl888 2013
/// </summary>
class OpenExeClass
{
static Process process = null;
static IntPtr appWin;
private static string exeName = ""; [DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [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", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
// CharSet = CharSet.Unicode, ExactSpelling = true,
// CallingConvention = CallingConvention.StdCall)]
//private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); //[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
//private static extern long GetWindowLong(IntPtr hwnd, int nIndex); //[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
//private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); //[DllImport("user32.dll", SetLastError = true)]
//private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags); //[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
//private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam); //[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
//private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); //[DllImport("user32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
//static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); //[StructLayout(LayoutKind.Sequential)]
//public struct RECT
//{
// public int Left; //最左坐标
// public int Top; //最上坐标
// public int Right; //最右坐标
// public int Bottom; //最下坐标
//} //private const int SWP_NOOWNERZORDER = 0x200;
//private const int SWP_NOREDRAW = 0x8;
//private const int SWP_NOZORDER = 0x4;
//private const int SWP_SHOWWINDOW = 0x0040;
//private const int WS_EX_MDICHILD = 0x40;
//private const int SWP_FRAMECHANGED = 0x20;
//private const int SWP_NOACTIVATE = 0x10;
//private const int SWP_ASYNCWINDOWPOS = 0x4000;
//private const int SWP_NOMOVE = 0x2;
//private const int SWP_NOSIZE = 0x1;
//private const int GWL_STYLE = (-16);
//private const int WS_VISIBLE = 0x10000000;
//private const int WM_CLOSE = 0x10;
//private const int WS_CHILD = 0x40000000;
//private const int SW_HIDE = 0; public string ExeName
{
get
{
return exeName;
}
set
{
exeName = value;
}
} public static void OpenExe(GroupBox fm, string filefullname)
{
exeName = filefullname;
if (exeName == null || exeName == string.Empty) return; try
{
// Start the process
process = System.Diagnostics.Process.Start(exeName); // Wait for process to be created and enter idle condition
process.WaitForInputIdle(); // Get the main handle
//appWin = process.MainWindowHandle; appWin = FindWindow(null, "War3 fixer");
}
catch (Exception ex)
{
MessageBox.Show(fm, ex.Message, "Error");
} // Put it into this form
SetParent(appWin, fm.Handle); // Remove border and whatnot
// SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE); // Move the window to overlay it on this window
MoveWindow(appWin, , , fm.Width+, fm.Height, true);
//RECT rc = new RECT();
//GetWindowRect(appWin, ref rc);
//int width = rc.Right - rc.Left; //窗口的宽度
//int height = rc.Bottom - rc.Top; //窗口的高度
//int x = rc.Left;
//int y = rc.Top;
//MoveWindow(appWin, 0, 0, width, height, true); fm.Text = "魔兽"; } public static bool CloseExe()
{
bool sc = false;
try
{
System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses ();
foreach (System.Diagnostics.Process process2 in processList)
{
if (process2.Id == process.Id)
{
process.Kill(); //结束进程
sc=process.WaitForExit(); }
}
}
catch { sc = true; }
return sc;
} public static Process IsProcessRun()
{
Process processRun = new Process();
try
{
System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses ();
foreach (System.Diagnostics.Process process2 in processList)
{
if (process2.Id == process.Id)
{
processRun = process; }
}
}
catch { } return processRun;
} }
} 主窗体退出时写FormClosed事件函数,调用CloseExe()函数 在控件改变大小的时候,重新MoveWindow()即可,如:
private void splitContainer1_Panel2_Resize(object sender, EventArgs e)
{
if (this.appWin != IntPtr.Zero)
{
MoveWindow(appWin, , , this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height, true);
}
base.OnResize(e);
}
C# 以嵌入到窗体的方式打开外部exe的更多相关文章
- MFC 打开外部EXE文件的三种方法
目前知道三种方式:WinExec,ShellExecute ,CreateProcess,别人已经总结的很好了<vc中调用其他应用程序的方法(函数) winexec,shellexecute , ...
- [转]VC中调用外部exe程序方式
本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...
- delphi 打开和关闭外部exe
一.打开外部exe 1.use文件-SHELLAPI 2.ShellExecute(handle,'open','E:\test.exe','-s','',SW_SHOWNORMAL); 二.关闭外部 ...
- C#嵌入子窗体,判断子窗体是否打开了
/// <summary> /// 嵌入子窗体,判断子窗体是否打开了 /// </summary> public static Form1 f; public void For ...
- 外部exe窗体嵌入winform
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- exe程序嵌入Winform窗体
1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...
- C# winform 将其他程序嵌入Form窗体
嵌入类 public class ExeImpaction { public void FrmClosing() { try { if (!process.HasExited) process.Kil ...
- linux 下串口独占方式打开
参考文章: http://blog.csdn.net/rl529014/article/details/51336161 http://blog.csdn.net/lin_fs/article/de ...
- Python文件使用“wb”方式打开,写入内容
Python文件使用"wb"方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. 所以写入 ...
随机推荐
- 一张图让你看清Java集合类(Java集合类的总结)
如今关于Java集合类的文章非常多,可是我近期看到一个非常有意思图片,基本上把Java集合的整体框架都给展现出来了.非常直观. watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 【Android开发经验】来,咱们自己写一个Android的IOC框架!
到眼下位置.afinal开发框架也是用了好几个月了,还记得第一次使用凝视完毕控件的初始化和事件绑定的时候,当时的心情是多么的兴奋- -代码居然能够这样写!然后随着不断的学习,也慢慢的对IOC框架和注解 ...
- 熟人UML
UML,全名Unified Modeling Language.模语言.它是软件和系统开发的标准建模语言.主要是以图形的方式对系统进行分析.设计. 同一时候,UML不是一个程序设计语言,也不是一个形式 ...
- github中origin和upstream的区别(转)
Fork,本身并不是git工具中的一个命令,也不是对git的扩展,它是在GitHub上的概念,是另一种clone方式——在服务器端的clone.而我们通常意义上的clone,是将远程repo 复制一份 ...
- 编程算法基地-2.1利用字符串API
2.1利用字符串API 字符串是Java类型最常用.并且是复合类型 串非常经常用于,其最佳API熟悉文档. 推断串中有没有反复的字符 String s ="abcdebxyz"; ...
- java求最大公约数(分解质因数)
下面是四种用java语言编程实现的求最大公约数的方法: package gcd; import java.util.ArrayList; import java.util.List; public c ...
- jQuery简要dom操作
文本 dom 获取标签 $(选择). 创建一个标签对象 $("标签"): 由于所有的返回jQuery对象,能够调用链(无论jQuery API 回报jQuery对象) 插入标签 内 ...
- js:深闭包(范围:上)
/** * 范围封锁 */ fn1(); //fn1 您可以运行,没有报错,对于由function func_name()这样的写法来定义的函数,永远都会被最先初始化. function fn1( ...
- 【足迹C++primer】47、Moving Objects(1)
Moving Objects(1) * 功能:Moving Objects * 时间:2014年7月17日08:46:45 * 作者:cutter_point */ #include<iostr ...
- jQuery.extend()方法和jQuery.fn.extend()方法
jQuery.extend()方法和jQuery.fn.extend()方法源码分析 这两个方法用的是相同的代码,一个用于给jQuery对象或者普通对象合并属性和方法一个是针对jQuery对象的实例, ...