c#调用 windows api实现WinForm中嵌入EXE程序
2009-11-13 23:00
585 查看
添加引用:
using System.Diagnostics;
using System.Runtime.InteropServices;
定义:
Process process =
null;
IntPtr appWin;
private string exeName =
"";
[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", 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", 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);
//private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr 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", SetLastError =
true)]
private static
extern bool MoveWindow(IntPtr hwnd,
int x, int y,
int cx, int cy, bool repaint);
[DllImport("user32.dll", EntryPoint =
"PostMessageA", SetLastError =
true)]
private static
extern bool PostMessage(IntPtr hwnd,
uint Msg, long wParam,
long lParam);
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;
public string ExeName
{
get
{
return exeName;
}
set
{
exeName = value;
}
}
调用:
this.exeName
= @"C:/Program Files/Kingview/Touchvew.exe";
try
{
// Start the process
process = System.Diagnostics.Process.Start(this.exeName);
// Wait for process to be created and enter idle condition
process.WaitForInputIdle();
// Get the main handle
appWin = process.MainWindowHandle;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message,
"Error");
}
// Put it into this form
SetParent(appWin, this.splitContainer1.Panel2.Handle);
// Remove border and whatnot
// SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0, this.Width,
this.Height, true);
在控件改变大小的时候,调用:
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);
}
窗口退出时:
private void Form1_FormClosed(object sender,
FormClosedEventArgs e)
{
process.Kill();
// Stop the application
//if (appWin != IntPtr.Zero)
//{
// // Post a colse message
// //PostMessage(appWin, WM_CLOSE, 0, 0);
// // Delay for it to get the message
// // System.Threading.Thread.Sleep(1000);
// // Clear internal handle
// appWin = IntPtr.Zero;
//}
//base.FormClosed(e);
}
相关文章推荐
- c#调用 windows api实现WinForm中嵌入EXE程序
- 用 c 调用 win-api 实现自动点击c# winform 程序 的按钮
- 给C#程序加壳(C# 调用嵌入资源的EXE文件方法)
- C#将exe运行程序嵌入到自己的winform窗体中
- 将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
- 给C#程序加壳(C# 调用嵌入资源的EXE文件方法)
- C# 加壳.exe程序文件(调用嵌入资源的exe文件)
- 将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
- [转]将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
- 黄聪:将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
- 将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
- 在C# WinForm 中嵌入另一个.EXE程序,详细步骤
- C#将exe运行程序嵌入到自己的winform窗体中
- 利用c#实现远程注入非托管WIN32程序,并利用嵌入汇编调用非托管WIN32程序中的内部过程
- C#将exe运行程序嵌入到自己的winform窗体中
- C#将exe运行程序嵌入到自己的winform窗体中
- C#程序实现动态调用DLL的研究
- C#程序实现动态调用DLL的研究
- VS 之 C# 通过console 调用 exe 程序,窗口隐藏设置