启动程序
String path = @"C:\Program Files (x86)\Babelbird\Babelbird.exe"; //VusionDTI ImAgenGine_MRDP
IntPtr hcalc = IntPtr.Zero; //用以存储目标窗口句柄
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = Environment.ExpandEnvironmentVariables(path); //exePath要启动的外部应用程序路径
proc = Process.Start(ps);
指定容器
System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
WindowsFormsHost host = new WindowsFormsHost();
host.Child = p;
grid.Children.Add(host);
获取窗口句柄并操作
hcalc = FindWindow(null, "xxx客户端");
SetParent(hcalc, hpanel1);
SetWindowPos(hcalc, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
Thread.Sleep(5000);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 700 * 65536 / 2560, 710 * 65536 / 1440, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 700 * 65536 / 2560, 710 * 65536 / 1440, 0, 0);
调用的win32 API
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
const int BM_CLICK = 0xF5;
const short SWP_NOMOVE = 0X2;
const short SWP_NOSIZE = 1;
const short SWP_NOZORDER = 0X4;
const int SWP_SHOWWINDOW = 0x0040;
const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标
const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //主要用于发现外部软件窗口的句柄
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //该api用于嵌入到窗口中运行
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam); //对外部软件窗口发送一些消息(如 窗口最大化、最小化等)
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);