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的更多相关文章

  1. MFC 打开外部EXE文件的三种方法

    目前知道三种方式:WinExec,ShellExecute ,CreateProcess,别人已经总结的很好了<vc中调用其他应用程序的方法(函数) winexec,shellexecute , ...

  2. [转]VC中调用外部exe程序方式

    本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...

  3. delphi 打开和关闭外部exe

    一.打开外部exe 1.use文件-SHELLAPI 2.ShellExecute(handle,'open','E:\test.exe','-s','',SW_SHOWNORMAL); 二.关闭外部 ...

  4. C#嵌入子窗体,判断子窗体是否打开了

    /// <summary> /// 嵌入子窗体,判断子窗体是否打开了 /// </summary> public static Form1 f; public void For ...

  5. 外部exe窗体嵌入winform

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  6. exe程序嵌入Winform窗体

    1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...

  7. C# winform 将其他程序嵌入Form窗体

    嵌入类 public class ExeImpaction { public void FrmClosing() { try { if (!process.HasExited) process.Kil ...

  8. linux 下串口独占方式打开

    参考文章: http://blog.csdn.net/rl529014/article/details/51336161  http://blog.csdn.net/lin_fs/article/de ...

  9. Python文件使用“wb”方式打开,写入内容

    Python文件使用"wb"方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. 所以写入 ...

随机推荐

  1. Vijos.1096 津津储蓄计划

    见问题: https://vijos.org/p/1096 主题概述 津津的零花钱一直都是自己的管理.每月初的母亲津津300美元,津津将于本月支出预算.而且总是做同样的实际支出与预算.  为了让津津学 ...

  2. Android日志收集功能设计和实施报告(总)

    几乎所有的应用程序开发者都知道"用户体验"重视,为了增强用户体验是从全面的监测和报告制度密不可分,在这些日志(控制Crash报告)是主要的问题跟踪和解决工具. 日志上报系统.首先看 ...

  3. [SignalR]一个简单的聊天室

    原文:[SignalR]一个简单的聊天室 1.说明 开发环境:Microsoft Visual Studio 2010 以及需要安装NuGet. 2.添加SignalR所需要的类库以及脚本文件: 3. ...

  4. 无插件,直接加参数,chrome它可以模拟手机浏览器

    在目标出现,加上一些参数即可:--user-agent="mozilla/5.0 (linux; u; android 2.3.3; en-us; sdk build/ gri34) app ...

  5. JavaScript-2.2 document.write 输出到页面的内容

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  6. 试图加载格式不正确的程序。 (Exception from HRESULT: 0x8007000B)

    原文:试图加载格式不正确的程序. (Exception from HRESULT: 0x8007000B) 今天在电脑上部署公司的项目,出现这个错误.Bing后,找到原来是因为项目是32位的,而我的系 ...

  7. Android SDCard Mount 流程分析

    前段时间对Android 的SDCard unmount 流程进行了几篇简短的分析,由于当时只是纸上谈兵,没有实际上的跟进,可能会有一些误导人或者小错误.今天重新梳理了头绪,针对mount的流程再重新 ...

  8. Codeforces 135A-Replacement(思维)

    A. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  9. 交易应用-运行多个SQL声明

    事务具有原子性.要么不运行.要么全运行.一旦成功运行永久保存.而这些正是因为事务的原子性和对数据库的持久性形成的.下面是一个关于统一给数据库中的数据改动的批量操作,利用到事务. TODO:批量改动数据 ...

  10. Win7安装和配置Tigase 5.2server

    Win7安装和配置Tigaseserver 笔者:chszs,转载注明. 博客首页:http://blog.csdn.net/chszs 1.下载tigase-server-5.2.0-b3447.e ...