主頁 > 知識庫 > Win7編程:在按鈕中加入管理員權(quán)限運行盾牌圖標教程

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

熱門標簽:電銷機器人 科大訊飛語音識別系統(tǒng) 萬科 硅谷的囚徒呼叫中心 電話機器人搭建 呼叫中心案例 電銷機器人源碼 鐵路電話系統(tǒng)
這篇教程是向大家介紹win7按鈕中加入管理員權(quán)限運行盾牌圖標過程,本教程分兩部分,第一,如何實現(xiàn)軟件本身運行時申請管理員權(quán)限,第二,如何在軟件窗體內(nèi)的按鈕上面加入盾牌圖標(意味著本功能需要管理員權(quán)限執(zhí)行)。

步驟/方法

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

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

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

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

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

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

7、下面我們看下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>

8、內(nèi)容里的說明夠詳細了吧,只要把 asInvoker替換成requireAdministrator,我們的程序就會默認要求管理員權(quán)限運行了,該下執(zhí)行試試效果。
恩,窗口彈出來了。 看下程序圖標:
大功告成...

9、二、下面再說下怎么給程序的按鈕上也加上小盾牌圖標吧
這我們就需要調(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,拖大一點哦,小了圖標看不清
然后在Form1_Load里,用API把圖標加到Button1上
private void Form1_Load(object sender, EventArgs e)
{
EnableElevateIcon_BCM_SETSHIELD(button1);
}

10、最后執(zhí)行看下效果吧!
恩?盾牌為啥有點不一樣呢,上面那個圖標是Windows Server 2008或者Vista上的,Win7和Windows Server 2008 R2上應該是下面這樣:
有錯誤的地方歡迎指證喔。

END

標簽:新余 株洲 天門 黃石 六安 南通 呼和浩特 黔東

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Win7編程:在按鈕中加入管理員權(quán)限運行盾牌圖標教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266