diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index 932263c..73f18d4 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -620,6 +620,19 @@ namespace DisplayMagician { throw new ApplyPathInfoException("Program/ApplyProfile: ApplyWindowsDisplayPathInfo: Error configuring the Windows Display Devices"); } + // If the applying path info worked, then we attempt to set the desktop background + if (profile.SetWallpaper) + { + if (Wallpaper.Set(profile.SavedProfileIconCacheFilename,profile.WallpaperStyle)) + { + logger.Trace($"Program/ApplyProfile: We attempted to set the desktop wallpaper to {profile.SavedProfileIconCacheFilename} using {profile.WallpaperStyle} style for profile {profile.Name}, and it worked!"); + } + else + { + logger.Warn($"Program/ApplyProfile: We attempted to set the desktop wallpaper to {profile.SavedProfileIconCacheFilename} using {profile.WallpaperStyle} style for profile {profile.Name}, and it failed :("); + } + } + }); // Set up the UI forms to show diff --git a/DisplayMagician/UIForms/DisplayProfileForm.cs b/DisplayMagician/UIForms/DisplayProfileForm.cs index 273d6d5..23cfa3a 100644 --- a/DisplayMagician/UIForms/DisplayProfileForm.cs +++ b/DisplayMagician/UIForms/DisplayProfileForm.cs @@ -539,8 +539,9 @@ namespace DisplayMagician.UIForms ProfileSettingsForm profileSettingsForm = new ProfileSettingsForm(); profileSettingsForm.Profile = _selectedProfile; profileSettingsForm.ShowDialog(this); - // If the profile was changed then save all the profiles - if (profileSettingsForm.ProfileSettingChanged) + // If the profile was previously saved and is now changed then save all the profiles + // otherwise we'll save it only when the user wants to save this profile. + if (_saveOrRenameMode == "rename" && profileSettingsForm.ProfileSettingChanged) { ProfileRepository.SaveProfiles(); } diff --git a/DisplayMagician/UIForms/ProfileSettingsForm.Designer.cs b/DisplayMagician/UIForms/ProfileSettingsForm.Designer.cs index 9306356..7e5b82b 100644 --- a/DisplayMagician/UIForms/ProfileSettingsForm.Designer.cs +++ b/DisplayMagician/UIForms/ProfileSettingsForm.Designer.cs @@ -36,7 +36,7 @@ namespace DisplayMagician.UIForms this.pb_wallpaper = new System.Windows.Forms.PictureBox(); this.btn_select = new System.Windows.Forms.Button(); this.cb_set_wallpaper = new System.Windows.Forms.CheckBox(); - this.label1 = new System.Windows.Forms.Label(); + this.lbl_style = new System.Windows.Forms.Label(); this.cmb_wallpaper_display_mode = new System.Windows.Forms.ComboBox(); this.gb_general.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pb_wallpaper)).BeginInit(); @@ -65,7 +65,7 @@ namespace DisplayMagician.UIForms this.gb_general.Controls.Add(this.pb_wallpaper); this.gb_general.Controls.Add(this.btn_select); this.gb_general.Controls.Add(this.cb_set_wallpaper); - this.gb_general.Controls.Add(this.label1); + this.gb_general.Controls.Add(this.lbl_style); this.gb_general.Controls.Add(this.cmb_wallpaper_display_mode); this.gb_general.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.gb_general.ForeColor = System.Drawing.Color.White; @@ -79,6 +79,7 @@ namespace DisplayMagician.UIForms // btn_current // this.btn_current.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btn_current.Enabled = false; this.btn_current.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed; this.btn_current.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown; this.btn_current.FlatStyle = System.Windows.Forms.FlatStyle.Flat; @@ -91,10 +92,12 @@ namespace DisplayMagician.UIForms this.btn_current.TabIndex = 19; this.btn_current.Text = "&Use Current"; this.btn_current.UseVisualStyleBackColor = true; + this.btn_current.Click += new System.EventHandler(this.btn_current_Click); // // btn_clear // this.btn_clear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btn_clear.Enabled = false; this.btn_clear.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed; this.btn_clear.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown; this.btn_clear.FlatStyle = System.Windows.Forms.FlatStyle.Flat; @@ -107,12 +110,14 @@ namespace DisplayMagician.UIForms this.btn_clear.TabIndex = 18; this.btn_clear.Text = "&Clear"; this.btn_clear.UseVisualStyleBackColor = true; + this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click); // // pb_wallpaper // this.pb_wallpaper.BackColor = System.Drawing.Color.White; this.pb_wallpaper.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.pb_wallpaper.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.pb_wallpaper.Enabled = false; this.pb_wallpaper.Location = new System.Drawing.Point(28, 68); this.pb_wallpaper.Name = "pb_wallpaper"; this.pb_wallpaper.Size = new System.Drawing.Size(381, 212); @@ -123,6 +128,7 @@ namespace DisplayMagician.UIForms // btn_select // this.btn_select.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btn_select.Enabled = false; this.btn_select.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed; this.btn_select.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown; this.btn_select.FlatStyle = System.Windows.Forms.FlatStyle.Flat; @@ -146,25 +152,27 @@ namespace DisplayMagician.UIForms this.cb_set_wallpaper.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.cb_set_wallpaper.Location = new System.Drawing.Point(28, 36); this.cb_set_wallpaper.Name = "cb_set_wallpaper"; - this.cb_set_wallpaper.Size = new System.Drawing.Size(151, 20); + this.cb_set_wallpaper.Size = new System.Drawing.Size(333, 20); this.cb_set_wallpaper.TabIndex = 14; - this.cb_set_wallpaper.Text = "Apply this Wallpaper"; + this.cb_set_wallpaper.Text = "Apply this Wallpaper when using this Display Profile"; this.cb_set_wallpaper.UseVisualStyleBackColor = true; this.cb_set_wallpaper.CheckedChanged += new System.EventHandler(this.cb_set_wallpaper_CheckedChanged); // - // label1 + // lbl_style // - this.label1.AutoSize = true; - this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.label1.ForeColor = System.Drawing.Color.Transparent; - this.label1.Location = new System.Drawing.Point(176, 291); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(44, 16); - this.label1.TabIndex = 13; - this.label1.Text = "Style: "; + this.lbl_style.AutoSize = true; + this.lbl_style.Enabled = false; + this.lbl_style.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lbl_style.ForeColor = System.Drawing.Color.Transparent; + this.lbl_style.Location = new System.Drawing.Point(176, 291); + this.lbl_style.Name = "lbl_style"; + this.lbl_style.Size = new System.Drawing.Size(44, 16); + this.lbl_style.TabIndex = 13; + this.lbl_style.Text = "Style: "; // // cmb_wallpaper_display_mode // + this.cmb_wallpaper_display_mode.Enabled = false; this.cmb_wallpaper_display_mode.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.cmb_wallpaper_display_mode.FormattingEnabled = true; this.cmb_wallpaper_display_mode.Location = new System.Drawing.Point(226, 286); @@ -203,7 +211,7 @@ namespace DisplayMagician.UIForms private System.Windows.Forms.Button btn_back; private System.Windows.Forms.GroupBox gb_general; private System.Windows.Forms.CheckBox cb_set_wallpaper; - private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label lbl_style; private System.Windows.Forms.ComboBox cmb_wallpaper_display_mode; private System.Windows.Forms.Button btn_select; private System.Windows.Forms.Button btn_clear; diff --git a/DisplayMagician/UIForms/ProfileSettingsForm.cs b/DisplayMagician/UIForms/ProfileSettingsForm.cs index 9c54d56..aa00cba 100644 --- a/DisplayMagician/UIForms/ProfileSettingsForm.cs +++ b/DisplayMagician/UIForms/ProfileSettingsForm.cs @@ -1,4 +1,5 @@ using DisplayMagicianShared; +//using Microsoft.Win32; using NHotkey; using NHotkey.WindowsForms; using System; @@ -6,9 +7,7 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System.Linq; using System.Windows.Forms; -using WK.Libraries.BootMeUpNS; namespace DisplayMagician.UIForms { @@ -29,9 +28,12 @@ namespace DisplayMagician.UIForms InitializeComponent(); // Populate the Style dictionary - wallpaperStyleText.Add(Wallpaper.Style.Centered, "Center the Wallpaper"); - wallpaperStyleText.Add(Wallpaper.Style.Stretched, "Stretch the Wallpaper"); - wallpaperStyleText.Add(Wallpaper.Style.Tiled, "Tile the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Center, "Center the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Fill, "Fill the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Fit, "Fit the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Stretch, "Stretch the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Span, "Span the Wallpaper"); + wallpaperStyleText.Add(Wallpaper.Style.Tile, "Tile the Wallpaper"); cmb_wallpaper_display_mode.DisplayMember = "Value"; cmb_wallpaper_display_mode.ValueMember = "Text"; @@ -68,10 +70,10 @@ namespace DisplayMagician.UIForms { // Load the existing Wallpaper into the PictureBox //Read the contents of the file into a stream - //StreamReader streamReader = new StreamReader(Profile.WallpaperBitmapFilename); FileStream fileStream = new FileStream(Profile.WallpaperBitmapFilename,FileMode.Open); - wallpaperImage = new Bitmap(fileStream); + wallpaperImage = new Bitmap(fileStream); + fileStream.Close(); pb_wallpaper.Image = wallpaperImage; } } @@ -86,18 +88,44 @@ namespace DisplayMagician.UIForms { Profile.SetWallpaper = cb_set_wallpaper.Checked; Profile.WallpaperStyle = ((KeyValuePair)cmb_wallpaper_display_mode.SelectedItem).Key; - //Profile.WallpaperBitmapFilename = txt_wallpaper_filename.Text; } private void btn_back_Click(object sender, EventArgs e) { - wallpaperImage.Dispose(); + // Check that if there isn't an image, and yet apply this profile checkbox is selected, then we need to unselect it to stop an error state + if (cb_set_wallpaper.Checked == true && (Profile.WallpaperBitmapFilename == "" || Profile.WallpaperBitmapFilename == null)) + { + // We need to force turn off the application of the desktop wallpaper as it won't work + Profile.SetWallpaper = false; + Profile.WallpaperBitmapFilename = ""; + cb_set_wallpaper.Checked = false; + } this.Close(); } private void cb_set_wallpaper_CheckedChanged(object sender, EventArgs e) { _profileSettingChanged = true; + if (cb_set_wallpaper.Checked) + { + // Enable all the things + pb_wallpaper.Enabled = true; + btn_select.Enabled = true; + btn_current.Enabled = true; + btn_clear.Enabled = true; + lbl_style.Enabled = true; + cmb_wallpaper_display_mode.Enabled = true; + } + else + { + // Disable all the things + pb_wallpaper.Enabled = false; + btn_select.Enabled = false; + btn_current.Enabled = false; + btn_clear.Enabled = false; + lbl_style.Enabled = false; + cmb_wallpaper_display_mode.Enabled = false; + } } private void cmb_wallpaper_display_mode_SelectedIndexChanged(object sender, EventArgs e) @@ -115,7 +143,7 @@ namespace DisplayMagician.UIForms { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { - openFileDialog.InitialDirectory = "c:\\"; + openFileDialog.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString(); openFileDialog.Filter = "Image Files(*.bmp; *.jpg; *.gif; *.png; *.tiff)| *.bmp; *.jpg; *.gif; *.png; *.tiff | All files(*.*) | *.*"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; @@ -124,27 +152,126 @@ namespace DisplayMagician.UIForms { //Get the path of specified file filePath = openFileDialog.FileName; + wallpaperPath = Path.Combine(Program.AppWallpaperPath, $"wallpaper-{Profile.UUID}.jpg"); + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_select_wallpaper_Click: Storing desktop wallpaper {filePath} as {wallpaperPath} for use in profile {Profile.Name}"); - // If The user selected a photo then we need to set the set wallpaper to yes - cb_set_wallpaper.Checked = true; - //Read the contents of the file into a stream Stream fileStream = openFileDialog.OpenFile(); + // Create a bitmap wallpaperImage = new Bitmap(fileStream); - wallpaperPath = Path.Combine(Program.AppWallpaperPath, $"wallpaper-{Profile.UUID}.jpg"); + // Save a copy of the bitmap as PNG wallpaperImage.Save(wallpaperPath, ImageFormat.Png); + // Close the original file to free it up + fileStream.Close(); + + // Save the path of the saved wallpaper Profile.WallpaperBitmapFilename = wallpaperPath; + // Show the wallpaper image so that the user can decide to use it pb_wallpaper.Image = wallpaperImage; } } } + catch (ArgumentNullException ex) + { + SharedLogger.logger.Warn(ex, $"ProfileSettingsForm/btn_select_wallpaper_Click: Argument Null Exception while while storing desktop wallpaper in {wallpaperPath}"); + } + catch (System.Runtime.InteropServices.ExternalException ex) + { + SharedLogger.logger.Warn(ex, $"ProfileSettingsForm/btn_select_wallpaper_Click: External InteropServices Exception while while storing desktop wallpaper in {wallpaperPath}"); + } catch (Exception ex) { - // TODO: handle the exceptions + SharedLogger.logger.Warn(ex, $"ProfileSettingsForm/btn_select_wallpaper_Click: Exception while while storing desktop wallpaper in {wallpaperPath}"); + } + } + + private void btn_clear_Click(object sender, EventArgs e) + { + // clear the wallpaper sample picturebox + pb_wallpaper.Image = null; + + // delete the saved wallpaper item + try + { + if (File.Exists(Profile.WallpaperBitmapFilename)) + { + File.Delete(Profile.WallpaperBitmapFilename); + } + } + catch (Exception ex) + { + + } + + // Empty the file name in the Profile + Profile.WallpaperBitmapFilename = ""; + } + + private void btn_current_Click(object sender, EventArgs e) + { + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: User requested we get the current windows desktop wallpaper and add them to this display profile"); + // Check if there is a current desktop wallpaper + Microsoft.Win32.RegistryKey wallpaperKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); + string wallpaperLocation = (string)wallpaperKey.GetValue("WallPaper"); + if (wallpaperLocation == null || wallpaperLocation == String.Empty) + { + // There is no current desktop wallpaper to use + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: There is no existing desktop wallpaper for us to use!"); + MessageBox.Show("There isn't a desktop wallpaper currently being used in Windows, so we have nothing to use!","Cannot find Wallpaper",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); + } + else + { + // Grab the current desktop wallpaper and save that with this profile + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: There IS an existing desktop wallpaper for us to use!"); + // Figure out the stored filename we want + string savedWallpaperPath = Path.Combine(Program.AppWallpaperPath, $"wallpaper-{Profile.UUID}.jpg"); + // Try and grab the file from the location listed in the wallpaper key + if (File.Exists(wallpaperLocation)) + { + // If the file is there then great!, we can grab it and save a copy of it + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: The existing desktop wallpaper file {wallpaperLocation} exists"); + File.Copy(wallpaperLocation, savedWallpaperPath); + } + else + { + // If the file doesn't exist, then we need to try another way to get it. + // We try the themes folder first + //% appdata %\Microsoft\Windows\Themes and look for TrancodedWallpaper, and just shove a .jpg on it + wallpaperLocation = Path.Combine(Environment.SpecialFolder.ApplicationData.ToString(), @"Microsoft\Windows\Themes\TranscodedWallpaper"); + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: Now looking for the TranscodedWallpaper file in {wallpaperLocation}"); + if (!File.Exists(wallpaperLocation)) + { + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: The TranscodedWallpaper file does NOT exist in {wallpaperLocation}"); + wallpaperLocation = Path.Combine(Environment.SpecialFolder.ApplicationData.ToString(), @"Microsoft\Windows\Themes\CachedFiles\TranscodedWallpaper"); + if (!File.Exists(wallpaperLocation)) + { + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: The TranscodedWallpaper file does NOT exist in {wallpaperLocation} either!"); + return; + } + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: The TranscodedWallpaper file exists in {wallpaperLocation} (2nd try)"); + } + else + { + SharedLogger.logger.Trace($"ProfileSettingsForm/btn_current_Click: The TranscodedWallpaper file exists in {wallpaperLocation}"); + } + + } + + // Create an image from the wallpaper + wallpaperImage = new Bitmap(wallpaperLocation); + + // Save a copy of the bitmap as PNG + wallpaperImage.Save(savedWallpaperPath, ImageFormat.Png); + + // Save the path of the saved wallpaper + Profile.WallpaperBitmapFilename = savedWallpaperPath; + + // Show the wallpaper image so that the user can decide to use it + pb_wallpaper.Image = wallpaperImage; } } } diff --git a/DisplayMagicianShared/Wallpaper.cs b/DisplayMagicianShared/Wallpaper.cs index c2c8cdd..404d9bb 100644 --- a/DisplayMagicianShared/Wallpaper.cs +++ b/DisplayMagicianShared/Wallpaper.cs @@ -1,6 +1,7 @@ using Microsoft.Win32; using System; using System.Collections.Generic; +using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; @@ -22,41 +23,65 @@ namespace DisplayMagicianShared public enum Style : int { - Tiled, - Centered, - Stretched + Tile, + Center, + Stretch, + Fill, + Fit, + Span } - public static void SetAndSave(Uri uri, Style style, string filename) + public static bool Set(String filename, Style style) { - System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); + //System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); - System.Drawing.Image img = System.Drawing.Image.FromStream(s); - img.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp); + Bitmap img = new Bitmap(filename); RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); - if (style == Style.Stretched) + if (style == Style.Fill) + { + key.SetValue(@"WallpaperStyle", 10.ToString()); + key.SetValue(@"TileWallpaper", 0.ToString()); + } + if (style == Style.Fit) + { + key.SetValue(@"WallpaperStyle", 6.ToString()); + key.SetValue(@"TileWallpaper", 0.ToString()); + } + if (style == Style.Span) // Windows 8 or newer only! + { + key.SetValue(@"WallpaperStyle", 22.ToString()); + key.SetValue(@"TileWallpaper", 0.ToString()); + } + if (style == Style.Stretch) { key.SetValue(@"WallpaperStyle", 2.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } - - if (style == Style.Centered) + if (style == Style.Tile) { - key.SetValue(@"WallpaperStyle", 1.ToString()); + key.SetValue(@"WallpaperStyle", 0.ToString()); + key.SetValue(@"TileWallpaper", 1.ToString()); + } + if (style == Style.Center) + { + key.SetValue(@"WallpaperStyle", 0.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } - if (style == Style.Tiled) - { - key.SetValue(@"WallpaperStyle", 1.ToString()); - key.SetValue(@"TileWallpaper", 1.ToString()); - } - - SystemParametersInfo(SPI_SETDESKWALLPAPER, + if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, - SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); + SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE) > 0) + { + // applying desktop wallpaper worked! + return true; + } + else + { + // applying desktop wallpaper failed! + return false; + } } } }