c# - 向按钮添加 UAC 屏蔽并保留其背景图像?

标签 c# winforms uac dllimport

在 winforms 应用程序中使用 C# 和 .Net 4.0:是否可以将 UAC 屏蔽添加到按钮并保留按钮背景图像?怎么办?

这就是我目前正在使用的,但它删除了图像...

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

public static void UACToButton(Button button, bool add)
{
    const Int32 BCM_SETSHIELD = 0x160C;
    if (add)
        {
        // The button must have the flat style
        button.FlatStyle = FlatStyle.System;
        if (button.Text == "")
        // and it must have text to display the shield
            button.Text = " ";
            SendMessage(button.Handle, BCM_SETSHIELD, 0, 1);        
        }
        else
            SendMessage(button.Handle, BCM_SETSHIELD, 0, 0);    
}

谢谢!

最佳答案

正如 Elmue 提到的,SystemIcons.Shield 是访问 UAC Shield 图标的最简单方法。这是我用来将它添加到其他图像之上的方法:

public static Image AddUACShieldToImage(Image image)
    {
        var shield = SystemIcons.Shield.ToBitmap();
        shield.MakeTransparent();

        var g = Graphics.FromImage(image);
        g.CompositingMode = CompositingMode.SourceOver;
        g.DrawImage(shield, new Rectangle(image.Width / 2, image.Height / 2, image.Width / 2, image.Height / 2));

        return image;
    }

关于c# - 向按钮添加 UAC 屏蔽并保留其背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13758458/

相关文章:

file - Win32 - 获取将文件保存到C盘的权限

c# - 如何为 NSubstitute.Received 调用指定失败消息?

c# - 无法将日期字符串转换为 DateTime

c# - 使用 .net (C#) System.Net.Cookie 处理 cookie 值内的逗号

c# - 在 App.Config 中设置应用程序语言

.net - 是否有 MessageBox.Show 的非阻塞版本(或类似的版本)?

具有不受信任完整性级别的 Windows 进程

c# - 创建 protected 链接

.net - 确保创建的每个控件在 .NET 中都有一个句柄有什么副作用?

windows - 我可以确定 Matlab 是否在 Windows 中以提升的权限运行吗?