ARTICLE

How to change desktop background in vb.net

Posted by Hirendra Sisodiya Articles | Visual Basic 2010 February 08, 2011
in this article we will discuss how to use User32 windows api for set the background of desktop or desktop wallpaper.
 
Reader Level:

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.


I
n 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:



1.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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor