[C#]Enable UAC Shield icons and run as administrator

在Win7中常會看到某些程式中會有個按鈕,按鈕上會有個盾牌的圖示,按下後能提升存取權限。這邊紀錄一下這樣的功能要怎樣實現。

 

首先是盾牌的圖示,實作時不是自己去換按鈕的圖片,而是要對Button發送BCM_SETSHIELD(0x0000160C)的Message,訊息發送時lParam參數帶1或0去決定是否顯示盾牌圖示。這邊需注意的是因為這功能在vista以前不提供,所以必須做些處理,另外則是這功能必須要將按鈕的FlatStyle設為System,運行後才會有效果。

01#region Const
02const int BCM_SETSHIELD = 0x0000160C;
03#endregion
04 
05 
06#region Private Method
07private static bool AtLeastVista()
08{
09    return (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6);
10}
11 
12private static void SetButtonShield(Button btn, bool showShield)
13{
14    if (!AtLeastVista())
15        return;
16 
17    btn.FlatStyle = FlatStyle.System;
18    SendMessage(new HandleRef(btn, btn.Handle), BCM_SETSHIELD, IntPtr.Zero, showShield ? new IntPtr(1) : IntPtr.Zero);
19}
20#endregion

 

提升權限的部分則是透過Process去實現RunAs功能,比較要注意的是要帶入當下執行檔的位置,並將Verb設為runas。

1ProcessStartInfo psi = new ProcessStartInfo
2     {
3         Arguments = "-justelevated",
4         ErrorDialog = true,
5         ErrorDialogParentHandle = form.Handle,
6         FileName = Application.ExecutablePath,
7         Verb = "runas"
8     };

 

這邊筆者方便後續使用,將其整理為擴充方法。

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Windows.Forms;
06using System.Runtime.InteropServices;
07using System.Diagnostics;
08 
09public static class ButtonExtension
10{
11    #region DllImport
12    [DllImport("shell32.dll", EntryPoint = "#680", CharSet = CharSet.Unicode)]
13    private static extern bool IsUserAnAdmin();
14 
15    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
16    private static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
17    #endregion
18 
19 
20    #region Const
21    const int BCM_SETSHIELD = 0x0000160C;
22    #endregion
23 
24 
25    #region Private Method
26    private static bool AtLeastVista()
27    {
28        return (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6);
29    }
30 
31    private static void SetButtonShield(Button btn, bool showShield)
32    {
33        if (!AtLeastVista())
34            return;
35 
36        btn.FlatStyle = FlatStyle.System;
37        SendMessage(new HandleRef(btn, btn.Handle), BCM_SETSHIELD, IntPtr.Zero, showShield ? new IntPtr(1) : IntPtr.Zero);
38    }
39 
40    private static Form GetWindowForm(Control control)
41    {
42        Control parent = control.Parent;
43 
44        if (parent == null)
45            return null;
46 
47        if(parent is Form)
48            return parent as Form;
49 
50        return GetWindowForm(parent);
51    }
52    #endregion
53 
54 
55    #region Public Method
56    public static void EnableShieldIcon(this Button button)
57    {
58        SetButtonShield(button, true);
59    }
60 
61    public static void EnableRunAsProcess(this Button button)
62    {
63        button.Click -= new EventHandler(button_RunAsProcess);
64        button.Click += new EventHandler(button_RunAsProcess);
65    }
66    #endregion
67 
68 
69    #region Event Process
70    private static void button_RunAsProcess(object sender, EventArgs e)
71    {
72        Button button = sender as Button;
73        Form form = GetWindowForm(button);
74 
75        if (form == null)
76            return;
77 
78        ProcessStartInfo psi = new ProcessStartInfo
79        {
80            Arguments = "-justelevated",
81            ErrorDialog = true,
82            ErrorDialogParentHandle = form.Handle,
83            FileName = Application.ExecutablePath,
84            Verb = "runas"
85        };
86        try
87        {
88            Process.Start(psi);
89            form.Close();
90        }
91        catch (Exception ex)
92        {
93            MessageBox.Show(ex.Message);
94        }
95    }
96    #endregion
97}

 

使用時對Button去設定EnableShieldIcon與EnableRunAsProcess就可以了。

1private void Form1_Load(object sender, EventArgs e)
2{
3    button1.EnableShieldIcon();
4    button1.EnableRunAsProcess();
5}

 

運行後就會看到盾牌圖示,點擊按鈕也會將權限提升。

image


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值