How to change desktop background in vb.net

Windows API are the dlls that are the part of microsoft windows operation system

There are many task in the application development that are very difficult to handle in your own code   for example  if we have need to change background of your desktop via your application than what you  do? Simple you can use User32 windows api for that. .Net framework use plateform invocation services for it.plateform invocation services is the process by which .net framework  communicate unmanaged code of api.


In this article we will discuss how to use User32 windows api for set the background of desktop or desktop wallpaper.

First design windows form like this:

form1-in-windows 8.jpg
 

 

User32 dll has a function 'SystemParametersInfo', and we can use this function for set and get all the settings controlled by the Windows Control Panel.

Normally a user would have to choose the   Windows Control  Panel to view or change system settings such as granularity, wallpaper, or icon title wrap. And we will use this function for set the desktop background…

 

   Code:

   Define function SystemParametersInfo:

 

    Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer

    'Declare two constant

    Private Const SETDESKWALLPAPER = 20

    Private Const UPDATEINIFILE = &H1

 

code for showing preview in picture box:
 

    Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click

        Dim dlg As New OpenFileDialog

        dlg.Title = "Choose Desktop background image"

        dlg.Filter = ""

        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

            TextBoxLocation.Text = dlg.FileName

            PictureBox1.BackgroundImage = Image.FromFile(dlg.FileName)

            PictureBox1.BackgroundImageLayout = ImageLayout.Stretch

        End If

 

    End Sub

 

Function call :


    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click

        SystemParametersInfo(SETDESKWALLPAPER, 0, TextBoxLocation.Text, UPDATEINIFILE)

 

    End Sub

Up Next
    Ebook Download
    View all
    Learn
    View all