zoukankan      html  css  js  c++  java
  • 几个常用的API在c#中的定义

    最近一个项目中用到的一些API,在解决一些实际的问题上(特别是和外部程序打交道)的时候还是蛮有用的。具体的参数什么的网上都有!

    代码
    public class API
    {
    #region 窗口函数

    [DllImport(
    "user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport(
    "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport(
    "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
    public static extern void SetForegroundWindow(IntPtr hwnd);

    [DllImport(
    "user32.dll", EntryPoint = "GetWindow", SetLastError = true)]
    public static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);

    [DllImport(
    "user32.dll", EntryPoint = "GetClassName", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int GetClassName(IntPtr hWnd, StringBuilder buf, int nMaxCount);

    [DllImport(
    "user32.dll", EntryPoint = "SetWindowPos", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool SetWindowPos(IntPtr hWnd, int hwndinsertAfter, int x, int y, int cx, int cy, int wFlags);

    //显示窗体(包括使得窗体最小化,最大化等等操作)
    [DllImport("user32.dll ")]
    public static extern bool ShowWindow(IntPtr hwnd, int cmdshow);

    /// <summary>
    /// 设置窗体标题
    /// </summary>
    /// <param name="hwnd">窗体句柄</param>
    /// <param name="lpStrjng">要设置的标题</param>
    /// <returns></returns>
    [DllImport("user32.dll ")]
    public static extern bool SetWindowText(IntPtr hwnd, string lpStrjng);

    /// <summary>
    /// 获得窗体的位置(相对于整个屏幕)
    /// </summary>
    /// <param name="hwnd">窗体句柄</param>
    /// <param name="rc">自定义的矩形结构,请调用该类中的RectangleEx</param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern int GetWindowRect(IntPtr hwnd, ref RectangleEx rc);
    public struct RectangleEx
    {
    public int leftTopX;
    public int leftTopY;
    public int rightBottomX;
    public int rightBottomY;

    public int Width
    {
    get { return rightBottomX - leftTopX; }
    }

    public int Height
    {
    get { return rightBottomY - leftTopY; }
    }

    }

    /// <summary>
    /// 获得包含指定点的窗口的句柄
    /// </summary>
    /// <param name="p">一个被检测的点的POINT结构</param>
    /// <returns></returns>
    [DllImport("user32.dll ")]
    public static extern IntPtr WindowFromPoint(Point p);

    /// <summary>
    /// 获得窗口文字
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="lpString">用来保存文字的stringBuilder</param>
    /// <param name="nMaxCount">stringBuilder的容量</param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
    int nMaxCount);


    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    /// <summary>
    /// 枚举所有控件
    /// </summary>
    /// <param name="lpEnumFunc"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);


    #endregion

    #region 消息函数

    //发送消息的类型(第二个参数)
    public const int WM_KEYDOWN = 0X100;
    public const int WM_KEYUP = 0X101;
    public const int WM_CHAR = 0X102;
    /// <summary>
    /// 发送消息,等待返回
    /// </summary>
    /// <param name="hWnd">窗体句柄</param>
    /// <param name="wMsg">被发送的消息类型(如键盘按下消息,字符消息等等)</param>
    /// <param name="wParam">附加的消息指定信息</param>
    /// <param name="lParam">附加的消息指定信息</param>
    /// <returns></returns>
    [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

    [DllImport(
    "user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
    public static extern IntPtr PostMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

    #endregion

    #region 鼠标函数

    [DllImport(
    "user32.dll", EntryPoint = "SetCursorPos")]
    internal extern static int SetCursorPos(int x, int y);

    /// 使用范例: API.mouse_event(0x2 | 0x4, x,y, 0, 0); 在(x,y)处单击一下
    public readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
    public readonly int MOUSEEVENTF_LEFTUP = 0x4;
    [DllImport(
    "user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);


    #endregion

    #region 键盘函数


    public static int KEYEVENTF_EXTENDEDKEY = 0x0001;
    public static int KEYEVENTF_KEYUP = 0x0002;
    public static byte VK_LWIN = 0x5B;
    [DllImport(
    "user32.dll", EntryPoint = "keybd_event")]
    public static extern void keybd_event(
    byte bVk,
    byte bScan,
    int dwFlags,
    int dwExtraInfo
    );


    #endregion

    #region 自绘函数

    /// <summary>
    /// 获得窗口指定点处的句柄
    /// </summary>
    /// <param name="hwnd">主窗体句柄</param>
    /// <param name="top">该点相对于主窗体上边框的距离</param>
    /// <param name="left">该点相对于主窗体左边框的距离</param>
    /// <returns></returns>
    public static IntPtr GetWinPartFromPoint(IntPtr hwnd, int top, int left)
    {
    RectangleEx r
    = new RectangleEx();
    GetWindowRect(hwnd,
    ref r);
    //转化为整个屏幕的坐标
    int topScreen = top + r.leftTopY;
    int leftScreen = left + r.leftTopX;
    return WindowFromPoint(new Point(topScreen, leftScreen));
    }

    /// <summary>
    /// 到某处单击一下返回
    /// </summary>
    /// <param name="ox">原来的鼠标地点x</param>
    /// <param name="oy"></param>
    /// <param name="x">目标地点x</param>
    /// <param name="y"></param>
    public static void ClickPosition(int ox,int oy,int x, int y)
    {
    SetCursorPos(x, y);
    mouse_event(
    0x2 | 0x4, x, y, 0, 0);
    SetCursorPos(ox, oy);
    }

    #endregion
    }
  • 相关阅读:
    Postman请求Https接口与认证
    HTML实用
    ORM实例教程_转
    web跨域问题CORS
    gin入门
    swagger应用
    k8s之容器
    腾讯高级工程师:如何从头开始写游戏服务器框架_转
    tensorflow入门
    sublime Text 3实用功能和常用快捷键收集
  • 原文地址:https://www.cnblogs.com/qianlifeng/p/1765459.html
Copyright © 2011-2022 走看看