zoukankan      html  css  js  c++  java
  • C# 使用Windows API获取系统当前鼠标信息(图案)

      通过使用Windows API来获取当前鼠标的图案,不论是系统图片还是自定义图标都能够获取到,在这个示例中,为了方便测试,给Form1添加了一个KeyPress事件,在程序激活状态下,将鼠标移动到任意能使鼠标图案不同的地方,随便按下一个按键,将会将当前鼠标图案描绘到Form1窗体中。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace GetWindowsCurrentCursorByWindowsAPI
    {
        public partial class Form1 : Form
        {
            [StructLayout(LayoutKind.Sequential)]
            struct CURSORINFO
            {
                public int cbSize;
                public int flags;
                public IntPtr hCursor;
                public Point ptScreenPos;
            }
            [DllImport("user32.dll")]
            static extern bool GetCursorInfo(out CURSORINFO pci);
            private const int CURSOR_SHOWING = 0x00000001;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                CURSORINFO vCurosrInfo;
                vCurosrInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
                GetCursorInfo(out vCurosrInfo);
                if ((vCurosrInfo.flags & CURSOR_SHOWING) != CURSOR_SHOWING) return;
                Cursor vCursor = new Cursor(vCurosrInfo.hCursor);
                Graphics vGraphics = Graphics.FromHwnd(Handle);
                Rectangle vRectangle = new Rectangle(95, 50, 32, 32);
                vGraphics.FillRectangle(new SolidBrush(BackColor), vRectangle);
                vCursor.Draw(vGraphics, vRectangle);
            }
        }
    }
    
  • 相关阅读:
    ATmega328P定时器详解
    成员指针与mem_fn
    引用传参与reference_wrapper
    定位new表达式与显式调用析构函数
    模板参数的“右值引用”是转发引用
    C++生成随机数
    测量C++程序运行时间
    Snmp扫描-snmpwalk、snmpcheck
    操作系统识别-python、nmap
    服务扫描-dmitry、nmap、amap和服务识别
  • 原文地址:https://www.cnblogs.com/hourglasser/p/3493366.html
Copyright © 2011-2022 走看看