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);
            }
        }
    }
    
  • 相关阅读:
    搜索框
    鼠标hover时改变图片透明度和颜色(方法二)
    让背景带上颜色
    右侧固定导航栏
    react native环境配置
    左侧固定导航栏
    鼠标hover时改变图片透明度和颜色
    androidSDK配置环境变量
    cordova插件开发
    Python电子书分享
  • 原文地址:https://www.cnblogs.com/hourglasser/p/3493366.html
Copyright © 2011-2022 走看看