Как отловить вид курсора вне формы - C#
Формулировка задачи:
Здравствуйте, собственно вопрос заключается как получить вид курсора вне формы.
Стремлюсь к коду который будет получать определенный вид курсора вне формы и тогда выполнять функцию.
Решение задачи: «Как отловить вид курсора вне формы»
textual
Листинг программы
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
// The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
// 0 The cursor is hidden.
// CURSOR_SHOWING The cursor is showing.
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
/// <summary>Must initialize cbSize</summary>
[DllImport("user32.dll")]
static extern bool GetCursorInfo(ref CURSORINFO pci);
Hooker hooker;
MouseHookListener mhListener;
public Form1()
{
InitializeComponent();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.FormClosing += Form1_FormClosing;
hooker = new GlobalHooker();
mhListener = new MouseHookListener(hooker);
mhListener.MouseMove += Mhl_MouseMove;
mhListener.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
mhListener.Dispose();
}
private void Mhl_MouseMove(object sender, MouseEventArgs e)
{
try
{
CURSORINFO ci = new CURSORINFO();
ci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
var res = GetCursorInfo(ref ci);
var cursor = new Cursor(ci.hCursor);
label1.Text = $"{res};{cursor.ToString()};{ci.hCursor};[{ci.ptScreenPos.x};{ci.ptScreenPos.y}]";
}
catch(Exception ex)
{
label1.Text = ex.Message;
}
}
}
}