博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
给button添加UAC的小盾牌图标
阅读量:4314 次
发布时间:2019-06-06

本文共 1804 字,大约阅读时间需要 6 分钟。

Sample Code:

public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            RestartElevated();        }        static internal bool IsAdmin()        {            WindowsIdentity id = WindowsIdentity.GetCurrent();            WindowsPrincipal p = new WindowsPrincipal(id);            return p.IsInRole(WindowsBuiltInRole.Administrator);        }        static internal void AddShieldToButton(Button b)        {            b.FlatStyle = FlatStyle.System;            SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);        }        [DllImport("user32")]        public static extern UInt32 SendMessage            (IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);        internal const int BCM_FIRST = 0x1600; //Normal button        internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C); //Elevated button        internal static void RestartElevated()        {            ProcessStartInfo startInfo = new ProcessStartInfo();            startInfo.UseShellExecute = true;            startInfo.WorkingDirectory = @"d:\";            startInfo.FileName = "ConsoleApplication4.exe";            startInfo.Verb = "runas";            try            {                Process p = Process.Start(startInfo);            }            catch (System.ComponentModel.Win32Exception ex)            {                return;            }            Application.Exit();        }        private void Form1_Load(object sender, EventArgs e)        {            if (!IsAdmin())            {                this.Text += " (Standard)"; //Unnecessary                AddShieldToButton(this.button1); //Important            }        }    }

  

转载于:https://www.cnblogs.com/xixifusigao/p/3289278.html

你可能感兴趣的文章
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
springMVC中一个class中的多个方法
查看>>
Linux系统安装出错后出现grub rescue的修复方法
查看>>
线段树模板整理
查看>>
[教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
查看>>
[iOS问题归总]iPhone上传项目遇到的问题
查看>>
Python天天美味(总) --转
查看>>
Spring Framework tutorial
查看>>
【VS开发】win7下让程序默认以管理员身份运行
查看>>
【机器学习】Learning to Rank 简介
查看>>
Unity 使用实体类
查看>>
【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件...
查看>>
MySQL常见注意事项及优化
查看>>
流畅的Python (Fluent Python) —— 前言
查看>>