diff --git a/WindowsInput/IInputDeviceStateAdaptor.cs b/WindowsInput/IInputDeviceStateAdaptor.cs index 59154b5..0360143 100644 --- a/WindowsInput/IInputDeviceStateAdaptor.cs +++ b/WindowsInput/IInputDeviceStateAdaptor.cs @@ -51,5 +51,15 @@ public interface IInputDeviceStateAdaptor /// true if the toggling key is toggled on (in-effect); otherwise, false. /// bool IsTogglingKeyInEffect(VirtualKeyCode keyCode); + + /// + /// Gets the current state of the mouse cursor. + /// + CursorFlag GetCursorFlag(); + + /// + /// Gets the current screen coordinates of the mouse cursor. + /// + Point GetCursorScreenCoordinates(); } } \ No newline at end of file diff --git a/WindowsInput/Native/CURSORINFO.cs b/WindowsInput/Native/CURSORINFO.cs new file mode 100644 index 0000000..8e1672a --- /dev/null +++ b/WindowsInput/Native/CURSORINFO.cs @@ -0,0 +1,32 @@ +using System; + +namespace WindowsInput.Native +{ +#pragma warning disable 649 + /// + /// Contains global cursor information. See: (https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-cursorinfo) + /// + public struct CURSORINFO + { + /// + /// The size of the structure in bytes. The caller must set this to sizeof(CURSORINFO). + /// + public Int32 cbSize; + + /// + /// The cursor state. This parameter can be one of the following values. + /// + public CursorFlag flags; + + /// + /// A handle to the cursor. + /// + public IntPtr hCursor; + + /// + /// The structure that receives the screen coordinates of the cursor. + /// + public Point ptScreenPos; + } +#pragma warning restore 649 +} \ No newline at end of file diff --git a/WindowsInput/Native/CursorFlag.cs b/WindowsInput/Native/CursorFlag.cs new file mode 100644 index 0000000..b7202aa --- /dev/null +++ b/WindowsInput/Native/CursorFlag.cs @@ -0,0 +1,26 @@ +using System; + +namespace WindowsInput.Native +{ + /// + /// The cursor state. CursorFlag is a part of CURSORINFO structure (DWORD flags) (winuser.h). (See: https://docs.microsoft.com/ru-ru/windows/win32/api/winuser/ns-winuser-cursorinfo) + /// + [Flags] + public enum CursorFlag : Int32 + { + /// + /// The cursor is hidden. + /// + Hidden = 0x0000, + + /// + /// The cursor is showing. + /// + Visible = 0x0001, + + /// + /// Windows 8: The cursor is suppressed. This flag indicates that the system is not drawing the cursor because the user is providing input through touch or pen instead of the mouse. + /// + Suppressed = 0x002 + } +} \ No newline at end of file diff --git a/WindowsInput/Native/NativeMethods.cs b/WindowsInput/Native/NativeMethods.cs index a223796..462b3f9 100644 --- a/WindowsInput/Native/NativeMethods.cs +++ b/WindowsInput/Native/NativeMethods.cs @@ -106,5 +106,13 @@ internal static class NativeMethods /// [DllImport("user32.dll")] public static extern UInt32 MapVirtualKey(UInt32 uCode, UInt32 uMapType); + + /// + /// Retrieves information about the global cursor. + /// + /// + /// + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetCursorInfo(ref CURSORINFO cursorInfo); } } \ No newline at end of file diff --git a/WindowsInput/Native/Point.cs b/WindowsInput/Native/Point.cs new file mode 100644 index 0000000..55830bf --- /dev/null +++ b/WindowsInput/Native/Point.cs @@ -0,0 +1,31 @@ +using System.Runtime.InteropServices; + +namespace WindowsInput.Native +{ + /// + /// The structure that receives the screen coordinates of the cursor. See: (https://docs.microsoft.com/en-us/previous-versions/dd162805(v=vs.85) + /// + [StructLayout(LayoutKind.Sequential)] + public readonly struct Point + { + /// + /// Make a point with the specified coordinates. + /// + /// + /// + public Point(int x, int y) + { + X = x; + Y = y; + } + + /// + /// The X coordinate of mouse cursor on virtual desktop. + /// + public readonly int X; + /// + /// The Y coordinate of mouse cursor on virtual desktop. + /// + public readonly int Y; + } +} \ No newline at end of file diff --git a/WindowsInput/WindowsInput.csproj b/WindowsInput/WindowsInput.csproj index 9b27456..7ef3be5 100644 --- a/WindowsInput/WindowsInput.csproj +++ b/WindowsInput/WindowsInput.csproj @@ -57,6 +57,8 @@ + + @@ -71,6 +73,7 @@ + diff --git a/WindowsInput/WindowsInputDeviceStateAdaptor.cs b/WindowsInput/WindowsInputDeviceStateAdaptor.cs index cde9575..d064963 100644 --- a/WindowsInput/WindowsInputDeviceStateAdaptor.cs +++ b/WindowsInput/WindowsInputDeviceStateAdaptor.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using WindowsInput.Native; namespace WindowsInput @@ -153,5 +154,46 @@ public bool IsTogglingKeyInEffect(VirtualKeyCode keyCode) Int16 result = NativeMethods.GetKeyState((UInt16)keyCode); return (result & 0x01) == 0x01; } + + /// + /// The cursor state. CursorFlag is a part of CURSORINFO structure (DWORD flags) (winuser.h). (See: https://docs.microsoft.com/ru-ru/windows/win32/api/winuser/ns-winuser-cursorinfo) + /// + /// + /// Hidden if the cursor is hidden. + /// Visible if the cursor is showing. + /// Suppressed Windows 8: The cursor is suppressed. This flag indicates that the system is not drawing the cursor because the user is providing input through touch or pen instead of the mouse. + /// + public CursorFlag GetCursorFlag() + { + return GetCursorInfo().flags; + } + + /// + /// The structure that receives the screen coordinates of the cursor. + /// + /// + /// Point The X and Y coordinates of cursor position on screen. + /// + public Point GetCursorScreenCoordinates() + { + return GetCursorInfo().ptScreenPos; + } + + /// + /// Contains global cursor information. See: (https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-cursorinfo) + /// + /// + /// cbSize The size of the structure, in bytes. + /// flags The cursor state. This parameter can be one of the following values. + /// hCursor A handle to the cursor. + /// ptScreenPos The structure that receives the screen coordinates of the cursor. + /// + private static CURSORINFO GetCursorInfo() + { + CURSORINFO cursorInfo = new CURSORINFO(); + cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); + NativeMethods.GetCursorInfo(ref cursorInfo); + return cursorInfo; + } } } \ No newline at end of file