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. PC结束 Spark 二次开发 收到自己主动,并允许好友请求

    本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...

  2. Codeforces 448 D. Multiplication Table

    二分法判断答案 D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes inp ...

  3. lua转让C++书面DLL达到“热更新”

    原创作品,请注明出处转载CSDN:http://blog.csdn.net/relar/article/details/38084689 开发游戏server往往有"热更新"的需求 ...

  4. Html.Partial和Html. RenderPartial用法

    Html.Partial和Html. RenderPartial用法 Html.partial和RenderPartial的用法与区别Html.partial和RenderPartial都是输出htm ...

  5. ASP.NET MVC:Razor 引入命名空间

    原文:ASP.NET MVC:Razor 引入命名空间 页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行 ...

  6. jQuery形式可以计算,它包含了无线电的变化价格,select价格变化,删除行动态计算加盟

    jQuery能够计算的表单,包含单选改变价格,select改变价格,动态加入删除行计算 各种表单情况的计算 演示 JavaScript Code <script type="text/ ...

  7. js 通信

    js 页面间的通信 看了一下公司原来的代码,原页面ajax post返回一个页面完整的HTML,然后再打开一个新页面并输出ajax返回的所有代码到新页面上,在新页面上以表单提交的形式实现重定向. 任凭 ...

  8. sql多表查询之一:Where 和 On的秘密

    原文 sql多表查询之一:Where 和 On的秘密 对于还在SQL初级阶段的朋友来说,sql多表查询问题是一个比较有趣也容易出错的技术.什么时候会用到sql多表查询呢?是在两张或两张以上表单中通过某 ...

  9. Office——检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 8000401a 1.运行dcomcnfg.e ...

  10. POJ 1664 把苹果

     把苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25785   Accepted: 16403 Descript ...