Win7编程:在按钮中加入管理员权限运行盾牌图标

0
回复
375
查看
[复制链接]
发表于 2014-1-5 23:13:17 | 显示全部楼层 |阅读模式
Windows7之家(www.908098.com):Win7 / Vista编程:在按钮中加入管理员权限运行盾牌图标
下文分两部分,第一,如何实现软件本身运行时申请管理员权限,第二,如何在软件窗体内的按钮上面加入盾牌图标(意味着本功能需要管理员权限执行)。
一、软件自身运行时的管理员权限申请机制
在开启UAC的时候,在Win7(Windows Server 2008 R2)或者Vista(Windows Server 2008)中执行程序默认是以一种权限较低的方式执行的,但是在这种方式下,我们有些操作会失败(比如修改注册表,监听端口,往系统目录写入文件等),要实现这些操作,就需要我们以管理员权限执行程序了。

当然,只有在程序上右键,选择“以管理员执行”就可以,不过如何让程序自己自动以管理员权限来运行呢,这就需要Manifest了。

首先我们来新建个项目(懒得改名字了,就叫WindowsFormsApplication1吧)



按F5执行下(恩,貌似没有啥问题[空文档,有问题才怪... ])



然后我们添加Manifest(中文版叫“应用程序清单文件”)




下面我们看下Manifest的内容——
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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 用户帐户控制级别,请用以下节点之一替换 requestedExecutionLevel 节点。 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 如果您希望利用文件和注册表虚拟化提供 向后兼容性,请删除 requestedExecutionLevel 节点。 --> <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo>
  3. </asmv1:assembly>
复制代码
内容里的说明够详细了吧,只要把 asInvoker替换成requireAdministrator,我们的程序就会默认要求管理员权限运行了,该下执行试试效果。

恩,窗口弹出来了。 看下程序图标:

大功告成...

二、下面再说下怎么给程序的按钮上也加上小盾牌图标吧

这我们就需要调用Win32 API了,要调用API么,要先引用命名空间——
  1. using System.Runtime.InteropServices;
复制代码
然后调用API
  1. [DllImport("user32.dll")]
复制代码
  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)); }
复制代码
在Form上拖个Button,拖大一点哦,小了图标看不清


然后在Form1_Load里,用API把图标加到Button1上
  1. private void Form1_Load(object sender, EventArgs e) { EnableElevateIcon_BCM_SETSHIELD(button1); }
复制代码
  1. 最后执行看下效果吧!
复制代码
复制代码
复制代码
  1. 恩?盾牌为啥有点不一样呢,上面那个图标是Windows Server 2008或者Vista上的,Win7和Windows Server 2008 R2上应该是下面这样:
复制代码
  1. [/code][code]using System.Runtime.InteropServices;0
复制代码
Windows7之家(www.908098.com),凝聚你我他
关注官方微信

微信号:大白网

微博:大白网

QQ1群:4731622

QQ2群:4731622

全国服务热线:

QQ24485416

(工作日:周一至周五 9:00-16:00)
大理市
www@dali8.net

手机版-小黑屋- 大理白族|大理乡村旅游网|大理网|大理论坛|白族网|大理旅游网|大理信息网|大理生活网

技术支持:挖主机网络 Powered by大白网© 2006-2025 dalibaizu.com  滇ICP备19004088号 滇公网安备 53290102000530号