之前讲的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));
}
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);
实现效果如下:
注意:这个图标不能将要运行的程序提升权限运行,仅仅是个样子货,呵呵。
然后是icacls这个工具,使用Vista的用户可以下载下来用用看,测试命令如下:
icacls c:\windows
然后得到如下结果:
C:\Users\WilsonWu
>
icacls c:\windows
c:\windows NT SERVICE\TrustedInstaller:(F)
NT SERVICE\TrustedInstaller:(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(M)
NT AUTHORITY\SYSTEM:(OI)(CI)(IO)(F)
BUILTIN\Administrators:(M)
BUILTIN\Administrators:(OI)(CI)(IO)(F)
BUILTIN\Users:(RX)
BUILTIN\Users:(OI)(CI)(IO)(GR,GE)
CREATOR OWNER:(OI)(CI)(IO)(F)
Successfully processed 1 files; Failed processing 0 files
c:\windows NT SERVICE\TrustedInstaller:(F)
NT SERVICE\TrustedInstaller:(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(M)
NT AUTHORITY\SYSTEM:(OI)(CI)(IO)(F)
BUILTIN\Administrators:(M)
BUILTIN\Administrators:(OI)(CI)(IO)(F)
BUILTIN\Users:(RX)
BUILTIN\Users:(OI)(CI)(IO)(GR,GE)
CREATOR OWNER:(OI)(CI)(IO)(F)
Successfully processed 1 files; Failed processing 0 files