------ #### MyDb V2.1.2102.1101 - *.[新增]新增Auto类,增加 鼠标模拟操作和按键模拟操作。 - *.[新增]新增WinAPI命名空间,将windows自带的API集中在这个命名空间。
289 lines
12 KiB
C#
289 lines
12 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Drawing;
|
|
using System.Runtime.InteropServices;
|
|
using static WinAPI.Struct;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Win32API
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public partial class User32
|
|
{
|
|
|
|
|
|
#region UnmanagedMethods
|
|
/// <summary>
|
|
/// 模拟鼠标点击
|
|
/// </summary>
|
|
/// <param name="dwFlags"></param>
|
|
/// <param name="dx"></param>
|
|
/// <param name="dy"></param>
|
|
/// <param name="dwData"></param>
|
|
/// <param name="dwExtraInfo"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32")]
|
|
public static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
|
|
/// <summary>
|
|
/// 模拟按键
|
|
/// </summary>
|
|
/// <param name="bVk"></param>
|
|
/// <param name="bScan"></param>
|
|
/// <param name="dwFlags"></param>
|
|
/// <param name="dwExtraInfo"></param>
|
|
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
|
|
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <returns></returns>
|
|
[DllImport("User32.dll", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr GetWindowDC(IntPtr hWnd);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <param name="width"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="repaint"></param>
|
|
/// <returns></returns>
|
|
[DllImport("User32.dll", CharSet = CharSet.Auto)]
|
|
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwndChild"></param>
|
|
/// <param name="hwndParent"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndParent);
|
|
|
|
/// <summary>
|
|
/// The GetParent function retrieves a handle to the specified window's parent or owner.
|
|
/// </summary>
|
|
/// <param name="hwnd">Handle to the window whose parent window handle is to be retrieved.</param>
|
|
/// <returns>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.</returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr GetParent(IntPtr hwnd);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <param name="wMsg"></param>
|
|
/// <param name="wParam"></param>
|
|
/// <param name="lParam"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern int SendMessage(
|
|
IntPtr hwnd,
|
|
int wMsg,
|
|
int wParam,
|
|
int lParam);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <param name="wMsg"></param>
|
|
/// <param name="wParam"></param>
|
|
/// <param name="lParam"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern int SendMessage(
|
|
IntPtr hwnd,
|
|
int wMsg,
|
|
IntPtr wParam,
|
|
IntPtr lParam);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="Msg"></param>
|
|
/// <param name="wParam"></param>
|
|
/// <param name="lParam"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="filename"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr LoadCursorFromFile(string filename);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern bool IsWindowVisible(IntPtr hwnd);
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="hwndParent">Handle to the parent window whose child windows are to be searched.</param>
|
|
/// <param name="hwndChildAfter">Handle to a child window.</param>
|
|
/// <param name="lpszClass">Specifies class name.</param>
|
|
/// <param name="lpszWindow">Pointer to a null-terminated string that specifies the window name (the window's title).</param>
|
|
/// <returns>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.</returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr FindWindowEx(
|
|
IntPtr hwndParent,
|
|
IntPtr hwndChildAfter,
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
string lpszClass,
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
string lpszWindow);
|
|
|
|
/// <summary>
|
|
/// The InvalidateRect function adds a rectangle to the specified window's update region.
|
|
/// </summary>
|
|
/// <param name="hwnd">Handle to window.</param>
|
|
/// <param name="rect">Rectangle coordinates.</param>
|
|
/// <param name="bErase">Erase state.</param>
|
|
/// <returns>If the function succeeds, the return value is true.If the function fails, the return value is false.</returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern bool InvalidateRect(
|
|
IntPtr hwnd,
|
|
ref Rectangle rect,
|
|
bool bErase);
|
|
|
|
/// <summary>
|
|
/// The ValidateRect function validates the client area within a rectangle by removing the rectangle from the update region of the specified window.
|
|
/// </summary>
|
|
/// <param name="hwnd">Handle to window.</param>
|
|
/// <param name="rect">Validation rectangle coordinates.</param>
|
|
/// <returns>If the function succeeds, the return value is true.If the function fails, the return value is false.</returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern bool ValidateRect(
|
|
IntPtr hwnd,
|
|
ref Rectangle rect);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="dwStyle"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern int GetWindowLong(
|
|
IntPtr hWnd,
|
|
int dwStyle);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern IntPtr GetDC(
|
|
IntPtr hwnd);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <param name="hdc"></param>
|
|
/// <returns></returns>
|
|
[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);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <param name="rect"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern int GetClientRect(
|
|
IntPtr hwnd,
|
|
[In, Out] ref Rectangle rect);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="rect"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern bool GetWindowRect(
|
|
IntPtr hWnd,
|
|
[In, Out] ref Rectangle rect);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hwnd"></param>
|
|
/// <param name="hdcDst"></param>
|
|
/// <param name="pptDst"></param>
|
|
/// <param name="psize"></param>
|
|
/// <param name="hdcSrc"></param>
|
|
/// <param name="pprSrc"></param>
|
|
/// <param name="crKey"></param>
|
|
/// <param name="pblend"></param>
|
|
/// <param name="dwFlags"></param>
|
|
/// <returns></returns>
|
|
[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);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="nIndex"></param>
|
|
/// <param name="dwNewLong"></param>
|
|
/// <returns></returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern uint SetWindowLong(
|
|
IntPtr hWnd,
|
|
int nIndex,
|
|
int dwNewLong);
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="hWnd">A handle to the window.</param>
|
|
/// <param name="hWndAfter">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.</param>
|
|
/// <param name="X">Specifies the new position of the left side of the window, in client coordinates.</param>
|
|
/// <param name="Y">Specifies the new position of the top of the window, in client coordinates.</param>
|
|
/// <param name="Width">Specifies the new width of the window, in pixels.</param>
|
|
/// <param name="Height">Specifies the new height of the window, in pixels.</param>
|
|
/// <param name="flags">Specifies the window sizing and positioning flags. This parameter can be a combination of the following values.</param>
|
|
/// <returns>If the function succeeds, the return value is nonzero, if the function fails, the return value is zero.</returns>
|
|
[DllImport("user32", CharSet = CharSet.Auto)]
|
|
public static extern int SetWindowPos(
|
|
IntPtr hWnd,
|
|
IntPtr hWndAfter,
|
|
int X,
|
|
int Y,
|
|
int Width,
|
|
int Height,
|
|
FlagsSetWindowPos flags);
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="hWnd"></param>
|
|
/// <param name="hWndInsertAfter"></param>
|
|
/// <param name="X"></param>
|
|
/// <param name="Y"></param>
|
|
/// <param name="cx"></param>
|
|
/// <param name="cy"></param>
|
|
/// <param name="uFlags"></param>
|
|
/// <returns></returns>
|
|
[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
|
|
}
|
|
} |