namespace zeroflag.Windows { using System; using System.Diagnostics; using System.Drawing; using System.Threading; using System.Windows.Forms; public class Window { private DebugMode m_Debug; private string m_DebugLog; private IntPtr m_Handle; private System.Windows.Forms.MouseButtons m_MouseButtons; System.Diagnostics.Process m_Process; public System.Diagnostics.Process Process { get { return m_Process; } set { m_Process = value; if (value != null) { this.Handle = value.MainWindowHandle; } } } public Window(System.Diagnostics.Process process) : this(process.MainWindowHandle) { this.Process = process; } public Window(IntPtr handle) { this.m_DebugLog = ""; this.Handle = handle; } protected void _MouseDown(Point location, System.Windows.Forms.MouseButtons button) { IntPtr position = WinAPI.ConvertMousePosition(location); this.MouseButtons |= button; zeroflag.Windows.MouseButtons winbutton = WinAPI.ConvertMouseButtons(this.MouseButtons); if ((button & System.Windows.Forms.MouseButtons.Left) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_LBUTTONDOWN, (IntPtr) ((long) winbutton), position); } else if ((button & System.Windows.Forms.MouseButtons.Right) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_RBUTTONDOWN, (IntPtr) ((long) winbutton), position); } else if ((button & System.Windows.Forms.MouseButtons.Middle) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_MBUTTONDOWN, (IntPtr) ((long) winbutton), position); } WinAPI.SendMessage(this.Handle, WindowsMessages.WM_NCHITTEST, WinAPI.NULL, position); } protected void _MouseUp(Point location, System.Windows.Forms.MouseButtons button) { IntPtr position = WinAPI.ConvertMousePosition(location); this.MouseButtons &= ~button; zeroflag.Windows.MouseButtons winbutton = WinAPI.ConvertMouseButtons(this.MouseButtons); if ((button & System.Windows.Forms.MouseButtons.Left) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_LBUTTONUP, (IntPtr) ((long) winbutton), position); } else if ((button & System.Windows.Forms.MouseButtons.Right) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_RBUTTONUP, (IntPtr) ((long) winbutton), position); } else if ((button & System.Windows.Forms.MouseButtons.Middle) != System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_MBUTTONUP, (IntPtr) ((long) winbutton), position); } WinAPI.SendMessage(this.Handle, WindowsMessages.WM_NCHITTEST, WinAPI.NULL, position); } public void Activate(bool actiavte) { IntPtr wParam = actiavte ? ((IntPtr) 1) : IntPtr.Zero; WinAPI.SendMessage(this.Handle, WindowsMessages.WM_ACTIVATE, wParam, WinAPI.NULL); } public void KeyDown(Keys key) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_KEYFIRST, (IntPtr) ((long) key), new IntPtr()); } public void KeyUp(Keys key) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_KEYUP, (IntPtr) ((long) key), new IntPtr()); } public void MouseClick(Point location, System.Windows.Forms.MouseButtons button) { this.MouseDown(location, button); Thread.Sleep(100); this.MouseUp(location, button); } public void MouseDown(Point location, System.Windows.Forms.MouseButtons button) { this.MouseMove(location); this._MouseDown(location, button); } public void MouseMove(Point location) { this.MouseMove(location, this.MouseButtons); } protected void MouseMove(Point location, System.Windows.Forms.MouseButtons button) { IntPtr position = WinAPI.ConvertMousePosition(location); zeroflag.Windows.MouseButtons winbutton = WinAPI.ConvertMouseButtons(button); this.SendDebug(WindowsMessages.WM_MOUSEFIRST, (IntPtr) ((long) winbutton), position); WinAPI.SendMessage(this.Handle, WindowsMessages.WM_MOUSEFIRST, (IntPtr) ((long) winbutton), position); WinAPI.SendMessage(this.Handle, WindowsMessages.WM_NCHITTEST, WinAPI.NULL, position); if (button == System.Windows.Forms.MouseButtons.None) { WinAPI.SendMessage(this.Handle, WindowsMessages.WM_SETCURSOR, this.Handle, WinAPI.Build(1, 0x200)); } } public void MouseUp(Point location, System.Windows.Forms.MouseButtons button) { this._MouseUp(location, button); this.MouseMove(location); } protected void SendDebug(object value) { string text = ""; if (value != null) { text = value.ToString(); } switch (this.Debug) { case DebugMode.Console: Console.WriteLine(text); return; case DebugMode.String: this.DebugLog = this.DebugLog + text; return; } } protected void SendDebug(WindowsMessages msg, int wParam, int lParam) { this.SendDebug(msg, (IntPtr) wParam, (IntPtr) lParam); } protected void SendDebug(WindowsMessages msg, IntPtr wParam, IntPtr lParam) { this.SendDebug(string.Concat(new object[] { msg, " wParam=0x", wParam.ToString("X").PadLeft(8, '0'), ", lParam=0x", lParam.ToString("X").PadLeft(8, '0') })); } public DebugMode Debug { get { return this.m_Debug; } set { this.m_Debug = value; } } public string DebugLog { get { return this.m_DebugLog; } protected set { this.m_DebugLog = value; } } public IntPtr Handle { get { return this.m_Handle; } set { this.m_Handle = value; } } public System.Windows.Forms.MouseButtons MouseButtons { get { return this.m_MouseButtons; } protected set { this.m_MouseButtons = value; } } public Rectangle ScreenRegion { get { return Screen.FromHandle(this.Handle).WorkingArea; } } public System.Drawing.Size Size { get { WinAPI.LPRECT rect = new WinAPI.LPRECT(); WinAPI.GetClientRect(this.Handle, ref rect); return new System.Drawing.Size((int) (rect.right - rect.left), (int) (rect.bottom - rect.top)); } } public enum DebugMode { None, Console, String } } }