我有使用 SystemIcons 的自定义 MessageBox。问题是它们看起来已经过时了。请参见下图,其中包含警告/感叹号图标。我想设置与顶部 MessageBox 相同的图标。我怎样才能做到呢?
相关问题 - 如何从 dll 中提取系统图标(我已经尝试过 7-Zip 方法和 Resource Hacker,但是无法获取内部图形,但在 Visual Studio 中它显示但排列在文件树而不是缩略图中)。

我在 System32 文件夹中找到了“SecurityAndMaintenance_Alert.PNG”文件,它看起来非常相似,但是边框看起来更薄,如果可能的话,我想设置完全相同的图标。
您需要使用SHGetStockIconInfo。
\nvar sii = new NativeMethods.SHSTOCKICONINFO();\nsii.cbSize = (UInt32)Marshal.SizeOf(typeof(NativeMethods.SHSTOCKICONINFO));\nNativeMethods.SHGetStockIconInfo(NativeMethods.SHSTOCKICONID.SIID_WARNING,\n NativeMethods.SHGSI.SHGSI_ICON,\n ref sii);\nthis.pictureBox1.Image = Icon.FromHandle(sii.hIcon).ToBitmap();\nRun Code Online (Sandbox Code Playgroud)\n\n您可以在这里看到所有图标。
\n这是 NativeMethods:
\nusing System;\nusing System.Runtime.InteropServices;\n\npublic class NativeMethods\n{\n public const int MAX_PATH = 260;\n\n [Flags]\n public enum SHGSI : uint\n {\n SHGSI_ICONLOCATION = 0,\n SHGSI_ICON = 0x000000100,\n SHGSI_SYSICONINDEX = 0x000004000,\n SHGSI_LINKOVERLAY = 0x000008000,\n SHGSI_SELECTED = 0x000010000,\n SHGSI_LARGEICON = 0x000000000,\n SHGSI_SMALLICON = 0x000000001,\n SHGSI_SHELLICONSIZE = 0x000000004\n }\n public enum SHSTOCKICONID : uint\n {\n SIID_DOCNOASSOC = 0, //Blank document icon (Document of a type with no associated application).\n SIID_DOCASSOC = 1, //Application-associated document icon (Document of a type with an associated application).\n SIID_APPLICATION = 2, //Generic application with no custom icon.\n SIID_FOLDER = 3, //Folder (generic, unspecified state).\n SIID_FOLDEROPEN = 4, //Folder (open).\n SIID_DRIVE525 = 5, //5.25-inch disk drive.\n SIID_DRIVE35 = 6, //3.5-inch disk drive.\n SIID_DRIVEREMOVE = 7, //Removable drive.\n SIID_DRIVEFIXED = 8, //Fixed drive (hard disk).\n SIID_DRIVENET = 9, //Network drive (connected).\n SIID_DRIVENETDISABLED = 10, //Network drive (disconnected).\n SIID_DRIVECD = 11, //CD drive.\n SIID_DRIVERAM = 12, //RAM disk drive.\n SIID_WORLD = 13, //The entire network.\n SIID_SERVER = 15, //A computer on the network.\n SIID_PRINTER = 16, //A local printer or print destination.\n SIID_MYNETWORK = 17, //The\xc2\xa0Network\xc2\xa0virtual folder (FOLDERID_NetworkFolder/CSIDL_NETWORK).\n SIID_FIND = 22, //The\xc2\xa0Search\xc2\xa0feature.\n SIID_HELP = 23, //The\xc2\xa0Help and Support\xc2\xa0feature.\n SIID_SHARE = 28, //Overlay for a shared item.\n SIID_LINK = 29, //Overlay for a shortcut.\n SIID_SLOWFILE = 30, //Overlay for items that are expected to be slow to access.\n SIID_RECYCLER = 31, //The Recycle Bin (empty).\n SIID_RECYCLERFULL = 32, //The Recycle Bin (not empty).\n SIID_MEDIACDAUDIO = 40, //Audio CD media.\n SIID_LOCK = 47, //Security lock.\n SIID_AUTOLIST = 49, //A virtual folder that contains the results of a search.\n SIID_PRINTERNET = 50, //A network printer.\n SIID_SERVERSHARE = 51, //A server shared on a network.\n SIID_PRINTERFAX = 52, //A local fax printer.\n SIID_PRINTERFAXNET = 53, //A network fax printer.\n SIID_PRINTERFILE = 54, //A file that receives the output of a\xc2\xa0Print to file\xc2\xa0operation.\n SIID_STACK = 55, //A category that results from a\xc2\xa0Stack by\xc2\xa0command to organize the contents of a folder.\n SIID_MEDIASVCD = 56, //Super Video CD (SVCD) media.\n SIID_STUFFEDFOLDER = 57, //A folder that contains only subfolders as child items.\n SIID_DRIVEUNKNOWN = 58, //Unknown drive type.\n SIID_DRIVEDVD = 59, //DVD drive.\n SIID_MEDIADVD = 60, //DVD media.\n SIID_MEDIADVDRAM = 61, //DVD-RAM media.\n SIID_MEDIADVDRW = 62, //DVD-RW media.\n SIID_MEDIADVDR = 63, //DVD-R media.\n SIID_MEDIADVDROM = 64, //DVD-ROM media.\n SIID_MEDIACDAUDIOPLUS = 65, //CD+ (enhanced audio CD) media.\n SIID_MEDIACDRW = 66, //CD-RW media.\n SIID_MEDIACDR = 67, //CD-R media.\n SIID_MEDIACDBURN = 68, //A writeable CD in the process of being burned.\n SIID_MEDIABLANKCD = 69, //Blank writable CD media.\n SIID_MEDIACDROM = 70, //CD-ROM media.\n SIID_AUDIOFILES = 71, //An audio file.\n SIID_IMAGEFILES = 72, //An image file.\n SIID_VIDEOFILES = 73, //A video file.\n SIID_MIXEDFILES = 74, //A mixed file.\n SIID_FOLDERBACK = 75, //Folder back.\n SIID_FOLDERFRONT = 76, //Folder front.\n SIID_SHIELD = 77, //Security shield. Use for UAC prompts only.\n SIID_WARNING = 78, //Warning.\n SIID_INFO = 79, //Informational.\n SIID_ERROR = 80, //Error.\n SIID_KEY = 81, //Key.\n SIID_SOFTWARE = 82, //Software.\n SIID_RENAME = 83, //A UI item, such as a button, that issues a rename command.\n SIID_DELETE = 84, //A UI item, such as a button, that issues a delete command.\n SIID_MEDIAAUDIODVD = 85, //Audio DVD media.\n SIID_MEDIAMOVIEDVD = 86, //Movie DVD media.\n SIID_MEDIAENHANCEDCD = 87, //Enhanced CD media.\n SIID_MEDIAENHANCEDDVD = 88, //Enhanced DVD media.\n SIID_MEDIAHDDVD = 89, //High definition DVD media in the HD DVD format.\n SIID_MEDIABLURAY = 90, //High definition DVD media in the Blu-ray Disc\xe2\x84\xa2 format.\n SIID_MEDIAVCD = 91, //Video CD (VCD) media.\n SIID_MEDIADVDPLUSR = 92, //DVD+R media.\n SIID_MEDIADVDPLUSRW = 93, //DVD+RW media.\n SIID_DESKTOPPC = 94, //A desktop computer.\n SIID_MOBILEPC = 95, //A mobile computer (laptop).\n SIID_USERS = 96, //The\xc2\xa0User Accounts\xc2\xa0Control Panel item.\n SIID_MEDIASMARTMEDIA = 97, //Smart media.\n SIID_MEDIACOMPACTFLASH = 98, //CompactFlash media.\n SIID_DEVICECELLPHONE = 99, //A cell phone.\n SIID_DEVICECAMERA = 100, //A digital camera.\n SIID_DEVICEVIDEOCAMERA = 101, //A digital video camera.\n SIID_DEVICEAUDIOPLAYER = 102, //An audio player.\n SIID_NETWORKCONNECT = 103, //Connect to network.\n SIID_INTERNET = 104, //The\xc2\xa0Network and Internet\xc2\xa0Control Panel item.\n SIID_ZIPFILE = 105, //A compressed file with a .zip file name extension.\n SIID_SETTINGS = 106, //The\xc2\xa0Additional Options\xc2\xa0Control Panel item.\n SIID_DRIVEHDDVD = 132, //Windows Vista with Service Pack 1 (SP1) and later. High definition DVD drive (any type - HD DVD-ROM, HD DVD-R, HD-DVD-RAM) that uses the HD DVD format.\n SIID_DRIVEBD = 133, //Windows Vista with SP1 and later. High definition DVD drive (any type - BD-ROM, BD-R, BD-RE) that uses the Blu-ray Disc format.\n SIID_MEDIAHDDVDROM = 134, //Windows Vista with SP1 and later. High definition DVD-ROM media in the HD DVD-ROM format.\n SIID_MEDIAHDDVDR = 135, //Windows Vista with SP1 and later. High definition DVD-R media in the HD DVD-R format.\n SIID_MEDIAHDDVDRAM = 136, //Windows Vista with SP1 and later. High definition DVD-RAM media in the HD DVD-RAM format.\n SIID_MEDIABDROM = 137, //Windows Vista with SP1 and later. High definition DVD-ROM media in the Blu-ray Disc BD-ROM format.\n SIID_MEDIABDR = 138, //Windows Vista with SP1 and later. High definition write-once media in the Blu-ray Disc BD-R format.\n SIID_MEDIABDRE = 139, //Windows Vista with SP1 and later. High definition read/write media in the Blu-ray Disc BD-RE format.\n SIID_CLUSTEREDDRIVE = 140, //Windows Vista with SP1 and later. A cluster disk array.\n SIID_MAX_ICONS = 174, //The highest valid value in the enumeration. Values over 160 are Windows\xc2\xa07-only icons.\n }\n [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]\n public struct SHSTOCKICONINFO\n {\n public UInt32 cbSize;\n public IntPtr hIcon;\n public Int32 iSysIconIndex;\n public Int32 iIcon;\n [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]\n public string szPath;\n }\n [DllImport("Shell32.dll", SetLastError = false)]\n public static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);\n}\nRun Code Online (Sandbox Code Playgroud)\n