该功能必须Vindows Vista以后的版本。
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Extentions {
public class ShieldIconButton : System.Windows.Forms.Button {
public ShieldIconButton() {
this.FlatStyle = FlatStyle.System;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public const int BCM_SETSHIELD = 0x160C;
public new FlatStyle FlatStyle {
get { return base.FlatStyle; }
set {
base.FlatStyle = value;
if (base.FlatStyle== FlatStyle.System){
this.SetShieldIcon(this.ShieldIcon);
}
}
}
private bool _ShieldIcon;
/// <summary>
/// 管理员权限属性。
/// </summary>
[Category("管理员权限")]
[DefaultValue(false)]
[Description("管理员权限图标设置/取消。")]
public bool ShieldIcon {
get { return _ShieldIcon; }
set {
if (value == _ShieldIcon) {
return;
}
_ShieldIcon = value;
this.SetShieldIcon(_ShieldIcon);
}
}
private void SetShieldIcon(bool shield) {
SendMessage(this.Handle, BCM_SETSHIELD, IntPtr.Zero, new IntPtr(shield ? 1 : 0));
}
}
}