From daf01c0de1f378d2895e929a1d76b357bb910715 Mon Sep 17 00:00:00 2001 From: David Hall Date: Sun, 24 Feb 2019 18:37:29 -0700 Subject: [PATCH] Added CopyXX methods for bitmaps, icons and cursors --- PInvoke/User32.Gdi/WinUser.Cursor.cs | 83 +++++++++++++++++++++++++++--------- PInvoke/User32.Gdi/WinUser.Gdi.cs | 20 +++++++++ PInvoke/User32.Gdi/WinUser.Icon.cs | 68 +++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 21 deletions(-) diff --git a/PInvoke/User32.Gdi/WinUser.Cursor.cs b/PInvoke/User32.Gdi/WinUser.Cursor.cs index 36db73f9..65ff4669 100644 --- a/PInvoke/User32.Gdi/WinUser.Cursor.cs +++ b/PInvoke/User32.Gdi/WinUser.Cursor.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Runtime.InteropServices; namespace Vanara.PInvoke @@ -104,32 +105,72 @@ namespace Vanara.PInvoke public static extern bool ClipCursor(in RECT lpRect); /// - /// Copies the specified cursor. + /// 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. /// - /// - /// Type: HCURSOR - /// A handle to the cursor to be copied. + /// + /// 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. + /// + /// /// /// - /// None + /// The return value is a safe handle to the newly created image. /// - /// - /// - /// CopyCursor enables an application or DLL to obtain the handle to a cursor shape owned by another module. Then if the other - /// module is freed, the application is still able to use the cursor shape. - /// - /// Before closing, an application must call the DestroyCursor function to free any system resources associated with the cursor. - /// Do not use the CopyCursor function for animated cursors. Instead, use the CopyImage function. - /// CopyCursor is implemented as a call to the CopyIcon function. - /// - // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-copycursor void CopyCursor( pcur ); - [PInvokeData("winuser.h", MSDNShortId = "copycursor")] - public static SafeHCURSOR CopyCursor(HCURSOR pcur) + [PInvokeData("winuser.h")] + public static SafeHCURSOR CopyCursor(HCURSOR h, Size desiredSize = default, CopyImageOptions options = 0) { - var safeHIcon = CopyIcon(new HICON((IntPtr)pcur)); - var ptr = safeHIcon.DangerousGetHandle(); - safeHIcon.SetHandleAsInvalid(); - return new SafeHCURSOR(ptr, true); + var hret = CopyImage(h.DangerousGetHandle(), LoadImageType.IMAGE_CURSOR, desiredSize.Width, desiredSize.Height, options); + if (hret == HANDLE.NULL) Win32Error.ThrowLastError(); + return new SafeHCURSOR(hret.DangerousGetHandle(), true); } /// diff --git a/PInvoke/User32.Gdi/WinUser.Gdi.cs b/PInvoke/User32.Gdi/WinUser.Gdi.cs index ac87a41a..ea352206 100644 --- a/PInvoke/User32.Gdi/WinUser.Gdi.cs +++ b/PInvoke/User32.Gdi/WinUser.Gdi.cs @@ -1489,6 +1489,26 @@ namespace Vanara.PInvoke [PInvokeData("winuser.h", MSDNShortId = "1448e04c-1452-4eab-bda4-4d249cb67a24")] public static extern int ChangeDisplaySettingsEx([Optional] string lpszDeviceName, in DEVMODE lpDevMode, [Optional] HWND hwnd, [Optional] ChangeDisplaySettingsFlags dwflags, in VIDEOPARAMETERS lParam); + /// + /// Creates a new image (icon, cursor, or bitmap) 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. + /// + /// Type: HANDLEA handle to the image to be copied. + /// Type: UINTThe type of image to be copied. This parameter can be one of the following values.ValueMeaning IMAGE_BITMAP 0 Copies a bitmap. IMAGE_CURSOR 2 Copies a cursor. IMAGE_ICON 1 Copies an icon. + /// Type: intThe desired width, in pixels, of the image. If this is zero, then the returned image will have the same width as the original hImage. + /// Type: intThe desired height, in pixels, of the image. If this is zero, then the returned image will have the same height as the original hImage. + /// Type: UINTThis parameter can be one or more of the following values.ValueMeaning 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. + /// + /// Type: HANDLEIf the function succeeds, the return value is the handle to the newly created image.If the function fails, the return value is NULL. To get extended error information, call GetLastError. + /// + /// + /// When you are finished using the resource, you can release its associated memory by calling one of the functions in the following table.ResourceRelease functionBitmapDeleteObjectCursorDestroyCursorIconDestroyIconThe system automatically deletes the resource when its process terminates, however, calling the appropriate function saves memory and decreases the size of the process's working set. + /// + // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-copyimage + // HANDLE CopyImage( HANDLE h, UINT type, int cx, int cy, UINT flags ); + [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] + [PInvokeData("winuser.h")] + public static extern HANDLE CopyImage(HANDLE h, LoadImageType type, int cx, int cy, CopyImageOptions flags); + /// /// The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method /// (expanding tabs, justifying characters, breaking lines, and so forth). diff --git a/PInvoke/User32.Gdi/WinUser.Icon.cs b/PInvoke/User32.Gdi/WinUser.Icon.cs index f228e9ae..584d55c0 100644 --- a/PInvoke/User32.Gdi/WinUser.Icon.cs +++ b/PInvoke/User32.Gdi/WinUser.Icon.cs @@ -60,6 +60,74 @@ namespace Vanara.PInvoke [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. ///