Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

WPF: Change desktop background, wallpaper

 

This C # wpf code example shows how to dynamically change the screen background of the desktop at runtime in a Windows application.

You can easily use the Windows system command: SystemParametersInfo

DllImport("user32.dll",…] SystemParametersInfo

 

Change screen background dynamically

 

The example shown alternates the 5 images from 1 to 5 cyclically.

 

Other changes

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace wpf_Desktop_Arranger

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        int iBackground = 0;

 

        private void btnSave_Click(object sender, RoutedEventArgs e)

        {

            //----------< btnSave_Click() >----------

            iBackground++;

            if (iBackground > 4) iBackground = 1;

            string sFile_FullPath = "C:\\_Daten\\Desktop\\Backgrounds\\desktop_Background_0" + iBackground  + ".jpg";

 

            //--< set desktop.background >--

            set_Desktop_Background(sFile_FullPath);

            //--</ set desktop.background >--

 

            //----------</ btnSave_Click() >----------

        }

 

        private void set_Desktop_Background(string sFile_FullPath)

        {

            //----------< btnSave_Click() >----------

            const int SET_DESKTOP_BACKGROUND = 20;

            const int UPDATE_INI_FILE = 1;

            const int SEND_WINDOWS_INI_CHANGE = 2;

           

            //--< set desktop.background >--

            win32.SystemParametersInfo(SET_DESKTOP_BACKGROUND, 0, sFile_FullPath, UPDATE_INI_FILE | SEND_WINDOWS_INI_CHANGE);

            //--</ set desktop.background >--

            //----------</ btnSave_Click() >----------

        }

 

        internal sealed class win32

        {

            //----------< win32 methods >----------

            //*windows stuff

 

            //----< .SystemParametersInfo() >----

            [DllImport("user32.dll", CharSet = CharSet.Auto)]

            internal static extern int SystemParametersInfo(

                int uAction,

                int uParam,

                String lpvParam,

                int fuWinIni);

            //----</ .SystemParametersInfo() >----

            //----------</ win32 methods >----------

        }

    }

}

 

 

 

Xaml code with button

<Window x:Class="wpf_Desktop_Arranger.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:wpf_Desktop_Arranger"

        mc:Ignorable="d"

        Title="WPF Set Desktop Background" Height="350" Width="525">

    <Grid>

        <Button x:Name="btnSave" VerticalAlignment="top" HorizontalAlignment="Left" Click="btnSave_Click" Margin="10,10,0,0" Height="44" Width="163" >set Desktop Backgroud</Button>

    </Grid>

</Window>

 

 

 

Other changes

 

desktop_Background_01.jpg

 

desktop_Background_02.jpg

 

Next screen backgrounds ..As a test

 

Mobile
»
WPF: Grid with variable cells
»
WPF: Multi-Column Multi-Row Grid with Cell Events
»
WPF: Change desktop background dynamically

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de