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

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

 

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

01 #region Const
02 const int BCM_SETSHIELD = 0x0000160C;
03 #endregion
04  
05  
06 #region Private Method
07 private static bool AtLeastVista()
08 {
09     return (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6);
10 }
11  
12 private 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。

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

 

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

view source print ?
01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.Windows.Forms;
06 using System.Runtime.InteropServices;
07 using System.Diagnostics;
08  
09 public 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就可以了。

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

 

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


你可能感兴趣的:([C#]Enable UAC Shield icons and run as administrator)