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"方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. 所以写入 ...
随机推荐
- JavaScript的隐式转换
原文:JavaScript的隐式转换 JavaScript的数据类型分为六种,分别为null,undefined,boolean,string,number,object.object是引用类型,其它 ...
- Web 前端开发环境
创建 Web 前端开发环境 Web 前端开发涉及多种工具,这里将常用工具的安装和配置进行说明,提供了详细的说明,为后继的开发创建一个坚实的基础. 本文介绍的工具有:NodeJS, NPM, Bower ...
- 关于继承modelDriven接口action的ajax来电参数
例如 Model类如下面,Teacher,public class Teacher{ private Integer id. priavte String name; private Sch ...
- jQuery简要dom操作
文本 dom 获取标签 $(选择). 创建一个标签对象 $("标签"): 由于所有的返回jQuery对象,能够调用链(无论jQuery API 回报jQuery对象) 插入标签 内 ...
- dede织梦背景经常使用标签
一些非常实用的标签调用的方法 关键描写叙述调用标签: <meta name="keywords" content="{dede:field name='keywor ...
- cocos2d-x 发动机分析:程序如何开始和结束?
原创地址:http://game.dapps.net/gamedev/game-engine/9515.html 感谢原创分享! 怎么样使用 Cocos2d-x 高速开发游戏,方法非常easy,你能够 ...
- 10个devexpress ASPxPivotGrid常见问题
原文:10个devexpress ASPxPivotGrid常见问题 1.DXperience ASPxGridView如何开启lightweight模式 描述:ASPxGridView样式主题中pa ...
- 从头开始学JavaScript (十二)——Array类型
原文:从头开始学JavaScript (十二)--Array类型 一.数组的创建 注:ECMAscript数组的每一项都可以保存任何类型的数据 1.1Array构造函数 var colors = ne ...
- Eclipse部署Web项目(图文讲解)
讲解是在linux下完成的,但对windows系统,操作也是一样的,不要被吓到了 1.下载Eclipse
- yii 使用 mongodb 小工具 YiiMongoDbSuite
YiiMongoDbSuite下载链接: http://www.yiiframework.com/extension/yiimongodbsuite/ 如果你的yii和mongodb它已经建立了一个良 ...