Как настроить фон формы Windows таким же, как обои рабочего стола пользователя? - PullRequest
0 голосов
/ 17 марта 2019

Мне нужно установить фон формы Windows на текущие обои рабочего стола пользователя. Как мне это сделать в C #? Спасибо

1 Ответ

4 голосов
/ 17 марта 2019

Вы можете попробовать следующий код.Я протестировал этот код на Windows 8, и он работает для меня:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StringFormatting
{
    public partial class WallpaperTest : Form
    {
        private const UInt32 SPI_GETDESKWALLPAPER = 0x73;
        private const int MAX_PATH = 260;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(UInt32 uAction, int uParam, string lpvParam, int fuWinIni);

        public WallpaperTest()
        {
            InitializeComponent();
            this.BackgroundImage = GetCurrentDesktopWallpaper();
            this.BackgroundImageLayout = ImageLayout.Stretch; 
        }

        public Image GetCurrentDesktopWallpaper()
        {
            string currentWallpaper = new string('\0', MAX_PATH);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, currentWallpaper.Length, currentWallpaper, 0);
            string imageAddress = currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
            return Image.FromFile(imageAddress); 
        }


    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...