我想使用,SystemIcons.Warning
但是对于我来说太大了。我要调整大小。
我试过了 :
Icon sizedIcon = new Icon(SystemIcons.Warning, new Size(10,10));
但它不起作用,图标保持不变。
有什么建议么?
在.NET Icon类是相当残缺,它是停留在过去十年中由于一次相关的Windows 98和Windows 2000的支持在此之上,Windows不支持任何尺寸装载系统图标其他较系统的默认图标大小。通常为32x32。调整大小将不可避免地看起来很糟糕。
请记住,没有任何图标在10x10的分辨率下看起来会很好。在现代显示器上,它只是一个斑点。您可以从接近所需最终尺寸的图标尺寸开始,取得一定的进步,所需的重新调整尺寸越少,看起来合理的几率就越大。如果可以的话,请偏爱图标中可能存在的尺寸。像16x16、32x32、48x48。
任何,这是可修复的。请记住,由于Windows不直接支持此操作,因此这需要花费大量的时间。所需要的是直接从系统DLL资源中读取图标。并且使用一种更现代的winapi LoadImage()而不是.NET使用的WinAPI LoadIcon()。适用于XP及更高版本。
在您的项目中添加一个新类,并粘贴以下代码:
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
public class IconEx : IDisposable {
public enum SystemIcons {
Application = 100,
Asterisk = 104,
Error = 103,
Exclamation = 101,
Hand = 103,
Information = 104,
Question = 102,
Shield = 106,
Warning = 101,
WinLogo = 100
}
public IconEx(string path, Size size) {
IntPtr hIcon = LoadImage(IntPtr.Zero, path, IMAGE_ICON, size.Width, size.Height, LR_LOADFROMFILE);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public IconEx(SystemIcons sysicon, Size size) {
IntPtr hUser = GetModuleHandle("user32");
IntPtr hIcon = LoadImage(hUser, (IntPtr)sysicon, IMAGE_ICON, size.Width, size.Height, 0);
if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
attach(hIcon);
}
public Icon Icon {
get { return this.icon; }
}
public void Dispose() {
if (icon != null) icon.Dispose();
}
private Icon icon;
private void attach(IntPtr hIcon) {
// Invoke the private constructor so we can get the Icon object to own the handle
var ci = typeof(Icon).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(IntPtr), typeof(bool) }, null);
this.icon = (Icon)ci.Invoke(new object[] { hIcon, true});
}
private const int IMAGE_ICON = 1;
private const int LR_LOADFROMFILE = 0x10;
private const int LR_SHARED = 0x8000;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, string lpszName, int uType,
int cxDesired, int cyDesired, int fuLoad);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadImage(IntPtr hinst, IntPtr resId, int uType,
int cxDesired, int cyDesired, int fuLoad);
}
用法示例:
using (var icon = new IconEx(IconEx.SystemIcons.Warning, new Size(10, 10))) {
e.Graphics.DrawIcon(icon.Icon, 0, 0);
}
它看起来确实比SystemIcons.Warning好。使用16x16时干净利落。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句