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"方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. 所以写入 ...
随机推荐
- 初步swift语言学习笔记8(保留了很多OC实现)
笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/32715833 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...
- Spring 5 (0) - Introduction & Index
Spring Framework Reference Documentation I. Overview of Spring Framework . Getting Started with Spri ...
- Python第一个基本教程6章 抽象的
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...
- cocos2d-x由Jni实现Java与C++打电话给对方
cocos2d-x由Jni实现Java与C++打电话给对方. cocos2d-x与开发商提供一个类JniHelper,提供java与c++之间的互jni解. 笔者所开发的"史上最坑爹的游戏& ...
- 图widget--jqplot样品和参数描述的简单演示
最简单的线图 第一步:引入必要的CSS.JS文件 <link rel="stylesheet" type="text/css" href="js ...
- 记2014“蓝桥杯全国软件大赛"决赛北京之行
5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...
- IntelliJ Idea中一个编译报错引发的
package verify; public class Verifier { private String name; public Verifier() { this.name = getClas ...
- zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...
- Android-支付宝快捷支付
支付宝的快捷支付Android版业务流程比較麻烦,出现的意外情况比較多.在此,简单说下开发流程以及出现错误的解决方式; 1.注冊支付业务.这里不在赘述.建立数据安全传输所须要的私钥公钥,这里採用的是R ...
- oracle_windows下命令启动oracle监听和服务
1.检查监听器状态 C:\Users\Administrator>lsnrctl status 2.启动监听程序 C:\Users\Administrator>lsnrctl start ...