You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
7.0 KiB
190 lines
7.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Container.Control
|
|
{
|
|
/// <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, 0, 0, fm.Width+2, 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(100000);
|
|
|
|
}
|
|
}
|
|
}
|
|
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, 0, 0, this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height, true);
|
|
// }
|
|
// base.OnResize(e);
|
|
//}
|
|
}
|
|
}
|
|
|