所谓"绑架"就是把其他Win32程序的窗体嵌入到我们托管的WinForm中.网上已经用很多java版和Delphi版还有WPF的.我在这里补充C#版的.
定义需要的Win32 API
启动我们需要绑架的程序
需要注意的是:ResizeControl()方法很重要,否则达不到我们预期的效果.
定义需要的Win32 API
[DllImport("user32.dll")]
private static extern int SetParent(IntPtr hWndChild,IntPtr hWndParent);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hWnd,int hWndInsertAfter,
int X,int Y,int cx,int cy,uint uFlags);
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static public extern bool ShowWindow(IntPtr hWnd, short State);
private const int HWND_TOP = 0x0;
private const int WM_COMMAND = 0x0112;
private const int WM_QT_PAINT = 0xC2DC;
private const int WM_PAINT = 0x000F;
private const int WM_SIZE = 0x0005;
private const int SWP_FRAMECHANGED = 0x0020;
private static extern int SetParent(IntPtr hWndChild,IntPtr hWndParent);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hWnd,int hWndInsertAfter,
int X,int Y,int cx,int cy,uint uFlags);
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static public extern bool ShowWindow(IntPtr hWnd, short State);
private const int HWND_TOP = 0x0;
private const int WM_COMMAND = 0x0112;
private const int WM_QT_PAINT = 0xC2DC;
private const int WM_PAINT = 0x000F;
private const int WM_SIZE = 0x0005;
private const int SWP_FRAMECHANGED = 0x0020;
启动我们需要绑架的程序
private void Form1_Load(object sender, EventArgs e)
{
p = new Process();
//需要启动的程序
p.StartInfo.FileName = @"c:\windows\system32\notepad.exe";
//为了美观,启动的时候最小化程序
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
//启动
p.Start();
//这里必须等待,否则启动程序的句柄还没有创建,不能控制程序
Thread.Sleep(1000);
//最大化启动的程序
ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_MAXIMIZE);
//设置被绑架程序的父窗口
SetParent(p.MainWindowHandle, this.Handle);
//改变尺寸
ResizeControl();
}
//控制嵌入程序的位置和尺寸
private void ResizeControl()
{
SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0);
PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0);
SetWindowPos(
p.MainWindowHandle,
HWND_TOP,
0-5,//设置偏移量,把原来窗口的菜单遮住
0 - 41,
(int)this.Width,
(int)this.Height+36,
SWP_FRAMECHANGED);
SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
ResizeControl();
}
{
p = new Process();
//需要启动的程序
p.StartInfo.FileName = @"c:\windows\system32\notepad.exe";
//为了美观,启动的时候最小化程序
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
//启动
p.Start();
//这里必须等待,否则启动程序的句柄还没有创建,不能控制程序
Thread.Sleep(1000);
//最大化启动的程序
ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_MAXIMIZE);
//设置被绑架程序的父窗口
SetParent(p.MainWindowHandle, this.Handle);
//改变尺寸
ResizeControl();
}
//控制嵌入程序的位置和尺寸
private void ResizeControl()
{
SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0);
PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0);
SetWindowPos(
p.MainWindowHandle,
HWND_TOP,
0-5,//设置偏移量,把原来窗口的菜单遮住
0 - 41,
(int)this.Width,
(int)this.Height+36,
SWP_FRAMECHANGED);
SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
ResizeControl();
}
需要注意的是:ResizeControl()方法很重要,否则达不到我们预期的效果.