Win7編程:在按鈕中加入管理員權(quán)限運行盾牌圖標(biāo)

2010/6/23 13:03:28    編輯:軟媒 - 笨笨     字體:【

Win7之家www.afsion.com.cn):Win7編程:在按鈕中加入管理員權(quán)限運行盾牌圖標(biāo)

下文分兩部分,第一,如何實現(xiàn)軟件本身運行時申請管理員權(quán)限,第二,如何在軟件窗體內(nèi)的按鈕上面加入盾牌圖標(biāo)(意味著本功能需要管理員權(quán)限執(zhí)行)。

一、軟件自身運行時的管理員權(quán)限申請機(jī)制

在開啟UAC的時候,在Win7(Windows Server 2008 R2)或者Vista(Windows Server 2008)中執(zhí)行程序默認(rèn)是以一種權(quán)限較低的方式執(zhí)行的,但是在這種方式下,我們有些操作會失。ū热缧薷淖员恚O(jiān)聽端口,往系統(tǒng)目錄寫入文件等),要實現(xiàn)這些操作,就需要我們以管理員權(quán)限執(zhí)行程序了。

當(dāng)然,只有在程序上右鍵,選擇“以管理員執(zhí)行”就可以,不過如何讓程序自己自動以管理員權(quán)限來運行呢,這就需要Manifest了。

首先我們來新建個項目(懶得改名字了,就叫WindowsFormsApplication1吧)



按F5執(zhí)行下(恩,貌似沒有啥問題[空文檔,有問題才怪... ])



然后我們添加Manifest(中文版叫“應(yīng)用程序清單文件”)



下面我們看下Manifest的內(nèi)容——

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC 清單選項
            如果希望更改 Windows 用戶帳戶控制級別,請用以下節(jié)點之一替換 
            requestedExecutionLevel 節(jié)點。
        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

          如果您希望利用文件和注冊表虛擬化提供
            向后兼容性,請刪除 requestedExecutionLevel 節(jié)點。
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

內(nèi)容里的說明夠詳細(xì)了吧,只要把 asInvoker替換成requireAdministrator,我們的程序就會默認(rèn)要求管理員權(quán)限運行了,該下執(zhí)行試試效果。

恩,窗口彈出來了。 看下程序圖標(biāo):

大功告成...

二、下面再說下怎么給程序的按鈕上也加上小盾牌圖標(biāo)吧

這我們就需要調(diào)用Win32 API了,要調(diào)用API么,要先引用命名空間——

using System.Runtime.InteropServices;

然后調(diào)用API

[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));
        }

在Form上拖個Button,拖大一點哦,小了圖標(biāo)看不清

 

然后在Form1_Load里,用API把圖標(biāo)加到Button1上

private void Form1_Load(object sender, EventArgs e)
        {
            EnableElevateIcon_BCM_SETSHIELD(button1);
        }
最后執(zhí)行看下效果吧!
 
 
恩?盾牌為啥有點不一樣呢,上面那個圖標(biāo)是Windows Server 2008或者Vista上的,Win7和Windows Server 2008 R2上應(yīng)該是下面這樣:
有錯誤的地方歡迎指證喔。