using System; using System.Text; using System.Drawing; using System.Runtime.InteropServices; using static WinAPI.Struct; using System.Windows.Forms; namespace WinAPI { /// /// /// public partial class User32 { #region UnmanagedMethods /// /// 模拟鼠标点击 /// /// /// /// /// /// /// [DllImport("user32")] public static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); /// /// 模拟按键 /// /// /// /// /// [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo); /// /// /// /// /// [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetWindowDC(IntPtr hWnd); /// /// 移动窗口 /// /// /// /// /// /// /// /// [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint); /// /// /// /// /// /// /// /// /// [DllImport("User32.dll")] public extern static IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam); /// /// /// /// /// /// /// [DllImport("User32.dll")] public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong); /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndParent); /// /// The GetParent function retrieves a handle to the specified window's parent or owner. /// /// Handle to the window whose parent window handle is to be retrieved. /// If the window is a child window, the return value is a handle to the parent window. If the window is a top-level window, the return value is a handle to the owner window. If the window is a top-level unowned window or if the function fails, the return value is NULL. [DllImport("user32", CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hwnd); /// /// /// /// /// /// [DllImport("USER32.DLL")] public static extern int GetSystemMenu(IntPtr hwnd, int bRevert); /// /// /// /// /// /// /// [DllImport("USER32.DLL")] public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags); /// /// /// /// /// [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); /// /// 获取当前前台窗口句柄 /// /// [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow(); /// /// 获得当前活动窗体 /// /// [DllImport("user32.dll")] public static extern IntPtr GetActiveWindow();//获得当前活动窗体 /// /// 设置活动窗体 /// /// /// [DllImport("user32.dll")] public static extern IntPtr SetActiveWindow(IntPtr hwnd);//设置活动窗体 /// /// 获取类名 /// /// /// /// /// [DllImport("user32", EntryPoint = "GetClassName", SetLastError = false, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount); /// /// 获取当前线程对应的进程ID /// /// /// /// [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); /// /// 判断指定句柄是否是一个窗口 /// /// /// [DllImport("user32.dll", EntryPoint = "IsWindow")] public static extern bool IsWindow(IntPtr hWnd); /// /// 获取窗口标题 /// /// /// /// /// [DllImport("user32", SetLastError = true)] public static extern int GetWindowText( int hWnd,//窗口句柄 StringBuilder lpString,//标题 int nMaxCount //最大值 ); /// /// /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern int SendMessage( IntPtr hwnd, int wMsg, int wParam, int lParam); /// /// /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern int SendMessage( IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); /// /// /// /// /// /// /// /// [DllImport("user32.dll", SetLastError = true)] public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); /// /// 发送消息 /// /// /// /// /// /// [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam); /// /// 发送消息 /// /// /// /// /// [DllImport("user32.dll")] public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, ref COPYDATASTRUCT IParam); /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern IntPtr LoadCursorFromFile(string filename); /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern bool IsWindowVisible(IntPtr hwnd); /// /// 查找窗口 /// /// /// /// [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// /// The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the specified child window. /// /// Handle to the parent window whose child windows are to be searched. /// Handle to a child window. /// Specifies class name. /// Pointer to a null-terminated string that specifies the window name (the window's title). /// If the function succeeds, the return value is a handle to the window that has the specified class and window names.If the function fails, the return value is NULL. [DllImport("user32", CharSet = CharSet.Auto)] public static extern IntPtr FindWindowEx( IntPtr hwndParent, IntPtr hwndChildAfter, [MarshalAs(UnmanagedType.LPTStr)] string lpszClass, [MarshalAs(UnmanagedType.LPTStr)] string lpszWindow); /// /// The InvalidateRect function adds a rectangle to the specified window's update region. /// /// Handle to window. /// Rectangle coordinates. /// Erase state. /// If the function succeeds, the return value is true.If the function fails, the return value is false. [DllImport("user32", CharSet = CharSet.Auto)] public static extern bool InvalidateRect( IntPtr hwnd, ref Rectangle rect, bool bErase); /// /// The ValidateRect function validates the client area within a rectangle by removing the rectangle from the update region of the specified window. /// /// Handle to window. /// Validation rectangle coordinates. /// If the function succeeds, the return value is true.If the function fails, the return value is false. [DllImport("user32", CharSet = CharSet.Auto)] public static extern bool ValidateRect( IntPtr hwnd, ref Rectangle rect); /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern int GetWindowLong( IntPtr hWnd, int dwStyle); /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern IntPtr GetDC( IntPtr hwnd); /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern int ReleaseDC( IntPtr hwnd, IntPtr hdc); //[DllImport("user32", SetLastError = false)] //internal static extern IntPtr GetDesktopWindow(); //[DllImport("user32", CharSet = CharSet.Auto)] //internal static extern int GetScrollPos( // IntPtr hwnd, // int nBar); /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern int GetClientRect( IntPtr hwnd, [In, Out] ref Rectangle rect); /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern bool GetWindowRect( IntPtr hWnd, out Rectangle rect); /// /// /// /// /// /// /// /// /// /// /// /// /// [DllImport("user32", ExactSpelling = true, SetLastError = true)] public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags); /// /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern uint SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong); /// /// Changes the size, position, and Z order of a child, pop-up, or top-level window. /// These windows are ordered according to their appearance on the screen. /// The topmost window receives the highest rank and is the first window in the Z order. /// /// A handle to the window. /// A handle to the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values. /// Specifies the new position of the left side of the window, in client coordinates. /// Specifies the new position of the top of the window, in client coordinates. /// Specifies the new width of the window, in pixels. /// Specifies the new height of the window, in pixels. /// Specifies the window sizing and positioning flags. This parameter can be a combination of the following values. /// If the function succeeds, the return value is nonzero, if the function fails, the return value is zero. [DllImport("user32", CharSet = CharSet.Auto)] public static extern int SetWindowPos( IntPtr hWnd, IntPtr hWndAfter, int X, int Y, int Width, int Height, FlagsSetWindowPos flags); /// /// /// /// /// /// /// /// /// /// /// [DllImport("user32", CharSet = CharSet.Auto)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); #endregion } }