Как отловить вид курсора вне формы - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, собственно вопрос заключается как получить вид курсора вне формы. Стремлюсь к коду который будет получать определенный вид курсора вне формы и тогда выполнять функцию.

Решение задачи: «Как отловить вид курсора вне формы»

textual
Листинг программы
  1. using MouseKeyboardActivityMonitor;
  2. using MouseKeyboardActivityMonitor.WinApi;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApp1
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         [StructLayout(LayoutKind.Sequential)]
  12.         struct POINT
  13.         {
  14.             public Int32 x;
  15.             public Int32 y;
  16.         }
  17.  
  18.         [StructLayout(LayoutKind.Sequential)]
  19.         struct CURSORINFO
  20.         {
  21.             public Int32 cbSize;        // Specifies the size, in bytes, of the structure.
  22.                                         // The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
  23.             public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
  24.                                         //    0             The cursor is hidden.
  25.                                         //    CURSOR_SHOWING    The cursor is showing.
  26.             public IntPtr hCursor;          // Handle to the cursor.
  27.             public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor.
  28.         }
  29.  
  30.         /// <summary>Must initialize cbSize</summary>
  31.         [DllImport("user32.dll")]
  32.         static extern bool GetCursorInfo(ref CURSORINFO pci);
  33.  
  34.         Hooker hooker;
  35.         MouseHookListener mhListener;
  36.  
  37.         public Form1()
  38.         {
  39.             InitializeComponent();
  40.  
  41.             this.TopMost = true;
  42.             this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
  43.  
  44.             this.FormClosing += Form1_FormClosing;
  45.  
  46.             hooker = new GlobalHooker();
  47.  
  48.             mhListener = new MouseHookListener(hooker);
  49.            
  50.             mhListener.MouseMove += Mhl_MouseMove;
  51.  
  52.             mhListener.Start();
  53.         }
  54.  
  55.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  56.         {
  57.             mhListener.Dispose();
  58.         }
  59.  
  60.         private void Mhl_MouseMove(object sender, MouseEventArgs e)
  61.         {
  62.             try
  63.             {
  64.  
  65.                 CURSORINFO ci = new CURSORINFO();
  66.                 ci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
  67.                 var res = GetCursorInfo(ref ci);
  68.  
  69.                 var cursor = new Cursor(ci.hCursor);
  70.  
  71.                 label1.Text = $"{res};{cursor.ToString()};{ci.hCursor};[{ci.ptScreenPos.x};{ci.ptScreenPos.y}]";
  72.             }
  73.             catch(Exception ex)
  74.             {
  75.                 label1.Text = ex.Message;
  76.             }
  77.         }
  78.     }
  79. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.533 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы