pbj

pbj

  • NA
  • 2
  • 0

using SystemParametersInfo to change wallpaper

Oct 14 2003 8:46 PM
i am using c# (2003) on win2k and xp as administrator. the following code successfully changes the wallpaper. i am wondering why i have to set the wallpaper to "(None)" first? if i do not, it will not change. if i do, it will change. in either case, SystemParametersInfo returns no error. in my experience, when i have to include a seemingly unnecessary extra line of code to do something, it usually means that i did something wrong earlier. can anyone spot my error? using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Post { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private const int SPI_SETDESKWALLPAPER = 20; private const int SPIF_UPDATEINIFILE = 0x1; private const int SPIF_SENDWININICHANGE = 0x2; [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)] public static extern int SystemParametersInfo(int uAction , int uParam , string lpvParam , int fuWinIni); public Form1() { InitializeComponent(); } #region Windows Form Designer generated code ... (omitted) [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { // Why do I have to set it to none first to make it work??? SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "(None)", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE ); SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, @"C:\WINNT\Soap Bubbles.bmp", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE ); } } }