コード例 #1
0
        /// <summary>
        /// This method will capture the current Cursor by using User32 Code
        /// </summary>
        /// <returns>A IElement with the Mouse Cursor as Image in it.</returns>
        public static bool TryGetCurrentCursor(out BitmapSource bitmapSource, out NativePoint location)
        {
            bitmapSource = null;
            location     = NativePoint.Empty;
            var cursorInfo = CursorInfo.Create();

            if (!NativeCursorMethods.GetCursorInfo(ref cursorInfo))
            {
                return(false);
            }

            if (cursorInfo.Flags != CursorInfoFlags.Showing)
            {
                return(false);
            }

            using (var safeIcon = NativeIconMethods.CopyIcon(cursorInfo.CursorHandle))
            {
                if (!NativeIconMethods.GetIconInfo(safeIcon, out var iconInfo))
                {
                    return(false);
                }

                using (iconInfo.BitmaskBitmapHandle)
                    using (iconInfo.ColorBitmapHandle)
                    {
                        var cursorLocation = User32Api.GetCursorLocation();
                        bitmapSource = Imaging.CreateBitmapSourceFromHIcon(safeIcon.DangerousGetHandle(), Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                        location     = new NativePoint(cursorLocation.X - iconInfo.Hotspot.X, cursorLocation.Y - iconInfo.Hotspot.Y);
                    }
            }

            return(true);
        }
コード例 #2
0
ファイル: Shell32.cs プロジェクト: molongwudi/Dapplo.Windows
        /// <summary>
        ///     Returns an icon for a given file extension - indicated by the name parameter.
        ///     See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx
        /// </summary>
        /// <param name="filename">Filename</param>
        /// <param name="size">Large or small</param>
        /// <param name="linkOverlay">Whether to include the link icon</param>
        /// <returns>System.Drawing.Icon</returns>
        public static TIcon GetFileExtensionIcon <TIcon>(string filename, IconSize size, bool linkOverlay) where TIcon : class
        {
            var shfi = new ShellFileInfo();
            // UseFileAttributes makes it simulate, just gets the icon for the extension
            var flags = ShellGetFileInfoFlags.Icon | ShellGetFileInfoFlags.UseFileAttributes;

            if (linkOverlay)
            {
                flags |= ShellGetFileInfoFlags.LinkOverlay;
            }

            // Check the size specified for return.
            if (IconSize.Small == size)
            {
                flags |= ShellGetFileInfoFlags.SmallIcon;
            }
            else
            {
                flags |= ShellGetFileInfoFlags.LargeIcon;
            }

            SHGetFileInfo(Path.GetFileName(filename), FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);

            // TODO: Fix bad practise for cleanup, and use generics to allow the user to specify if it's an icon/bitmap/-source
            // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
            try
            {
                return(IconHelper.IconHandleTo <TIcon>(shfi.IconHandle));
            }
            finally
            {
                if (shfi.IconHandle != IntPtr.Zero)
                {
                    // Cleanup
                    NativeIconMethods.DestroyIcon(shfi.IconHandle);
                }
            }
        }
コード例 #3
0
ファイル: Shell32.cs プロジェクト: molongwudi/Dapplo.Windows
        /// <summary>
        ///     Used to access system folder icons.
        /// </summary>
        /// <param name="size">Specify large or small icons.</param>
        /// <param name="folderType">Specify open or closed FolderType.</param>
        /// <returns>System.Drawing.Icon</returns>
        public static TIcon GetFolderIcon <TIcon>(IconSize size, FolderType folderType) where TIcon : class
        {
            // Need to add size check, although errors generated at present!
            var flags = ShellGetFileInfoFlags.Icon | ShellGetFileInfoFlags.UseFileAttributes;

            if (FolderType.Open == folderType)
            {
                flags |= ShellGetFileInfoFlags.OpenIcon;
            }

            if (IconSize.Small == size)
            {
                flags |= ShellGetFileInfoFlags.SmallIcon;
            }
            else
            {
                flags |= ShellGetFileInfoFlags.LargeIcon;
            }

            // Get the folder icon
            var shellFileInfo = new ShellFileInfo();

            SHGetFileInfo(null, FILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), flags);

            // Now clone the icon, so that it can be successfully stored in an ImageList
            try
            {
                return(IconHelper.IconHandleTo <TIcon>(shellFileInfo.IconHandle));
            }
            finally
            {
                if (shellFileInfo.IconHandle != IntPtr.Zero)
                {
                    // Cleanup
                    NativeIconMethods.DestroyIcon(shellFileInfo.IconHandle);
                }
            }
        }