Changing desktop wallpaper

allstar

Member
Joined
Sep 9, 2004
Messages
5
Location
Bristol, UK
Ive been searching the forum and Ive cobbled together the following code:

Code:
Imports Microsoft.Win32
Imports System

Public Class Form1
    Inherits System.Windows.Forms.Form
    Declare Function SystemParametersInfo Lib "User32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As String, ByVal fuWinIni As Long) As Long
    Private Const SPI_SETDESKWALLPAPER As Integer = 20
    Private Const SPIF_UPDATEINIFILE As Integer = &H1

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SystemParametersInfo _(SPI_SETDESKWALLPAPER,0, "C:\boxes_1024.bmp", SPIF_UPDATEINIFILE)
    End Sub
End Class

But it doesnt seem to do anything, can anyone tell me why?
 
You probably need to change the longs to integers with .Net and IIRC you also need to set the final parameter to SPIF_SENDCHANGE.

Try the following - it should work.

Code:
 Declare Function SystemParametersInfo Lib "User32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Long

    Private Const SPI_SETDESKWALLPAPER As Integer = 20
    Private Const SPIF_SENDCHANGE As Integer = &H2

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "C:\windows\Coffee Bean.bmp", SPIF_SENDCHANGE)
    End Sub
 
Thats great that fixed it straight away. Thanks.

I have another question though...

If I wanted to change the desktop wallpaper to a web page for example, so it would have to be an active desktop, how could I go about it?

Thats my ultimate goal, what I posted above is the first thing I have found that approaches what I want to do.
 
Yeah, Ive tried that, but the code he gives doesnt allow me to put an html page as the desktop background. Just jpgs and bmps. Ill try playing with the code though.
 
you would first need to create a bitmap / some sort of image of the html page , then you can use the above mentioned methods of setting as desktop background.
heres an example i built a long time ago ( been digging through my cd archives ) its based on the screenshot application i did in C++ to capture the mouse but thats another story .
the form ( Form1 ) ...
C#:
[COLOR=Green]/// in a Form with 2 buttons ...[/COLOR]
	[COLOR=Gray]/// <summary>
	///[/COLOR] [COLOR=Green]the code to use the class ( in this case its button clicks in Form1 )[/COLOR]		
[COLOR=Gray]	/// </summary>[/COLOR]		
            private void button1_Click(object sender, System.EventArgs e)
		{
			Process.Start("IEXPLORE.EXE" , textBox1.Text ); /// textBox1.Text contains a valid url to snapshot.
			button1.Enabled = false;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Visible=false;
			while(this.Visible)
			{
				Application.DoEvents();
			}
			System.Threading.Thread.Sleep(75); /// give the form chance to hide.

			_win32 bmpShot = new _win32(); /// the snapshot takes place upon creating this class.
			bmpShot.bmp.Save( @"C:\imgWeb.bmp" ); /// in this case i save the image as a bitmap to the C drive.
			button1.Enabled = true;
			this.Visible=true;
		}
the class that takes the html screen and makes an image of it...
C#:
[COLOR=Gray]/// <summary>
///[/COLOR] [COLOR=Green]the class that performs the snapshot of your html page.[/COLOR]
[COLOR=Gray]/// </summary>[/COLOR]
[COLOR=Blue]public class[/COLOR] _win32
{
	[COLOR=Blue]#region[/COLOR] " Api Calls "

	[COLOR=Blue]private const int[/COLOR] SRCCOPY = 0xCC0020;
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr BitBlt (IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr CreateCompatibleDC (IntPtr hdc);
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr DeleteDC (IntPtr hdc);
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr DeleteObject (IntPtr hObject);
	[DllImport("user32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr GetDC (IntPtr hwnd);
	[DllImport("user32.dll")]
	[COLOR=Blue]private static extern int[/COLOR] GetWindowRect (IntPtr hwnd, ref Rectangle lpRect);
	[DllImport("user32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr ReleaseDC (IntPtr hwnd, IntPtr hdc);
	[DllImport("gdi32.dll")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr SelectObject (IntPtr hdc, IntPtr hObject);
	[DllImport("user32.dll", EntryPoint="FindWindowA")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr FindWindow (string lpClassName, string lpWindowName);
	[DllImport("user32.dll", EntryPoint="FindWindowExA")]
	[COLOR=Blue]private static extern[/COLOR] IntPtr FindWindowEx (IntPtr hWnd1,IntPtr hWnd2, string lpsz1, string lpsz2);
    
	[COLOR=Blue]#endregion[/COLOR]

	[COLOR=Blue]public[/COLOR] Rectangle clientRect=new Rectangle();
	[COLOR=Blue]public[/COLOR] Bitmap bmp;

	[COLOR=Blue]public[/COLOR] _win32()
	{
		IntPtr val = this.WebHandle();
		if(val!=IntPtr.Zero)
		{
			this.dcHandle( val );
		}
    }
	[COLOR=Blue]public[/COLOR] IntPtr WebHandle()
	{
		IntPtr hwnd = FindWindow( "IEFRAME" , null );
		IntPtr hwndSrc = IntPtr.Zero;
		if(hwnd != IntPtr.Zero)
		{
			hwndSrc = FindWindowEx( hwnd , IntPtr.Zero , "Shell DocObject View" , null );
			hwndSrc = FindWindowEx( hwndSrc , IntPtr.Zero , "InterNet Explorer_Server" , null );
		    GetWindowRect( hwndSrc , ref clientRect );
		}
		return hwndSrc;
	}

	[COLOR=Blue]public void[/COLOR] dcHandle(IntPtr hHandle)
	{
		IntPtr srcHdc = gDc( hHandle );

		IntPtr destHdc = CreateCompatibleDC( srcHdc );
		IntPtr bmpDc = CreateCompatibleBitmap( srcHdc , clientRect.Width , clientRect.Height - 145 );
		
		CreateImageCanvas( destHdc , srcHdc , bmpDc , clientRect , SRCCOPY );

		bmp = bMap( bmpDc ); /// create the bitmap to save.

		CleanDcs( hHandle , destHdc , srcHdc , bmpDc );
		return;
	}

	[COLOR=Blue]public[/COLOR] IntPtr gDc(IntPtr hHandle)
	{
		return GetDC( hHandle );
	}

	[COLOR=Blue]public void[/COLOR] CreateImageCanvas(IntPtr dest , IntPtr source , IntPtr bDc , Rectangle rct , int dwRop )
	{
		IntPtr objDc = SelectObject( dest , bDc );
		BitBlt( dest , 0 , 0 , clientRect.Width , clientRect.Height - 145 , source, 0 , 0 , SRCCOPY );
		SelectObject( dest , objDc );
		return;
	}

	[COLOR=Blue]public[/COLOR] Bitmap bMap(IntPtr b)
	{
		return Bitmap.FromHbitmap(b);
	}

	[COLOR=Blue]public void[/COLOR] CleanDcs(IntPtr hHandle , IntPtr dest , IntPtr source , IntPtr bDc )
	{
		ReleaseDC( hHandle , source );
		DeleteDC( dest );
		DeleteObject( bDc );
		return;
	}
}
hope it helps :)
 
Thanks dynamic, but that wont help me unfortunately.

I have a flash sequence embedded in an html page.

At the moment I can make it my desktop background by adding the html page as an active desktop. By doing a right click, properties etc.

Basically I want a way to automate this so that I can avoid having to go through all the windows menus.

I presume that I should be able to just change the relevent registry settings to get it to work, but I have several problems... mainly that I dont really know what Im doing.

Im more of a Java programmer than any other language. Work have had me start doing VB .Net but Im only scratching the surface with that. Obviously my Java experience means I can understand c# pretty well, but again I just dont know enough about the api to be confident to approach this project on my own.

Added to this is the fact that I have searched high and low on the Internet for information on how to do this and cant find anything, despite all the googling!

I think my next step is going to have to be buying a load of c# books and sitting down with them until I can do it myself!
 
Back
Top