// THIS FILE IS PART OF Vanara PROJECT
// THE Vanara PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MIT License.
// COPYRIGHT (C) dahall. ALL RIGHTS RESERVED.
// GITHUB: https://github.com/dahall/Vanara
using System;
using System.Runtime.InteropServices;
namespace Vanara.PInvoke
{
public static partial class User32
{
///
///
/// Confines the cursor to a rectangular area on the screen. If a subsequent cursor position (set by the SetCursorPos function or the
/// mouse) lies outside the rectangle, the system automatically adjusts the position to keep the cursor inside the rectangular area.
///
///
///
/// Type: const RECT*
///
/// A pointer to the structure that contains the screen coordinates of the upper-left and lower-right corners of the confining
/// rectangle. If this parameter is NULL, the cursor is free to move anywhere on the screen.
///
///
///
/// Type: BOOL
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// The cursor is a shared resource. If an application confines the cursor, it must release the cursor by using ClipCursor
/// before relinquishing control to another application.
///
/// The calling process must have WINSTA_WRITEATTRIBUTES access to the window station.
/// Examples
/// For an example, see Confining a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-clipcursor BOOL ClipCursor( CONST RECT *lpRect );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "clipcursor")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ClipCursor(in RECT lpRect);
///
/// Creates a cursor having the specified size, bit patterns, and hot spot.
///
///
/// Type: HINSTANCE
/// A handle to the current instance of the application creating the cursor.
///
///
/// Type: int
/// The horizontal position of the cursor's hot spot.
///
///
/// Type: int
/// The vertical position of the cursor's hot spot.
///
///
/// Type: int
/// The width of the cursor, in pixels.
///
///
/// Type: int
/// The height of the cursor, in pixels.
///
///
/// Type: const VOID*
/// An array of bytes that contains the bit values for the AND mask of the cursor, as in a device-dependent monochrome bitmap.
///
///
/// Type: const VOID*
/// An array of bytes that contains the bit values for the XOR mask of the cursor, as in a device-dependent monochrome bitmap.
///
///
/// Type: HCURSOR
/// If the function succeeds, the return value is a handle to the cursor.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
///
///
/// The nWidth and nHeight parameters must specify a width and height that are supported by the current display driver, because the
/// system cannot create cursors of other sizes. To determine the width and height supported by the display driver, use the
/// GetSystemMetrics function, specifying the SM_CXCURSOR or SM_CYCURSOR value.
///
/// Before closing, an application must call the DestroyCursor function to free any system resources associated with the cursor.
/// DPI Virtualization
///
/// This API does not participate in DPI virtualization. The output returned is in terms of physical coordinates, and is not affected
/// by the DPI of the calling thread. Note that the cursor created may still be scaled to match the DPI of any given window it is
/// drawn into.
///
/// Examples
/// For an example, see Creating a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createcursor HCURSOR CreateCursor( HINSTANCE hInst, int
// xHotSpot, int yHotSpot, int nWidth, int nHeight, CONST VOID *pvANDPlane, CONST VOID *pvXORPlane );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "createcursor")]
public static extern SafeHCURSOR CreateCursor(HINSTANCE hInst, int xHotSpot, int yHotSpot, int nWidth, int nHeight, IntPtr pvANDPlane, IntPtr pvXORPlane);
///
/// Destroys a cursor and frees any memory the cursor occupied. Do not use this function to destroy a shared cursor.
///
///
/// Type: HCURSOR
/// A handle to the cursor to be destroyed. The cursor must not be in use.
///
///
/// Type: BOOL
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// The DestroyCursor function destroys a nonshared cursor. Do not use this function to destroy a shared cursor. A shared
/// cursor is valid as long as the module from which it was loaded remains in memory. The following functions obtain a shared cursor:
///
///
/// -
/// LoadCursor
///
/// -
/// LoadCursorFromFile
///
/// -
/// LoadImage (if you use the LR_SHARED flag)
///
/// -
/// CopyImage (if you use the LR_COPYRETURNORG flag and the hImage parameter is a shared cursor)
///
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-destroycursor BOOL DestroyCursor( HCURSOR hCursor );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "destroycursor")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyCursor(HCURSOR hCursor);
///
/// Retrieves the screen coordinates of the rectangular area to which the cursor is confined.
///
///
/// Type: LPRECT
///
/// A pointer to a RECT structure that receives the screen coordinates of the confining rectangle. The structure receives the
/// dimensions of the screen if the cursor is not confined to a rectangle.
///
///
///
/// Type: BOOL
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// The cursor is a shared resource. If an application confines the cursor with the ClipCursor function, it must later release the
/// cursor by using ClipCursor before relinquishing control to another application.
///
/// The calling process must have WINSTA_READATTRIBUTES access to the window station.
/// Examples
/// For an example, see Confining a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipcursor BOOL GetClipCursor( LPRECT lpRect );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "getclipcursor")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClipCursor(out RECT lpRect);
///
/// Retrieves a handle to the current cursor.
/// To get information on the global cursor, even if it is not owned by the current thread, use GetCursorInfo.
///
///
/// Type: HCURSOR
/// The return value is the handle to the current cursor. If there is no cursor, the return value is NULL.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursor HCURSOR GetCursor( );
[DllImport("user32.dll", SetLastError = false, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "getcursor")]
public static extern SafeHCURSOR GetCursor();
///
/// Retrieves the position of the mouse cursor, in screen coordinates.
///
///
/// Type: LPPOINT
/// A pointer to a POINT structure that receives the screen coordinates of the cursor.
///
///
/// Type: BOOL
/// Returns nonzero if successful or zero otherwise. To get extended error information, call GetLastError.
///
///
///
/// The cursor position is always specified in screen coordinates and is not affected by the mapping mode of the window that contains
/// the cursor.
///
/// The calling process must have WINSTA_READATTRIBUTES access to the window station.
///
/// The input desktop must be the current desktop when you call GetCursorPos. Call OpenInputDesktop to determine whether the
/// current desktop is the input desktop. If it is not, call SetThreadDesktop with the HDESK returned by
/// OpenInputDesktop to switch to that desktop.
///
/// Examples
/// For an example, see Using the Keyboard to Move the Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursorpos BOOL GetCursorPos( LPPOINT lpPoint );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "getcursorpos")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out System.Drawing.Point lpPoint);
///
/// Retrieves the position of the cursor in physical coordinates.
///
///
/// Type: LPPOINT
/// The position of the cursor, in physical coordinates.
///
///
/// Type: BOOL
/// TRUE if successful; otherwise FALSE.
/// GetLastError can be called to get more information about any error that is generated.
///
///
/// For a description of the difference between logicial coordinates and physical coordinates, see PhysicalToLogicalPoint.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getphysicalcursorpos BOOL GetPhysicalCursorPos( LPPOINT
// lpPoint );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "getphysicalcursorpos")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPhysicalCursorPos(out System.Drawing.Point lpPoint);
///
/// Loads the specified cursor resource from the executable (.EXE) file associated with an application instance.
/// Note This function has been superseded by the LoadImage function.
///
///
/// Type: HINSTANCE
/// A handle to an instance of the module whose executable file contains the cursor to be loaded.
///
///
/// Type: LPCTSTR
///
/// The name of the cursor resource to be loaded. Alternatively, this parameter can consist of the resource identifier in the
/// low-order word and zero in the high-order word. The MAKEINTRESOURCE macro can also be used to create this value. To use one of
/// the predefined cursors, the application must set the hInstance parameter to NULL and the lpCursorName parameter to one the
/// following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// IDC_APPSTARTING MAKEINTRESOURCE(32650)
/// Standard arrow and small hourglass
///
/// -
/// IDC_ARROW MAKEINTRESOURCE(32512)
/// Standard arrow
///
/// -
/// IDC_CROSS MAKEINTRESOURCE(32515)
/// Crosshair
///
/// -
/// IDC_HAND MAKEINTRESOURCE(32649)
/// Hand
///
/// -
/// IDC_HELP MAKEINTRESOURCE(32651)
/// Arrow and question mark
///
/// -
/// IDC_IBEAM MAKEINTRESOURCE(32513)
/// I-beam
///
/// -
/// IDC_ICON MAKEINTRESOURCE(32641)
/// Obsolete for applications marked version 4.0 or later.
///
/// -
/// IDC_NO MAKEINTRESOURCE(32648)
/// Slashed circle
///
/// -
/// IDC_SIZE MAKEINTRESOURCE(32640)
/// Obsolete for applications marked version 4.0 or later. Use IDC_SIZEALL.
///
/// -
/// IDC_SIZEALL MAKEINTRESOURCE(32646)
/// Four-pointed arrow pointing north, south, east, and west
///
/// -
/// IDC_SIZENESW MAKEINTRESOURCE(32643)
/// Double-pointed arrow pointing northeast and southwest
///
/// -
/// IDC_SIZENS MAKEINTRESOURCE(32645)
/// Double-pointed arrow pointing north and south
///
/// -
/// IDC_SIZENWSE MAKEINTRESOURCE(32642)
/// Double-pointed arrow pointing northwest and southeast
///
/// -
/// IDC_SIZEWE MAKEINTRESOURCE(32644)
/// Double-pointed arrow pointing west and east
///
/// -
/// IDC_UPARROW MAKEINTRESOURCE(32516)
/// Vertical arrow
///
/// -
/// IDC_WAIT MAKEINTRESOURCE(32514)
/// Hourglass
///
///
///
///
/// Type: HCURSOR
/// If the function succeeds, the return value is the handle to the newly loaded cursor.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
///
///
/// The LoadCursor function loads the cursor resource only if it has not been loaded; otherwise, it retrieves the handle to
/// the existing resource. This function returns a valid cursor handle only if the lpCursorName parameter is a pointer to a cursor
/// resource. If lpCursorName is a pointer to any type of resource other than a cursor (such as an icon), the return value is not
/// NULL, even though it is not a valid cursor handle.
///
///
/// The LoadCursor function searches the cursor resource most appropriate for the cursor for the current display device. The
/// cursor resource can be a color or monochrome bitmap.
///
/// DPI Virtualization
/// This API does not participate in DPI virtualization. The output returned is not affected by the DPI of the calling thread.
/// Examples
/// For an example, see Creating a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadcursora HCURSOR LoadCursorA( HINSTANCE hInstance,
// LPCSTR lpCursorName );
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("winuser.h", MSDNShortId = "loadcursor")]
public static extern SafeHCURSOR LoadCursor(HINSTANCE hInstance, string lpCursorName);
///
/// Loads the specified cursor resource from the executable (.EXE) file associated with an application instance.
/// Note This function has been superseded by the LoadImage function.
///
///
/// Type: HINSTANCE
/// A handle to an instance of the module whose executable file contains the cursor to be loaded.
///
///
/// Type: LPCTSTR
///
/// The name of the cursor resource to be loaded. Alternatively, this parameter can consist of the resource identifier in the
/// low-order word and zero in the high-order word. The MAKEINTRESOURCE macro can also be used to create this value. To use one of
/// the predefined cursors, the application must set the hInstance parameter to NULL and the lpCursorName parameter to one the
/// following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// IDC_APPSTARTING MAKEINTRESOURCE(32650)
/// Standard arrow and small hourglass
///
/// -
/// IDC_ARROW MAKEINTRESOURCE(32512)
/// Standard arrow
///
/// -
/// IDC_CROSS MAKEINTRESOURCE(32515)
/// Crosshair
///
/// -
/// IDC_HAND MAKEINTRESOURCE(32649)
/// Hand
///
/// -
/// IDC_HELP MAKEINTRESOURCE(32651)
/// Arrow and question mark
///
/// -
/// IDC_IBEAM MAKEINTRESOURCE(32513)
/// I-beam
///
/// -
/// IDC_ICON MAKEINTRESOURCE(32641)
/// Obsolete for applications marked version 4.0 or later.
///
/// -
/// IDC_NO MAKEINTRESOURCE(32648)
/// Slashed circle
///
/// -
/// IDC_SIZE MAKEINTRESOURCE(32640)
/// Obsolete for applications marked version 4.0 or later. Use IDC_SIZEALL.
///
/// -
/// IDC_SIZEALL MAKEINTRESOURCE(32646)
/// Four-pointed arrow pointing north, south, east, and west
///
/// -
/// IDC_SIZENESW MAKEINTRESOURCE(32643)
/// Double-pointed arrow pointing northeast and southwest
///
/// -
/// IDC_SIZENS MAKEINTRESOURCE(32645)
/// Double-pointed arrow pointing north and south
///
/// -
/// IDC_SIZENWSE MAKEINTRESOURCE(32642)
/// Double-pointed arrow pointing northwest and southeast
///
/// -
/// IDC_SIZEWE MAKEINTRESOURCE(32644)
/// Double-pointed arrow pointing west and east
///
/// -
/// IDC_UPARROW MAKEINTRESOURCE(32516)
/// Vertical arrow
///
/// -
/// IDC_WAIT MAKEINTRESOURCE(32514)
/// Hourglass
///
///
///
///
/// Type: HCURSOR
/// If the function succeeds, the return value is the handle to the newly loaded cursor.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
///
///
/// The LoadCursor function loads the cursor resource only if it has not been loaded; otherwise, it retrieves the handle to
/// the existing resource. This function returns a valid cursor handle only if the lpCursorName parameter is a pointer to a cursor
/// resource. If lpCursorName is a pointer to any type of resource other than a cursor (such as an icon), the return value is not
/// NULL, even though it is not a valid cursor handle.
///
///
/// The LoadCursor function searches the cursor resource most appropriate for the cursor for the current display device. The
/// cursor resource can be a color or monochrome bitmap.
///
/// DPI Virtualization
/// This API does not participate in DPI virtualization. The output returned is not affected by the DPI of the calling thread.
/// Examples
/// For an example, see Creating a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadcursora HCURSOR LoadCursorA( HINSTANCE hInstance,
// LPCSTR lpCursorName );
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("winuser.h", MSDNShortId = "loadcursor")]
public static extern SafeHCURSOR LoadCursor([Optional] HINSTANCE hInstance, ResourceId lpCursorName);
///
/// Creates a cursor based on data contained in a file.
///
///
/// Type: LPCTSTR
/// The source of the file data to be used to create the cursor. The data in the file must be in either .CUR or .ANI format.
///
/// If the high-order word of lpFileName is nonzero, it is a pointer to a string that is a fully qualified name of a file containing
/// cursor data.
///
///
///
/// Type: HCURSOR
/// If the function is successful, the return value is a handle to the new cursor.
///
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError. GetLastError
/// may return the following value.
///
///
///
/// Return code
/// Description
///
/// -
/// ERROR_FILE_NOT_FOUND
/// The specified file cannot be found.
///
///
///
///
/// DPI Virtualization
/// This API does not participate in DPI virtualization. The output returned is not affected by the DPI of the calling thread.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadcursorfromfilea HCURSOR LoadCursorFromFileA( LPCSTR
// lpFileName );
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("winuser.h", MSDNShortId = "loadcursorfromfile")]
public static extern SafeHCURSOR LoadCursorFromFile(string lpFileName);
///
/// Sets the cursor shape.
///
///
/// Type: HCURSOR
///
/// A handle to the cursor. The cursor must have been created by the CreateCursor function or loaded by the LoadCursor or LoadImage
/// function. If this parameter is NULL, the cursor is removed from the screen.
///
///
///
/// Type: HCURSOR
/// The return value is the handle to the previous cursor, if there was one.
/// If there was no previous cursor, the return value is NULL.
///
///
/// The cursor is set only if the new cursor is different from the previous cursor; otherwise, the function returns immediately.
///
/// The cursor is a shared resource. A window should set the cursor shape only when the cursor is in its client area or when the
/// window is capturing mouse input. In systems without a mouse, the window should restore the previous cursor before the cursor
/// leaves the client area or before it relinquishes control to another window.
///
///
/// If your application must set the cursor while it is in a window, make sure the class cursor for the specified window's class is
/// set to NULL. If the class cursor is not NULL, the system restores the class cursor each time the mouse is moved.
///
///
/// The cursor is not shown on the screen if the internal cursor display count is less than zero. This occurs if the application uses
/// the ShowCursor function to hide the cursor more times than to show the cursor.
///
/// Examples
/// For an example, see Displaying a Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setcursor HCURSOR SetCursor( HCURSOR hCursor );
[DllImport("user32.dll", SetLastError = false, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "setcursor")]
public static extern SafeHCURSOR SetCursor(SafeHCURSOR hCursor);
///
///
/// Moves the cursor to the specified screen coordinates. If the new coordinates are not within the screen rectangle set by the most
/// recent ClipCursor function call, the system automatically adjusts the coordinates so that the cursor stays within the rectangle.
///
///
///
/// Type: int
/// The new x-coordinate of the cursor, in screen coordinates.
///
///
/// Type: int
/// The new y-coordinate of the cursor, in screen coordinates.
///
///
/// Type: BOOL
/// Returns nonzero if successful or zero otherwise. To get extended error information, call GetLastError.
///
///
/// The cursor is a shared resource. A window should move the cursor only when the cursor is in the window's client area.
/// The calling process must have WINSTA_WRITEATTRIBUTES access to the window station.
///
/// The input desktop must be the current desktop when you call SetCursorPos. Call OpenInputDesktop to determine whether the
/// current desktop is the input desktop. If it is not, call SetThreadDesktop with the HDESK returned by
/// OpenInputDesktop to switch to that desktop.
///
/// Examples
/// For an example, see Using the Keyboard to Move the Cursor.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setcursorpos BOOL SetCursorPos( int X, int Y );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "setcursorpos")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
///
/// Sets the position of the cursor in physical coordinates.
///
///
/// Type: int
/// The new x-coordinate of the cursor, in physical coordinates.
///
///
/// Type: int
/// The new y-coordinate of the cursor, in physical coordinates.
///
///
/// Type: BOOL
/// TRUE if successful; otherwise FALSE.
///
///
/// For a description of the difference between logicial coordinates and physical coordinates, see PhysicalToLogicalPoint.
/// GetLastError can be called to get more information about any error that is generated.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setphysicalcursorpos BOOL SetPhysicalCursorPos( int X, int
// Y );
[DllImport("user32.dll", SetLastError = true, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "setphysicalcursorpos")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetPhysicalCursorPos(int X, int Y);
/// Displays or hides the cursor.
///
/// Type: BOOL
///
/// If bShow is TRUE, the display count is incremented by one. If bShow is FALSE, the display count is decremented by one.
///
///
///
/// Type: int
/// The return value specifies the new display counter.
///
///
/// Windows 8: Call GetCursorInfo to determine the cursor visibility.
///
/// This function sets an internal display counter that determines whether the cursor should be displayed. The cursor is displayed
/// only if the display count is greater than or equal to 0. If a mouse is installed, the initial display count is 0. If no mouse is
/// installed, the display count is ?.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-showcursor int ShowCursor( BOOL bShow );
[DllImport("user32.dll", SetLastError = false, ExactSpelling = true)]
[PInvokeData("winuser.h", MSDNShortId = "showcursor")]
public static extern int ShowCursor([MarshalAs(UnmanagedType.Bool)] bool bShow);
/// Provides a to a Windows that disposes a created HCURSOR instance at disposal using DestroyCursor.
public class SafeHCURSOR : SafeHANDLE, IUserHandle
{
/// Initializes a new instance of the class and assigns an existing handle.
/// An object that represents the pre-existing handle to use.
///
/// to reliably release the handle during the finalization phase; otherwise, (not recommended).
///
public SafeHCURSOR(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }
private SafeHCURSOR() : base()
{
}
/// Performs an implicit conversion from to .
/// The safe handle instance.
/// The result of the conversion.
public static implicit operator HCURSOR(SafeHCURSOR h) => h.handle;
///
protected override bool InternalReleaseHandle() => DestroyCursor(this);
}
}
}