You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取用户壁纸?实现类似UAC背景的效果

实现UAC风格的桌面背景效果

你找到的这段获取壁纸路径的代码存在明显局限——当用户删除了原壁纸文件后,就无法正常获取路径,也就没法基于它来模拟UAC背景。而UAC的背景效果本质是对当前实时桌面进行模糊+暗化处理,不需要依赖壁纸文件,直接操作桌面窗口或使用系统API就能实现。

方案一:利用DWM API实现桌面模糊+全屏暗化层

通过Windows的DWM(桌面窗口管理器)API给桌面窗口添加模糊效果,同时创建一个全屏半透明置顶窗体模拟UAC的暗化层,无论用户是否删除壁纸都能生效:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace UACBackgroundSimulator
{
    public partial class UACBackgroundForm : Form
    {
        // DWM API 声明
        [DllImport("dwmapi.dll")]
        private static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DWM_BLURBEHIND pBlurBehind);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        // 模糊背景配置结构体
        [StructLayout(LayoutKind.Sequential)]
        private struct DWM_BLURBEHIND
        {
            public uint dwFlags;
            public bool fEnable;
            public IntPtr hRgnBlur;
            public bool fTransitionOnMaximized;
        }

        const uint DWM_BB_ENABLE = 0x00000001;

        public UACBackgroundForm()
        {
            InitializeComponent();
            // 配置窗体为全屏无框置顶
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            TopMost = true;
            // 设置半透明暗化效果
            Opacity = 0.7;

            // 给桌面窗口应用模糊效果
            IntPtr desktopHandle = GetDesktopWindow();
            DWM_BLURBEHIND blurConfig = new DWM_BLURBEHIND();
            blurConfig.dwFlags = DWM_BB_ENABLE;
            blurConfig.fEnable = true;
            DwmEnableBlurBehindWindow(desktopHandle, ref blurConfig);
        }
    }
}

方案二:截取实时桌面并模糊绘制

直接截取当前桌面画面,对截图进行模糊处理后绘制在全屏窗体上,再叠加暗化层,效果更可控且不依赖系统DWM设置:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace UACBackgroundSimulator
{
    public partial class BlurDesktopForm : Form
    {
        [DllImport("user32.dll")]
        private static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, uint nFlags);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        public BlurDesktopForm()
        {
            InitializeComponent();
            // 配置全屏无框置顶
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            TopMost = true;
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            // 截取当前桌面画面
            var screenBounds = Screen.PrimaryScreen.Bounds;
            Bitmap desktopScreenshot = new Bitmap(screenBounds.Width, screenBounds.Height);
            using (Graphics g = Graphics.FromImage(desktopScreenshot))
            {
                IntPtr hdc = g.GetHdc();
                PrintWindow(GetDesktopWindow(), hdc, 0);
                g.ReleaseHdc(hdc);
            }

            // 应用模糊滤镜(这里用简易灰度模糊,也可以替换为高斯模糊算法)
            using (var blurAttr = new System.Drawing.Imaging.ImageAttributes())
            {
                blurAttr.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(new float[][]
                {
                    new float[] {0.33f, 0.33f, 0.33f, 0, 0},
                    new float[] {0.33f, 0.33f, 0.33f, 0, 0},
                    new float[] {0.33f, 0.33f, 0.33f, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                }));
                e.Graphics.DrawImage(desktopScreenshot, ClientRectangle, 0, 0, desktopScreenshot.Width, desktopScreenshot.Height, GraphicsUnit.Pixel, blurAttr);
            }

            // 叠加半透明黑色暗化层
            using (SolidBrush darkLayer = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
            {
                e.Graphics.FillRectangle(darkLayer, ClientRectangle);
            }
        }
    }
}

注意事项

  • 原代码存在语法错误:Main方法的参数应为static void Main(string[] args),而非static void Main(string[]) args
  • 两种方案都需要确保窗体全屏覆盖桌面且置顶,避免被其他窗口遮挡
  • 方案一依赖系统DWM服务,若系统禁用DWM(如Windows 7经典模式)则失效,方案二则兼容性更好

内容的提问来源于stack exchange,提问作者Idad Wind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 06:20:22