为程序中按钮添加Shield图标2011-09-20之前讲的Windows 7兼容性Webcast中我做了一个Shield Icon的Demo,这个图标就是表示点击该按钮后执行的是一个要求提升权限的操作,借此写一下它的主要实现过程以及把Webcast中的Demo放出来。其实主要实现是调用了user32.dll中的方法给现有按钮控件贴上一个小图标,可以使用下面方法来实现:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/**////////////////////////////////////////////////////////////////////////
/// <summary>
/// Enables the elevated shield icon on the given button control
/// </summary>
/// <param name="ThisButton">
/// Button control to enable the elevated shield icon on.
/// </param>
///////////////////////////////////////////////////////////////////////
private void EnableElevateIcon_BCM_SETSHIELD(Button ThisButton)
{
// Input validation, validate that ThisControl is not null
if (ThisButton == null)
{
return;
}
// Define BCM_SETSHIELD locally, declared originally in Commctrl.h
uint BCM_SETSHIELD = 0x0000160C;
// Set button style to the system style
ThisButton.FlatStyle = FlatStyle.System;
// Send the BCM_SETSHIELD message to the button control
SendMessage(new HandleRef(ThisButton, ThisButton.Handle), BCM_SETSHIELD, new IntPtr(0), new IntPtr(1));
}
然后直接将按钮传入该方法就可以实现效果:
EnableElevateIcon_BCM_SETSHIELD(button2);
实现效果如下: