using System; using System.Drawing; using System.Runtime.InteropServices; using static Vanara.PInvoke.Gdi32; using static Vanara.PInvoke.Kernel32; namespace Vanara.PInvoke { public static partial class User32 { /// Flags used by . [PInvokeData("winuser.h", MSDNShortId = "drawiconex")] [Flags] public enum DrawIconExFlags { /// Draws the icon or cursor using the mask. DI_MASK = 0x0001, /// Draws the icon or cursor using the image. DI_IMAGE = 0x0002, /// Combination of DI_IMAGE and DI_MASK. DI_NORMAL = 0x0003, /// This flag is ignored. DI_COMPAT = 0x0004, /// /// Draws the icon or cursor using the width and height specified by the system metric values for icons, if the cxWidth and /// cyWidth parameters are set to zero. If this flag is not specified and cxWidth and cyWidth are set to zero, the function uses /// the actual resource size. /// DI_DEFAULTSIZE = 0x0008, /// Draws the icon as an unmirrored icon. By default, the icon is drawn as a mirrored icon if hdc is mirrored. DI_NOMIRROR = 0x0010, } /// /// Copies the specified icon from another module to the current module. /// /// /// Type: HICON /// A handle to the icon to be copied. /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to the duplicate icon. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// The CopyIcon function enables an application or DLL to get its own handle to an icon owned by another module. If the other /// module is freed, the application icon will still be able to use the icon. /// /// Before closing, an application must call the DestroyIcon function to free any system resources associated with the icon. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-copyicon HICON CopyIcon( HICON hIcon ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "copyicon")] public static extern SafeHICON CopyIcon(HICON hIcon); /// /// Creates a new cursor and copies the attributes of the specified image to the new one. If necessary, the function stretches the /// bits to fit the desired size of the new image. /// /// /// A handle to the image to be copied. /// /// /// The desired size, in pixels, of the image. If this is Size.Empty, then the returned image will have the same size as the original hImage. /// /// /// This parameter can be one or more of the following values. /// /// /// Value /// Meaning /// /// /// LR_COPYDELETEORG 0x00000008 /// Deletes the original image after creating the copy. /// /// /// LR_COPYFROMRESOURCE 0x00004000 /// /// Tries to reload an icon or cursor resource from the original resource file rather than simply copying the current image. This is /// useful for creating a different-sized copy when the resource file contains multiple sizes of the resource. Without this flag, /// CopyImage stretches the original image to the new size. If this flag is set, CopyImage uses the size in the resource file closest /// to the desired size. This will succeed only if hImage was loaded by LoadIcon or LoadCursor, or by LoadImage with the LR_SHARED flag. /// /// /// /// LR_COPYRETURNORG 0x00000004 /// /// Returns the original hImage if it satisfies the criteria for the copy—that is, correct dimensions and color depth—in which case /// the LR_COPYDELETEORG flag is ignored. If this flag is not specified, a new object is always created. /// /// /// /// LR_CREATEDIBSECTION 0x00002000 /// /// If this is set and a new bitmap is created, the bitmap is created as a DIB section. Otherwise, the bitmap image is created as a /// device-dependent bitmap. This flag is only valid if uType is IMAGE_BITMAP. /// /// /// /// LR_DEFAULTSIZE 0x00000040 /// /// Uses the width or height specified by the system metric values for cursors or icons, if the cxDesired or cyDesired values are set /// to zero. If this flag is not specified and cxDesired and cyDesired are set to zero, the function uses the actual resource size. /// If the resource contains multiple images, the function uses the size of the first image. /// /// /// /// LR_MONOCHROME 0x00000001 /// Creates a new monochrome image. /// /// /// /// /// The return value is a safe handle to the newly created image. /// public static SafeHICON CopyIcon(HICON h, Size desiredSize = default, CopyImageOptions options = 0) { var hret = CopyImage(h.DangerousGetHandle(), LoadImageType.IMAGE_ICON, desiredSize.Width, desiredSize.Height, options); if (hret == HANDLE.NULL) Win32Error.ThrowLastError(); return new SafeHICON(hret.DangerousGetHandle(), true); } /// /// Creates an icon that has the specified size, colors, and bit patterns. /// /// /// Type: HINSTANCE /// A handle to the instance of the module creating the icon. /// /// /// Type: int /// The width, in pixels, of the icon. /// /// /// Type: int /// The height, in pixels, of the icon. /// /// /// Type: BYTE /// The number of planes in the XOR bitmask of the icon. /// /// /// Type: BYTE /// The number of bits-per-pixel in the XOR bitmask of the icon. /// /// /// Type: const BYTE* /// An array of bytes that contains the bit values for the AND bitmask of the icon. This bitmask describes a monochrome bitmap. /// /// /// Type: const BYTE* /// /// An array of bytes that contains the bit values for the XOR bitmask of the icon. This bitmask describes a monochrome or /// device-dependent color bitmap. /// /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to an icon. /// 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 supported by the current display driver, because the system /// cannot create icons of other sizes. To determine the width and height supported by the display driver, use the GetSystemMetrics /// function, specifying the SM_CXICON or SM_CYICON value. /// /// CreateIcon applies the following truth table to the AND and XOR bitmasks. /// /// /// AND bitmask /// XOR bitmask /// Display /// /// /// 0 /// 0 /// Black /// /// /// 0 /// 1 /// White /// /// /// 1 /// 0 /// Screen /// /// /// 1 /// 1 /// Reverse screen /// /// /// When you are finished using the icon, destroy it using the DestroyIcon function. /// Examples /// For an example, see Creating an Icon. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createicon HICON CreateIcon( HINSTANCE hInstance, int // nWidth, int nHeight, BYTE cPlanes, BYTE cBitsPixel, CONST BYTE *lpbANDbits, CONST BYTE *lpbXORbits ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "createicon")] public static extern SafeHICON CreateIcon(HINSTANCE hInstance, int nWidth, int nHeight, byte cPlanes, byte cBitsPixel, [In] byte[] lpbANDbits, [In] byte[] lpbXORbits); /// /// Creates an icon or cursor from resource bits describing the icon. /// To specify a desired height or width, use the CreateIconFromResourceEx function. /// /// /// Type: PBYTE /// /// The buffer containing the icon or cursor resource bits. These bits are typically loaded by calls to the /// LookupIconIdFromDirectory, LookupIconIdFromDirectoryEx, and LoadResource functions. /// /// /// /// Type: DWORD /// The size, in bytes, of the set of bits pointed to by the presbits parameter. /// /// /// Type: BOOL /// /// Indicates whether an icon or a cursor is to be created. If this parameter is TRUE, an icon is to be created. If it is /// FALSE, a cursor is to be created. /// /// /// /// Type: DWORD /// /// The version number of the icon or cursor format for the resource bits pointed to by the presbits parameter. The value must be /// greater than or equal to 0x00020000 and less than or equal to 0x00030000. This parameter is generally set to 0x00030000. /// /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to the icon or cursor. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// The CreateIconFromResource, CreateIconFromResourceEx, CreateIconIndirect, GetIconInfo, LookupIconIdFromDirectory, and /// LookupIconIdFromDirectoryEx functions allow shell applications and icon browsers to examine and use resources throughout the system. /// /// The CreateIconFromResource function calls CreateIconFromResourceEx passing as flags. /// When you are finished using the icon, destroy it using the DestroyIcon function. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createiconfromresource HICON CreateIconFromResource( PBYTE // presbits, DWORD dwResSize, BOOL fIcon, DWORD dwVer ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "createiconfromresource")] public static extern SafeHICON CreateIconFromResource([In] byte[] presbits, uint dwResSize, [MarshalAs(UnmanagedType.Bool)] bool fIcon, uint dwVer); /// /// Creates an icon or cursor from resource bits describing the icon. /// /// /// Type: PBYTE /// /// The icon or cursor resource bits. These bits are typically loaded by calls to the LookupIconIdFromDirectoryEx and LoadResource functions. /// /// /// /// Type: DWORD /// The size, in bytes, of the set of bits pointed to by the pbIconBits parameter. /// /// /// Type: BOOL /// /// Indicates whether an icon or a cursor is to be created. If this parameter is TRUE, an icon is to be created. If it is /// FALSE, a cursor is to be created. /// /// /// /// Type: DWORD /// /// The version number of the icon or cursor format for the resource bits pointed to by the pbIconBits parameter. The value must be /// greater than or equal to 0x00020000 and less than or equal to 0x00030000. This parameter is generally set to 0x00030000. /// /// /// /// Type: int /// /// The desired width, in pixels, of the icon or cursor. If this parameter is zero, the function uses the SM_CXICON or /// SM_CXCURSOR system metric value to set the width. /// /// /// /// Type: int /// /// The desired height, in pixels, of the icon or cursor. If this parameter is zero, the function uses the SM_CYICON or /// SM_CYCURSOR system metric value to set the height. /// /// /// /// Type: UINT /// A combination of the following values. /// /// /// Value /// Meaning /// /// /// LR_DEFAULTCOLOR 0x00000000 /// Uses the default color format. /// /// /// LR_DEFAULTSIZE 0x00000040 /// /// Uses the width or height specified by the system metric values for cursors or icons, if the cxDesired or cyDesired values are set /// to zero. If this flag is not specified and cxDesired and cyDesired are set to zero, the function uses the actual resource size. /// If the resource contains multiple images, the function uses the size of the first image. /// /// /// /// LR_MONOCHROME 0x00000001 /// Creates a monochrome icon or cursor. /// /// /// LR_SHARED 0x00008000 /// /// Shares the icon or cursor handle if the icon or cursor is created multiple times. If LR_SHARED is not set, a second call to /// CreateIconFromResourceEx for the same resource will create the icon or cursor again and return a different handle. When you use /// this flag, the system will destroy the resource when it is no longer needed. Do not use LR_SHARED for icons or cursors that have /// non-standard sizes, that may change after loading, or that are loaded from a file. When loading a system icon or cursor, you must /// use LR_SHARED or the function will fail to load the resource. /// /// /// /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to the icon or cursor. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// The CreateIconFromResource, CreateIconFromResourceEx, CreateIconIndirect, GetIconInfo, and LookupIconIdFromDirectoryEx /// functions allow shell applications and icon browsers to examine and use resources throughout the system. /// /// You should call DestroyIcon for icons created with CreateIconFromResourceEx. /// Examples /// For an example, see Sharing Icon Resources. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createiconfromresourceex HICON CreateIconFromResourceEx( // PBYTE presbits, DWORD dwResSize, BOOL fIcon, DWORD dwVer, int cxDesired, int cyDesired, UINT Flags ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "createiconfromresourceex")] public static extern SafeHICON CreateIconFromResourceEx([In] byte[] presbits, uint dwResSize, [MarshalAs(UnmanagedType.Bool)] bool fIcon, uint dwVer, int cxDesired, int cyDesired, LoadImageOptions Flags); /// /// Creates an icon or cursor from an ICONINFO structure. /// /// /// Type: PICONINFO /// A pointer to an ICONINFO structure the function uses to create the icon or cursor. /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to the icon or cursor that is created. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// The system copies the bitmaps in the ICONINFO structure before creating the icon or cursor. Because the system may temporarily /// select the bitmaps in a device context, the hbmMask and hbmColor members of the ICONINFO structure should /// not already be selected into a device context. The application must continue to manage the original bitmaps and delete them when /// they are no longer necessary. /// /// When you are finished using the icon, destroy it using the DestroyIcon function. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createiconindirect HICON CreateIconIndirect( PICONINFO // piconinfo ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "createiconindirect")] public static extern SafeHICON CreateIconIndirect([In] ICONINFO piconinfo); /// /// Destroys an icon and frees any memory the icon occupied. /// /// /// Type: HICON /// A handle to the icon to be destroyed. The icon 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. /// /// /// /// It is only necessary to call DestroyIcon for icons and cursors created with the following functions: /// CreateIconFromResourceEx (if called without the LR_SHARED flag), CreateIconIndirect, and CopyIcon. Do not use this /// function to destroy a shared icon. A shared icon is valid as long as the module from which it was loaded remains in memory. The /// following functions obtain a shared icon. /// /// /// /// LoadIcon /// /// /// LoadImage (if you use the LR_SHARED flag) /// /// /// CopyImage (if you use the LR_COPYRETURNORG flag and the hImage parameter is a shared icon) /// /// /// CreateIconFromResource /// /// /// CreateIconFromResourceEx (if you use the LR_SHARED flag) /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-destroyicon BOOL DestroyIcon( HICON hIcon ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "destroyicon")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DestroyIcon(HICON hIcon); /// /// Draws an icon or cursor into the specified device context. /// To specify additional drawing options, use the DrawIconEx function. /// /// /// Type: HDC /// A handle to the device context into which the icon or cursor will be drawn. /// /// /// Type: int /// The logical x-coordinate of the upper-left corner of the icon. /// /// /// Type: int /// The logical y-coordinate of the upper-left corner of the icon. /// /// /// Type: HICON /// A handle to the icon to be drawn. /// /// /// 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. /// /// /// /// DrawIcon places the icon's upper-left corner at the location specified by the X and Y parameters. The location is subject /// to the current mapping mode of the device context. /// /// /// DrawIcon draws the icon or cursor using the width and height specified by the system metric values for icons; for more /// information, see GetSystemMetrics. /// /// Examples /// For an example, see Displaying an Icon. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-drawicon BOOL DrawIcon( HDC hDC, int X, int Y, HICON hIcon ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "drawicon")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DrawIcon(HDC hDC, int X, int Y, HICON hIcon); /// /// /// Draws an icon or cursor into the specified device context, performing the specified raster operations, and stretching or /// compressing the icon or cursor as specified. /// /// /// /// Type: HDC /// A handle to the device context into which the icon or cursor will be drawn. /// /// /// Type: int /// The logical x-coordinate of the upper-left corner of the icon or cursor. /// /// /// Type: int /// The logical y-coordinate of the upper-left corner of the icon or cursor. /// /// /// Type: HICON /// A handle to the icon or cursor to be drawn. This parameter can identify an animated cursor. /// /// /// Type: int /// /// The logical width of the icon or cursor. If this parameter is zero and the diFlags parameter is DI_DEFAULTSIZE, the /// function uses the SM_CXICON system metric value to set the width. If this parameter is zero and DI_DEFAULTSIZE is /// not used, the function uses the actual resource width. /// /// /// /// Type: int /// /// The logical height of the icon or cursor. If this parameter is zero and the diFlags parameter is DI_DEFAULTSIZE, the /// function uses the SM_CYICON system metric value to set the width. If this parameter is zero and DI_DEFAULTSIZE is /// not used, the function uses the actual resource height. /// /// /// /// Type: UINT /// /// The index of the frame to draw, if hIcon identifies an animated cursor. This parameter is ignored if hIcon does not identify an /// animated cursor. /// /// /// /// Type: HBRUSH /// /// A handle to a brush that the system uses for flicker-free drawing. If hbrFlickerFreeDraw is a valid brush handle, the system /// creates an offscreen bitmap using the specified brush for the background color, draws the icon or cursor into the bitmap, and /// then copies the bitmap into the device context identified by hdc. If hbrFlickerFreeDraw is NULL, the system draws the icon /// or cursor directly into the device context. /// /// /// /// Type: UINT /// The drawing flags. This parameter can be one of the following values. /// /// /// Value /// Meaning /// /// /// DI_COMPAT 0x0004 /// This flag is ignored. /// /// /// DI_DEFAULTSIZE 0x0008 /// /// Draws the icon or cursor using the width and height specified by the system metric values for icons, if the cxWidth and cyWidth /// parameters are set to zero. If this flag is not specified and cxWidth and cyWidth are set to zero, the function uses the actual /// resource size. /// /// /// /// DI_IMAGE 0x0002 /// Draws the icon or cursor using the image. /// /// /// DI_MASK 0x0001 /// Draws the icon or cursor using the mask. /// /// /// DI_NOMIRROR 0x0010 /// Draws the icon as an unmirrored icon. By default, the icon is drawn as a mirrored icon if hdc is mirrored. /// /// /// DI_NORMAL 0x0003 /// Combination of DI_IMAGE and DI_MASK. /// /// /// /// /// 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 DrawIconEx function places the icon's upper-left corner at the location specified by the xLeft and yTop parameters. /// The location is subject to the current mapping mode of the device context. /// /// To duplicate , call DrawIconEx as follows: /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-drawiconex BOOL DrawIconEx( HDC hdc, int xLeft, int yTop, // HICON hIcon, int cxWidth, int cyWidth, UINT istepIfAniCur, HBRUSH hbrFlickerFreeDraw, UINT diFlags ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "drawiconex")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DrawIconEx(HDC hdc, int xLeft, int yTop, HICON hIcon, int cxWidth, int cyWidth, uint istepIfAniCur, HBRUSH hbrFlickerFreeDraw, DrawIconExFlags diFlags); /// /// Retrieves information about the specified icon or cursor. /// /// /// Type: HICON /// /// A handle to the icon or cursor. To retrieve information about a standard icon or cursor, specify one of the following values. /// /// /// /// Value /// Meaning /// /// /// IDC_APPSTARTING MAKEINTRESOURCE(32650) /// Standard arrow and small hourglass cursor. /// /// /// IDC_ARROW MAKEINTRESOURCE(32512) /// Standard arrow cursor. /// /// /// IDC_CROSS MAKEINTRESOURCE(32515) /// Crosshair cursor. /// /// /// IDC_HAND MAKEINTRESOURCE(32649) /// Hand cursor. /// /// /// IDC_HELP MAKEINTRESOURCE(32651) /// Arrow and question mark cursor. /// /// /// IDC_IBEAM MAKEINTRESOURCE(32513) /// I-beam cursor. /// /// /// IDC_NO MAKEINTRESOURCE(32648) /// Slashed circle cursor. /// /// /// IDC_SIZEALL MAKEINTRESOURCE(32646) /// Four-pointed arrow cursor pointing north, south, east, and west. /// /// /// IDC_SIZENESW MAKEINTRESOURCE(32643) /// Double-pointed arrow cursor pointing northeast and southwest. /// /// /// IDC_SIZENS MAKEINTRESOURCE(32645) /// Double-pointed arrow cursor pointing north and south. /// /// /// IDC_SIZENWSE MAKEINTRESOURCE(32642) /// Double-pointed arrow cursor pointing northwest and southeast. /// /// /// IDC_SIZEWE MAKEINTRESOURCE(32644) /// Double-pointed arrow cursor pointing west and east. /// /// /// IDC_UPARROW MAKEINTRESOURCE(32516) /// Vertical arrow cursor. /// /// /// IDC_WAIT MAKEINTRESOURCE(32514) /// Hourglass cursor. /// /// /// IDI_APPLICATION MAKEINTRESOURCE(32512) /// Application icon. /// /// /// IDI_ASTERISK MAKEINTRESOURCE(32516) /// Asterisk icon. /// /// /// IDI_EXCLAMATION MAKEINTRESOURCE(32515) /// Exclamation point icon. /// /// /// IDI_HAND MAKEINTRESOURCE(32513) /// Stop sign icon. /// /// /// IDI_QUESTION MAKEINTRESOURCE(32514) /// Question-mark icon. /// /// /// IDI_WINLOGO MAKEINTRESOURCE(32517) /// Application icon. Windows 2000: Windows logo icon. /// /// /// /// /// Type: PICONINFO /// A pointer to an ICONINFO structure. The function fills in the structure's members. /// /// /// Type: BOOL /// /// If the function succeeds, the return value is nonzero and the function fills in the members of the specified ICONINFO structure. /// /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// GetIconInfo creates bitmaps for the hbmMask and hbmCol or members of ICONINFO. The calling application must /// manage these bitmaps and delete them when they are no longer necessary. /// /// 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-geticoninfo BOOL GetIconInfo( HICON hIcon, PICONINFO // piconinfo ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "geticoninfo")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetIconInfo(HICON hIcon, [In, Out] ICONINFO piconinfo); /// /// /// Retrieves information about the specified icon or cursor. GetIconInfoEx extends GetIconInfo by using the newer ICONINFOEX structure. /// /// /// /// Type: HICON /// /// A handle to the icon or cursor. To retrieve information about a standard icon or cursor, specify one of the following values. /// /// /// /// Value /// Meaning /// /// /// IDC_APPSTARTING MAKEINTRESOURCE(32650) /// Standard arrow and small hourglass cursor. /// /// /// IDC_ARROW MAKEINTRESOURCE(32512) /// Standard arrow cursor. /// /// /// IDC_CROSS MAKEINTRESOURCE(32515) /// Crosshair cursor. /// /// /// IDC_HAND MAKEINTRESOURCE(32649) /// Hand cursor. /// /// /// IDC_HELP MAKEINTRESOURCE(32651) /// Arrow and question mark cursor. /// /// /// IDC_IBEAM MAKEINTRESOURCE(32513) /// I-beam cursor. /// /// /// IDC_NO MAKEINTRESOURCE(32648) /// Slashed circle cursor. /// /// /// IDC_SIZEALL MAKEINTRESOURCE(32646) /// Four-pointed arrow cursor pointing north, south, east, and west. /// /// /// IDC_SIZENESW MAKEINTRESOURCE(32643) /// Double-pointed arrow cursor pointing northeast and southwest. /// /// /// IDC_SIZENS MAKEINTRESOURCE(32645) /// Double-pointed arrow cursor pointing north and south. /// /// /// IDC_SIZENWSE MAKEINTRESOURCE(32642) /// Double-pointed arrow cursor pointing northwest and southeast. /// /// /// IDC_SIZEWE MAKEINTRESOURCE(32644) /// Double-pointed arrow cursor pointing west and east. /// /// /// IDC_UPARROW MAKEINTRESOURCE(32516) /// Vertical arrow cursor. /// /// /// IDC_WAIT MAKEINTRESOURCE(32514) /// Hourglass cursor. /// /// /// IDI_APPLICATION MAKEINTRESOURCE(32512) /// Application icon. /// /// /// IDI_ASTERISK MAKEINTRESOURCE(32516) /// Asterisk icon. /// /// /// IDI_EXCLAMATION MAKEINTRESOURCE(32515) /// Exclamation point icon. /// /// /// IDI_HAND MAKEINTRESOURCE(32513) /// Stop sign icon. /// /// /// IDI_QUESTION MAKEINTRESOURCE(32514) /// Question-mark icon. /// /// /// IDI_WINLOGO MAKEINTRESOURCE(32517) /// Application icon. Windows 2000: Windows logo icon. /// /// /// /// /// Type: PICONINFOEX /// When this method returns, contains a pointer to an ICONINFOEX structure. The function fills in the structure's members. /// /// /// Type: BOOL /// TRUE indicates success, FALSE indicates failure. /// /// /// /// GetIconInfoEx creates bitmaps for the hbmMask and hbmCol or members of ICONINFOEX. The calling application /// must manage these bitmaps and delete them when they are no longer necessary. /// /// 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-geticoninfoexa BOOL GetIconInfoExA( HICON hicon, // PICONINFOEXA piconinfo ); [DllImport(Lib.User32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("winuser.h", MSDNShortId = "geticoninfoex")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetIconInfoEx(HICON hicon, ref ICONINFOEX piconinfo); /// /// Loads the specified icon 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 icon to be loaded. This parameter must be NULL /// when a standard icon is being loaded. /// /// /// /// Type: LPCTSTR /// /// The name of the icon resource to be loaded. Alternatively, this parameter can contain the resource identifier in the low-order /// word and zero in the high-order word. Use the MAKEINTRESOURCE macro to create this value. /// /// /// To use one of the predefined icons, set the hInstance parameter to NULL and the lpIconName parameter to one of the /// following values. /// /// /// /// Value /// Meaning /// /// /// IDI_APPLICATION MAKEINTRESOURCE(32512) /// Default application icon. /// /// /// IDI_ASTERISK MAKEINTRESOURCE(32516) /// Asterisk icon. Same as IDI_INFORMATION. /// /// /// IDI_ERROR MAKEINTRESOURCE(32513) /// Hand-shaped icon. /// /// /// IDI_EXCLAMATION MAKEINTRESOURCE(32515) /// Exclamation point icon. Same as IDI_WARNING. /// /// /// IDI_HAND MAKEINTRESOURCE(32513) /// Hand-shaped icon. Same as IDI_ERROR. /// /// /// IDI_INFORMATION MAKEINTRESOURCE(32516) /// Asterisk icon. /// /// /// IDI_QUESTION MAKEINTRESOURCE(32514) /// Question mark icon. /// /// /// IDI_SHIELD MAKEINTRESOURCE(32518) /// Security Shield icon. /// /// /// IDI_WARNING MAKEINTRESOURCE(32515) /// Exclamation point icon. /// /// /// IDI_WINLOGO MAKEINTRESOURCE(32517) /// Default application icon. Windows 2000: Windows logo icon. /// /// /// /// /// Type: HICON /// If the function succeeds, the return value is a handle to the newly loaded icon. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// LoadIcon loads the icon resource only if it has not been loaded; otherwise, it retrieves a handle to the existing /// resource. The function searches the icon resource for the icon most appropriate for the current display. The icon resource can be /// a color or monochrome bitmap. /// /// /// LoadIcon can only load an icon whose size conforms to the SM_CXICON and SM_CYICON system metric values. Use /// the LoadImage function to load icons of other sizes. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadicona HICON LoadIconA( HINSTANCE hInstance, LPCSTR // lpIconName ); [DllImport(Lib.User32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("winuser.h", MSDNShortId = "loadicon")] public static extern SafeHICON LoadIcon(HINSTANCE hInstance, string lpIconName); /// /// Searches through icon or cursor data for the icon or cursor that best fits the current display device. /// To specify a desired height or width, use the LookupIconIdFromDirectoryEx function. /// /// /// Type: PBYTE /// /// The icon or cursor directory data. Because this function does not validate the resource data, it causes a general protection (GP) /// fault or returns an undefined value if presbits is not pointing to valid resource data. /// /// /// /// Type: BOOL /// /// Indicates whether an icon or a cursor is sought. If this parameter is TRUE, the function is searching for an icon; if the /// parameter is FALSE, the function is searching for a cursor. /// /// /// /// Type: int /// /// If the function succeeds, the return value is an integer resource identifier for the icon or cursor that best fits the current /// display device. /// /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// A resource file of type RT_GROUP_ICON ( RT_GROUP_CURSOR indicates cursors) contains icon (or cursor) data in /// several device-dependent and device-independent formats. LookupIconIdFromDirectory searches the resource file for the icon /// (or cursor) that best fits the current display device and returns its integer identifier. The FindResource and FindResourceEx /// functions use the MAKEINTRESOURCE macro with this identifier to locate the resource in the module. /// /// /// The icon directory is loaded from a resource file with resource type RT_GROUP_ICON (or RT_GROUP_CURSOR for /// cursors), and an integer resource name for the specific icon to be loaded. LookupIconIdFromDirectory returns an integer /// identifier that is the resource name of the icon that best fits the current display device. /// /// /// The LoadIcon, LoadCursor, and LoadImage functions use this function to search the specified resource data for the icon or cursor /// that best fits the current display device. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-lookupiconidfromdirectory int LookupIconIdFromDirectory( // PBYTE presbits, BOOL fIcon ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "lookupiconidfromdirectory")] public static extern int LookupIconIdFromDirectory([In] byte[] presbits, [MarshalAs(UnmanagedType.Bool)] bool fIcon); /// /// Searches through icon or cursor data for the icon or cursor that best fits the current display device. /// /// /// Type: PBYTE /// /// The icon or cursor directory data. Because this function does not validate the resource data, it causes a general protection (GP) /// fault or returns an undefined value if presbits is not pointing to valid resource data. /// /// /// /// Type: BOOL /// /// Indicates whether an icon or a cursor is sought. If this parameter is TRUE, the function is searching for an icon; if the /// parameter is FALSE, the function is searching for a cursor. /// /// /// /// Type: int /// /// The desired width, in pixels, of the icon. If this parameter is zero, the function uses the SM_CXICON or /// SM_CXCURSOR system metric value. /// /// /// /// Type: int /// /// The desired height, in pixels, of the icon. If this parameter is zero, the function uses the SM_CYICON or /// SM_CYCURSOR system metric value. /// /// /// /// Type: UINT /// A combination of the following values. /// /// /// Value /// Meaning /// /// /// LR_DEFAULTCOLOR 0x00000000 /// Uses the default color format. /// /// /// LR_MONOCHROME 0x00000001 /// Creates a monochrome icon or cursor. /// /// /// /// /// Type: int /// /// If the function succeeds, the return value is an integer resource identifier for the icon or cursor that best fits the current /// display device. /// /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// A resource file of type RT_GROUP_ICON ( RT_GROUP_CURSOR indicates cursors) contains icon (or cursor) data in /// several device-dependent and device-independent formats. LookupIconIdFromDirectoryEx searches the resource file for the /// icon (or cursor) that best fits the current display device and returns its integer identifier. The FindResource and /// FindResourceEx functions use the MAKEINTRESOURCE macro with this identifier to locate the resource in the module. /// /// /// The icon directory is loaded from a resource file with resource type RT_GROUP_ICON (or RT_GROUP_CURSOR for /// cursors), and an integer resource name for the specific icon to be loaded. LookupIconIdFromDirectoryEx returns an integer /// identifier that is the resource name of the icon that best fits the current display device. /// /// /// The LoadIcon, LoadImage, and LoadCursor functions use this function to search the specified resource data for the icon or cursor /// that best fits the current display device. /// /// Examples /// For an example, see Sharing Icon Resources. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-lookupiconidfromdirectoryex int // LookupIconIdFromDirectoryEx( PBYTE presbits, BOOL fIcon, int cxDesired, int cyDesired, UINT Flags ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "lookupiconidfromdirectoryex")] public static extern int LookupIconIdFromDirectoryEx([In] byte[] presbits, [MarshalAs(UnmanagedType.Bool)] bool fIcon, int cxDesired, int cyDesired, LoadImageOptions Flags); /// /// [This function is not intended for general use. It may be altered or unavailable in subsequent versions of Windows.] /// Creates an array of handles to icons that are extracted from a specified file. /// /// /// Type: LPCTSTR /// The path and name of the file from which the icon(s) are to be extracted. /// /// /// Type: int /// /// The zero-based index of the first icon to extract. For example, if this value is zero, the function extracts the first icon in /// the specified file. /// /// /// /// Type: int /// The horizontal icon size wanted. See Remarks. /// /// /// Type: int /// The vertical icon size wanted. See Remarks. /// /// /// Type: HICON* /// A pointer to the returned array of icon handles. /// /// /// Type: UINT* /// /// A pointer to a returned resource identifier for the icon that best fits the current display device. The returned identifier is /// 0xFFFFFFFF if the identifier is not available for this format. The returned identifier is 0 if the identifier cannot otherwise be obtained. /// /// /// /// Type: UINT /// The number of icons to extract from the file. This parameter is only valid when extracting from .exe and .dll files. /// /// /// Type: UINT /// Specifies flags that control this function. These flags are the LR_* flags used by the LoadImage function. /// /// /// Type: UINT /// /// If the phiconparameter is NULL and this function succeeds, then the return value is the number of icons in the file. If /// the function fails then the return value is 0. /// /// /// If the phicon parameter is not NULL and the function succeeds, then the return value is the number of icons extracted. /// Otherwise, the return value is 0xFFFFFFFF if the file is not found. /// /// /// /// /// This function extracts from executable (.exe), DLL (.dll), icon (.ico), cursor (.cur), animated cursor (.ani), and bitmap (.bmp) /// files. Extractions from Windows 3.x 16-bit executables (.exe or .dll) are also supported. /// /// /// The cxIcon and cyIcon parameters specify the size of the icons to extract. Two sizes can be extracted by putting the first size /// in the LOWORD of the parameter and the second size in the HIWORD. For example, for both the cxIcon and cyIcon parameters would /// extract both 24 and 48 size icons. /// /// You must destroy all icons extracted by PrivateExtractIcons by calling the DestroyIcon function. /// /// This function was not included in the SDK headers and libraries until Windows XP Service Pack 1 (SP1) and Windows Server 2003. If /// you do not have a header file and import library for this function, you can call the function using LoadLibrary and GetProcAddress. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-privateextracticonsa UINT PrivateExtractIconsA( LPCSTR // szFileName, int nIconIndex, int cxIcon, int cyIcon, HICON *phicon, UINT *piconid, UINT nIcons, UINT flags ); [DllImport(Lib.User32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("winuser.h", MSDNShortId = "privateextracticons")] public static extern uint PrivateExtractIcons(string szFileName, int nIconIndex, int cxIcon, int cyIcon, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 6)] SafeHICON[] phicon, out uint piconid, uint nIcons, LoadImageOptions flags); /// Creates a managed from this HICON instance. /// A managed bitmap instance. public static Bitmap ToBitmap(this HICON hIcon) => hIcon.IsNull ? null : (Bitmap)Bitmap.FromHicon((IntPtr)hIcon).Clone(); /// Creates a managed from this HICON instance. /// A managed icon instance. public static Icon ToIcon(this HICON hIcon) => hIcon.IsNull ? null : (Icon)Icon.FromHandle((IntPtr)hIcon).Clone(); /// /// Contains information about an icon or a cursor. Extends ICONINFO. Used by GetIconInfoEx. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-_iconinfoexa typedef struct _ICONINFOEXA { DWORD cbSize; // BOOL fIcon; DWORD xHotspot; DWORD yHotspot; HBITMAP hbmMask; HBITMAP hbmColor; WORD wResID; CHAR szModName[MAX_PATH]; CHAR // szResName[MAX_PATH]; } ICONINFOEXA, *PICONINFOEXA; [PInvokeData("winuser.h", MSDNShortId = "iconinfoex")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct ICONINFOEX { /// /// Type: DWORD /// The size, in bytes, of this structure. /// public uint cbSize; [MarshalAs(UnmanagedType.Bool)] public bool fIcon; /// /// Type: DWORD /// /// The x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the icon, /// and this member is ignored. /// /// public uint xHotspot; /// /// Type: DWORD /// /// The y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot spot is always in the center of the /// icon, and this member is ignored. /// /// public uint yHotspot; /// /// Type: HBITMAP /// /// The icon bitmask bitmap. If this structure defines a black and white icon, this bitmask is formatted so that the upper half /// is the icon AND bitmask and the lower half is the icon XOR bitmask. Under this condition, the height should be an even /// multiple of two. If this structure defines a color icon, this mask only defines the AND bitmask of the icon. /// /// public HBITMAP hbmMask; /// /// Type: HBITMAP /// /// A handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND /// bitmask of hbmMask is applied with the SRCAND flag to the destination; subsequently, the color bitmap is /// applied (using XOR) to the destination by using the SRCINVERT flag. /// /// public HBITMAP hbmColor; /// /// Type: WORD /// /// The icon or cursor resource bits. These bits are typically loaded by calls to the LookupIconIdFromDirectoryEx and /// LoadResource functions. /// /// public ushort wResID; /// /// Type: TCHAR[MAX_PATH] /// The fully qualified path of the module. /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] public string szModName; /// /// Type: TCHAR[MAX_PATH] /// The fully qualified path of the resource. /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] public string szResName; } /// Provides a to a Windows that disposes a created HICON instance at disposal using DestroyIcon. public class SafeHICON : 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 SafeHICON(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } private SafeHICON() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator HICON(SafeHICON h) => h.handle; /// Creates a managed . /// A managed bitmap instance. public Bitmap ToBitmap() => ((HICON)this).ToBitmap(); /// Creates a managed . /// A managed icon instance. public Icon ToIcon() => ((HICON)this).ToIcon(); /// protected override bool InternalReleaseHandle() => DestroyIcon(this); } } }