为WPF按钮添加UAC盾牌图标

在上一篇文章中,介绍了如何获取系统Shell的图标。在C#中调用SHGetStockIconInfo即可获取获取图标。

https://www.cnblogs.com/zhaotianff/p/17662167.html

定义SHGetStockIconInfo函数用到的数据结构

public enum SHSTOCKICONID : uint
    {
        /// Document of a type with no associated application.
        SIID_DOCNOASSOC = 0,
        /// Document of a type with an associated application.
        SIID_DOCASSOC = 1,
        /// Generic application with no custom icon.
        SIID_APPLICATION = 2,
        /// Folder (generic, unspecified state).
        SIID_FOLDER = 3,
        /// Folder (open).
        SIID_FOLDEROPEN = 4,
        /// 5.25-inch disk drive.
        SIID_DRIVE525 = 5,
        /// 3.5-inch disk drive.
        SIID_DRIVE35 = 6,
        /// Removable drive.
        SIID_DRIVEREMOVE = 7,
        /// Fixed drive (hard disk).
        SIID_DRIVEFIXED = 8,
        /// Network drive (connected).
        SIID_DRIVENET = 9,
        /// Network drive (disconnected).
        SIID_DRIVENETDISABLED = 10,
        /// CD drive.
        SIID_DRIVECD = 11,
        /// RAM disk drive.
        SIID_DRIVERAM = 12,
        /// The entire network.
        SIID_WORLD = 13,
        /// A computer on the network.
        SIID_SERVER = 15,
        /// A local printer or print destination.
        SIID_PRINTER = 16,
        /// The Network virtual folder (FOLDERID_NetworkFolder/CSIDL_NETWORK).
        SIID_MYNETWORK = 17,
        /// The Search feature.
        SIID_FIND = 22,
        /// The Help and Support feature.
        SIID_HELP = 23,
        /// Overlay for a shared item.
        SIID_SHARE = 28,
        /// Overlay for a shortcut.
        SIID_LINK = 29,
        /// Overlay for items that are expected to be slow to access.
        SIID_SLOWFILE = 30,
        /// The Recycle Bin (empty).
        SIID_RECYCLER = 31,
        /// The Recycle Bin (not empty).
        SIID_RECYCLERFULL = 32,
        /// Audio CD media.
        SIID_MEDIACDAUDIO = 40,
        /// Security lock.
        SIID_LOCK = 47,
        /// A virtual folder that contains the results of a search.
        SIID_AUTOLIST = 49,
        /// A network printer.
        SIID_PRINTERNET = 50,
        /// A server shared on a network.
        SIID_SERVERSHARE = 51,
        /// A local fax printer.
        SIID_PRINTERFAX = 52,
        /// A network fax printer.
        SIID_PRINTERFAXNET = 53,
        /// A file that receives the output of a Print to file operation.
        SIID_PRINTERFILE = 54,
        /// A category that results from a Stack by command to organize the contents of a folder.
        SIID_STACK = 55,
        /// Super Video CD (SVCD) media.
        SIID_MEDIASVCD = 56,
        /// A folder that contains only subfolders as child items.
        SIID_STUFFEDFOLDER = 57,
        /// Unknown drive type.
        SIID_DRIVEUNKNOWN = 58,
        /// DVD drive.
        SIID_DRIVEDVD = 59,
        /// DVD media.
        SIID_MEDIADVD = 60,
        /// DVD-RAM media.
        SIID_MEDIADVDRAM = 61,
        /// DVD-RW media.
        SIID_MEDIADVDRW = 62,
        /// DVD-R media.
        SIID_MEDIADVDR = 63,
        /// DVD-ROM media.
        SIID_MEDIADVDROM = 64,
        /// CD+ (enhanced audio CD) media.
        SIID_MEDIACDAUDIOPLUS = 65,
        /// CD-RW media.
        SIID_MEDIACDRW = 66,
        /// CD-R media.
        SIID_MEDIACDR = 67,
        /// A writable CD in the process of being burned.
        SIID_MEDIACDBURN = 68,
        /// Blank writable CD media.
        SIID_MEDIABLANKCD = 69,
        /// CD-ROM media.
        SIID_MEDIACDROM = 70,
        /// An audio file.
        SIID_AUDIOFILES = 71,
        /// An image file.
        SIID_IMAGEFILES = 72,
        /// A video file.
        SIID_VIDEOFILES = 73,
        /// A mixed file.
        SIID_MIXEDFILES = 74,
        /// Folder back.
        SIID_FOLDERBACK = 75,
        /// Folder front.
        SIID_FOLDERFRONT = 76,
        /// Security shield. Use for UAC prompts only.
        SIID_SHIELD = 77,
        /// Warning.
        SIID_WARNING = 78,
        /// Informational.
        SIID_INFO = 79,
        /// Error.
        SIID_ERROR = 80,
        /// Key.
        SIID_KEY = 81,
        /// Software.
        SIID_SOFTWARE = 82,
        /// A UI item, such as a button, that issues a rename command.
        SIID_RENAME = 83,
        /// A UI item, such as a button, that issues a delete command.
        SIID_DELETE = 84,
        /// Audio DVD media.
        SIID_MEDIAAUDIODVD = 85,
        /// Movie DVD media.
        SIID_MEDIAMOVIEDVD = 86,
        /// Enhanced CD media.
        SIID_MEDIAENHANCEDCD = 87,
        /// Enhanced DVD media.
        SIID_MEDIAENHANCEDDVD = 88,
        /// High definition DVD media in the HD DVD format.
        SIID_MEDIAHDDVD = 89,
        /// High definition DVD media in the Blu-ray Disc™ format.
        SIID_MEDIABLURAY = 90,
        /// Video CD (VCD) media.
        SIID_MEDIAVCD = 91,
        /// DVD+R media.
        SIID_MEDIADVDPLUSR = 92,
        /// DVD+RW media.
        SIID_MEDIADVDPLUSRW = 93,
        /// A desktop computer.
        SIID_DESKTOPPC = 94,
        /// A mobile computer (laptop).
        SIID_MOBILEPC = 95,
        /// The User Accounts Control Panel item.
        SIID_USERS = 96,
        /// Smart media.
        SIID_MEDIASMARTMEDIA = 97,
        /// CompactFlash media.
        SIID_MEDIACOMPACTFLASH = 98,
        /// A cell phone.
        SIID_DEVICECELLPHONE = 99,
        /// A digital camera.
        SIID_DEVICECAMERA = 100,
        /// A digital video camera.
        SIID_DEVICEVIDEOCAMERA = 101,
        /// An audio player.
        SIID_DEVICEAUDIOPLAYER = 102,
        /// Connect to network.
        SIID_NETWORKCONNECT = 103,
        /// The Network and Internet Control Panel item.
        SIID_INTERNET = 104,
        /// A compressed file with a .zip file name extension.
        SIID_ZIPFILE = 105,
        /// The Additional Options Control Panel item.
        SIID_SETTINGS = 106,
        /// High definition DVD drive (any type - HD DVD-ROM, HD DVD-R, HD-DVD-RAM) that uses the HD DVD format.
        /// Windows Vista with SP1 and later.
        SIID_DRIVEHDDVD = 132,
        /// High definition DVD drive (any type - BD-ROM, BD-R, BD-RE) that uses the Blu-ray Disc format.
        /// Windows Vista with SP1 and later.
        SIID_DRIVEBD = 133,
        /// High definition DVD-ROM media in the HD DVD-ROM format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIAHDDVDROM = 134,
        /// High definition DVD-R media in the HD DVD-R format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIAHDDVDR = 135,
        /// High definition DVD-RAM media in the HD DVD-RAM format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIAHDDVDRAM = 136,
        /// High definition DVD-ROM media in the Blu-ray Disc BD-ROM format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIABDROM = 137,
        /// High definition write-once media in the Blu-ray Disc BD-R format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIABDR = 138,
        /// High definition read/write media in the Blu-ray Disc BD-RE format.
        /// Windows Vista with SP1 and later.
        SIID_MEDIABDRE = 139,
        /// A cluster disk array.
        /// Windows Vista with SP1 and later.
        SIID_CLUSTEREDDRIVE = 140,
        /// The highest valid value in the enumeration.
        /// Values over 160 are Windows 7-only icons.
        SIID_MAX_ICONS = 175
    }

    [Flags]
    public enum SHGSI : uint
    {
        /// The szPath and iIcon members of the SHSTOCKICONINFO structure receive the path and icon index of the requested icon, in a format suitable for passing to the ExtractIcon function. The numerical value of this flag is zero, so you always get the icon location regardless of other flags.
        SHGSI_ICONLOCATION = 0,
        /// The hIcon member of the SHSTOCKICONINFO structure receives a handle to the specified icon.
        SHGSI_ICON = 0x000000100,
        /// The iSysImageImage member of the SHSTOCKICONINFO structure receives the index of the specified icon in the system imagelist.
        SHGSI_SYSICONINDEX = 0x000004000,
        /// Modifies the SHGSI_ICON value by causing the function to add the link overlay to the file's icon.
        SHGSI_LINKOVERLAY = 0x000008000,
        /// Modifies the SHGSI_ICON value by causing the function to blend the icon with the system highlight color.
        SHGSI_SELECTED = 0x000010000,
        /// Modifies the SHGSI_ICON value by causing the function to retrieve the large version of the icon, as specified by the SM_CXICON and SM_CYICON system metrics.
        SHGSI_LARGEICON = 0x000000000,
        /// Modifies the SHGSI_ICON value by causing the function to retrieve the small version of the icon, as specified by the SM_CXSMICON and SM_CYSMICON system metrics.
        SHGSI_SMALLICON = 0x000000001,
        /// Modifies the SHGSI_LARGEICON or SHGSI_SMALLICON values by causing the function to retrieve the Shell-sized icons rather than the sizes specified by the system metrics.
        SHGSI_SHELLICONSIZE = 0x000000004
    }

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHSTOCKICONINFO
    {
        public UInt32 cbSize;
        public IntPtr hIcon;
        public Int32 iSysIconIndex;
        public Int32 iIcon;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UACIcon.MAX_PATH)]
        public string szPath;
    }

 说明:

SHSTOCKICONID这里实际只需要定义SIID_SHIELD这一个值就行了,我这里全部贴出来了,以后需要用到的时候可以直接到这里复制了。

定义UACIcon的类,在这个类里封装获取UAC图标的方法

public class UACIcon
    {
        public const int MAX_PATH = 260;

        [DllImport("Shell32.dll", SetLastError = false)]
        public static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool DestroyIcon(IntPtr hIcon);

        public static BitmapSource GetUACIcon()
        {
            BitmapSource shieldSource = null;

            SHSTOCKICONINFO sii = new SHSTOCKICONINFO();
            sii.cbSize = (UInt32)Marshal.SizeOf(typeof(SHSTOCKICONINFO));

            Marshal.ThrowExceptionForHR(SHGetStockIconInfo(SHSTOCKICONID.SIID_SHIELD,
                SHGSI.SHGSI_ICON | SHGSI.SHGSI_LARGEICON,
                ref sii));

            shieldSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                sii.hIcon,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            DestroyIcon(sii.hIcon);
            return shieldSource;
        }
    }

上面的代码中包含了从句柄创建WPF ImageSource的代码,如果对这一块不了解的话,可以参考

Imaging.CreateBitmapSourceFromHIcon Method (System.Windows.Interop) | Microsoft Learn

界面XAML如下:


        
    

加载图标

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.img_Icon.Source = UACIcon.GetUACIcon();
        }
    }

运行效果:

为WPF按钮添加UAC盾牌图标_第1张图片

示例代码

参考资料:

获取系统图标

https://www.cnblogs.com/zhaotianff/p/17662167.html

SHGetStockIconInfo函数P/Invoke签名

pinvoke.net:SHGetStockIconInfo (shell32)   

你可能感兴趣的:(WPF,wpf,系统图标)