/// <summary> /// Sets the current wallpaper with the image at it's designated path. Can cause issues/black wallpaper if path incorrect/not set. /// </summary> public void SetImage() { Natives.SystemParametersInfo((uint)Natives.SPIWALL.SET, 0, GetCurrentWallpaperPath(), Natives.SPIF.SPIF_UPDATEINIFILE | Natives.SPIF.SPIF_SENDCHANGE); }
private void button1_Click(object sender, EventArgs e) { var workerw = GetHandle(); string gifPath = ""; //Select a gif file to be placed in the picture box. using (OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Gif files (*.gif)|*.gif", Title = "Open gif file", AutoUpgradeEnabled = false }) { if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } try { //Set variables to match the data returned by the selected image gifPath = openFileDialog.FileName; //Set the value in the picturebox as a preview of the selected image pictureBox1.ImageLocation = gifPath; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } } // Create a new background form var form = new BackgroundForm(); //Fetch the selected monitor int monitorId = int.Parse(numericUpDown1.Value.ToString()); //Attempt to remove previously allocated items from that monitor if (Forms.TryGetValue(monitorId, out var currentForm)) { currentForm.Form.Close(); Forms.Remove(monitorId); } Forms.Add(monitorId, form); // Set the background forum to be borderless form.Form.FormBorderStyle = FormBorderStyle.None; form.Form.Text = "PassiveWallpaper"; Screen selectedScreen = Screen.AllScreens[monitorId]; //On the loading of the background form set the new properties form.Form.Load += (s, e2) => { //Ensure the form takes up the entire screen size of the selected monitor form.Form.Width = selectedScreen.Bounds.Width; form.Form.Height = selectedScreen.Bounds.Height; form.Form.Left = selectedScreen.Bounds.X; form.Form.Top = selectedScreen.Bounds.Y; //Create a new picturebox that fills the form and set it's image contents PictureBox pic = new PictureBox { Width = form.Form.Width, Height = form.Form.Height, Left = 0, Top = 0, Name = "PictureBox", ImageLocation = gifPath, SizeMode = PictureBoxSizeMode.StretchImage }; if (transparent.Checked) { //Set the image to be transparent backgrounded and set it's background image to be the current desktop background. pic.BackColor = Color.Transparent; pic.BackgroundImage = new Bitmap(GetCurrentWallpaperPath()); } else { pic.BackColor = Color.White; pic.BackgroundImage = null; } //Add the picturebox to the form form.Form.Controls.Add(pic); //Ensure the parent of the new form is the WorkerW process that was spawned between the desktop wallpaper and icons Natives.SetParent(form.Form.Handle, workerw); }; //Ensure that the closing event of the form is set in order to re-set the current desktop wallpaper and 'refresh' it, removing any artifacts form.Form.Closing += (s2, a2) => SetImage(); //Finally display the new form to the user. form.Form.Show(); }