First working display background per profile

Added functionality to implement display wallpaper changes with each display profile as per #36 and #10. Tested in Windows 10 ok.
This commit is contained in:
Terry MacDonald 2021-09-02 10:42:26 +12:00
parent ee1d2e4717
commit 9cef45416e
5 changed files with 170 additions and 61 deletions

View File

@ -31,11 +31,13 @@ namespace DisplayMagician.UIForms
{
this.btn_back = new System.Windows.Forms.Button();
this.gb_general = new System.Windows.Forms.GroupBox();
this.rb_leave_wallpaper = new System.Windows.Forms.RadioButton();
this.rb_clear_wallpaper = new System.Windows.Forms.RadioButton();
this.rb_apply_wallpaper = new System.Windows.Forms.RadioButton();
this.btn_current = new System.Windows.Forms.Button();
this.btn_clear = new System.Windows.Forms.Button();
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.lbl_style = new System.Windows.Forms.Label();
this.cmb_wallpaper_display_mode = new System.Windows.Forms.ComboBox();
this.gb_general.SuspendLayout();
@ -50,7 +52,7 @@ namespace DisplayMagician.UIForms
this.btn_back.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown;
this.btn_back.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_back.ForeColor = System.Drawing.Color.White;
this.btn_back.Location = new System.Drawing.Point(476, 380);
this.btn_back.Location = new System.Drawing.Point(476, 436);
this.btn_back.Name = "btn_back";
this.btn_back.Size = new System.Drawing.Size(75, 23);
this.btn_back.TabIndex = 9;
@ -60,22 +62,59 @@ namespace DisplayMagician.UIForms
//
// gb_general
//
this.gb_general.Controls.Add(this.rb_leave_wallpaper);
this.gb_general.Controls.Add(this.rb_clear_wallpaper);
this.gb_general.Controls.Add(this.rb_apply_wallpaper);
this.gb_general.Controls.Add(this.btn_current);
this.gb_general.Controls.Add(this.btn_clear);
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.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;
this.gb_general.Location = new System.Drawing.Point(27, 21);
this.gb_general.Location = new System.Drawing.Point(27, 27);
this.gb_general.Name = "gb_general";
this.gb_general.Size = new System.Drawing.Size(525, 337);
this.gb_general.Size = new System.Drawing.Size(525, 381);
this.gb_general.TabIndex = 11;
this.gb_general.TabStop = false;
this.gb_general.Text = "Wallpaper Settings";
//
// rb_leave_wallpaper
//
this.rb_leave_wallpaper.AutoSize = true;
this.rb_leave_wallpaper.Checked = true;
this.rb_leave_wallpaper.Location = new System.Drawing.Point(28, 30);
this.rb_leave_wallpaper.Name = "rb_leave_wallpaper";
this.rb_leave_wallpaper.Size = new System.Drawing.Size(146, 20);
this.rb_leave_wallpaper.TabIndex = 22;
this.rb_leave_wallpaper.TabStop = true;
this.rb_leave_wallpaper.Text = "Do Nothing (Default)";
this.rb_leave_wallpaper.UseVisualStyleBackColor = true;
this.rb_leave_wallpaper.CheckedChanged += new System.EventHandler(this.rb_leave_wallpaper_CheckedChanged);
//
// rb_clear_wallpaper
//
this.rb_clear_wallpaper.AutoSize = true;
this.rb_clear_wallpaper.Location = new System.Drawing.Point(28, 56);
this.rb_clear_wallpaper.Name = "rb_clear_wallpaper";
this.rb_clear_wallpaper.Size = new System.Drawing.Size(381, 20);
this.rb_clear_wallpaper.TabIndex = 21;
this.rb_clear_wallpaper.Text = "Clear the Desktop Wallpaper when using this Display Profile";
this.rb_clear_wallpaper.UseVisualStyleBackColor = true;
this.rb_clear_wallpaper.CheckedChanged += new System.EventHandler(this.rb_clear_wallpaper_CheckedChanged);
//
// rb_apply_wallpaper
//
this.rb_apply_wallpaper.AutoSize = true;
this.rb_apply_wallpaper.Location = new System.Drawing.Point(28, 82);
this.rb_apply_wallpaper.Name = "rb_apply_wallpaper";
this.rb_apply_wallpaper.Size = new System.Drawing.Size(386, 20);
this.rb_apply_wallpaper.TabIndex = 20;
this.rb_apply_wallpaper.Text = "Apply this Desktop Wallpaper when using this Display Profile";
this.rb_apply_wallpaper.UseVisualStyleBackColor = true;
this.rb_apply_wallpaper.CheckedChanged += new System.EventHandler(this.rb_apply_wallpaper_CheckedChanged);
//
// btn_current
//
this.btn_current.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
@ -86,7 +125,7 @@ namespace DisplayMagician.UIForms
this.btn_current.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_current.ForeColor = System.Drawing.Color.White;
this.btn_current.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btn_current.Location = new System.Drawing.Point(417, 158);
this.btn_current.Location = new System.Drawing.Point(417, 201);
this.btn_current.Name = "btn_current";
this.btn_current.Size = new System.Drawing.Size(75, 23);
this.btn_current.TabIndex = 19;
@ -104,7 +143,7 @@ namespace DisplayMagician.UIForms
this.btn_clear.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_clear.ForeColor = System.Drawing.Color.White;
this.btn_clear.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btn_clear.Location = new System.Drawing.Point(417, 187);
this.btn_clear.Location = new System.Drawing.Point(417, 230);
this.btn_clear.Name = "btn_clear";
this.btn_clear.Size = new System.Drawing.Size(75, 23);
this.btn_clear.TabIndex = 18;
@ -118,7 +157,7 @@ namespace DisplayMagician.UIForms
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.Location = new System.Drawing.Point(28, 111);
this.pb_wallpaper.Name = "pb_wallpaper";
this.pb_wallpaper.Size = new System.Drawing.Size(381, 212);
this.pb_wallpaper.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
@ -135,7 +174,7 @@ namespace DisplayMagician.UIForms
this.btn_select.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_select.ForeColor = System.Drawing.Color.White;
this.btn_select.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btn_select.Location = new System.Drawing.Point(417, 129);
this.btn_select.Location = new System.Drawing.Point(417, 172);
this.btn_select.Name = "btn_select";
this.btn_select.Size = new System.Drawing.Size(75, 23);
this.btn_select.TabIndex = 16;
@ -143,28 +182,13 @@ namespace DisplayMagician.UIForms
this.btn_select.UseVisualStyleBackColor = true;
this.btn_select.Click += new System.EventHandler(this.btn_select_wallpaper_Click);
//
// cb_set_wallpaper
//
this.cb_set_wallpaper.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.cb_set_wallpaper.AutoSize = true;
this.cb_set_wallpaper.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F);
this.cb_set_wallpaper.ForeColor = System.Drawing.Color.White;
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(333, 20);
this.cb_set_wallpaper.TabIndex = 14;
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);
//
// lbl_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.Location = new System.Drawing.Point(176, 334);
this.lbl_style.Name = "lbl_style";
this.lbl_style.Size = new System.Drawing.Size(44, 16);
this.lbl_style.TabIndex = 13;
@ -175,7 +199,7 @@ namespace DisplayMagician.UIForms
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);
this.cmb_wallpaper_display_mode.Location = new System.Drawing.Point(226, 329);
this.cmb_wallpaper_display_mode.Name = "cmb_wallpaper_display_mode";
this.cmb_wallpaper_display_mode.Size = new System.Drawing.Size(183, 24);
this.cmb_wallpaper_display_mode.TabIndex = 12;
@ -186,7 +210,7 @@ namespace DisplayMagician.UIForms
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(580, 427);
this.ClientSize = new System.Drawing.Size(580, 483);
this.Controls.Add(this.gb_general);
this.Controls.Add(this.btn_back);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
@ -210,12 +234,14 @@ namespace DisplayMagician.UIForms
#endregion
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 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;
private System.Windows.Forms.PictureBox pb_wallpaper;
private System.Windows.Forms.Button btn_current;
private System.Windows.Forms.RadioButton rb_leave_wallpaper;
private System.Windows.Forms.RadioButton rb_clear_wallpaper;
private System.Windows.Forms.RadioButton rb_apply_wallpaper;
}
}

View File

@ -28,12 +28,12 @@ namespace DisplayMagician.UIForms
InitializeComponent();
// Populate the Style dictionary
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");
wallpaperStyleText.Add(Wallpaper.Style.Center, "Center the Wallpaper");
wallpaperStyleText.Add(Wallpaper.Style.Span, "Span the Wallpaper");
cmb_wallpaper_display_mode.DisplayMember = "Value";
cmb_wallpaper_display_mode.ValueMember = "Text";
@ -61,10 +61,10 @@ namespace DisplayMagician.UIForms
private void ProfileSettingsForm_Load(object sender, EventArgs e)
{
if (Profile.SetWallpaper)
if (Profile.WallpaperMode.Equals(Wallpaper.Mode.Apply))
{
logger.Info($"ProfileSettingsForm/ProfileSettingsForm_Load: Profile {Profile.Name} has loaded with Set Wallpaper enabled and Wallpaper Style {Profile.WallpaperStyle.ToString("G")} and Wallpaper Filename of {Profile.WallpaperBitmapFilename}.");
cb_set_wallpaper.Checked = true;
logger.Info($"ProfileSettingsForm/ProfileSettingsForm_Load: Profile {Profile.Name} has loaded with Apply Wallpaper enabled and Wallpaper Style {Profile.WallpaperStyle.ToString("G")} and Wallpaper Filename of {Profile.WallpaperBitmapFilename}.");
rb_apply_wallpaper.Checked = true;
cmb_wallpaper_display_mode.SelectedIndex = cmb_wallpaper_display_mode.FindStringExact(wallpaperStyleText[Profile.WallpaperStyle]);
if (Profile.WallpaperBitmapFilename != "" && File.Exists(Profile.WallpaperBitmapFilename))
{
@ -77,36 +77,55 @@ namespace DisplayMagician.UIForms
pb_wallpaper.Image = wallpaperImage;
}
}
else if (Profile.WallpaperMode.Equals(Wallpaper.Mode.Clear))
{
logger.Info($"ProfileSettingsForm/ProfileSettingsForm_Load: Profile {Profile.Name} has loaded with Clear Wallpaper enabled.");
rb_clear_wallpaper.Checked = true;
}
else
{
cb_set_wallpaper.Checked = false;
logger.Info($"ProfileSettingsForm/ProfileSettingsForm_Load: Profile {Profile.Name} is set to do nothing.");
rb_leave_wallpaper.Checked = true;
cmb_wallpaper_display_mode.SelectedIndex = 0;
}
}
private void ProfileSettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
Profile.SetWallpaper = cb_set_wallpaper.Checked;
if (rb_apply_wallpaper.Checked)
{
Profile.WallpaperMode = Wallpaper.Mode.Apply;
}
else if (rb_clear_wallpaper.Checked)
{
Profile.WallpaperMode = Wallpaper.Mode.Clear;
}
else
{
Profile.WallpaperMode = Wallpaper.Mode.DoNothing;
}
Profile.WallpaperStyle = ((KeyValuePair<Wallpaper.Style, string>)cmb_wallpaper_display_mode.SelectedItem).Key;
}
private void btn_back_Click(object sender, EventArgs e)
{
// 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))
if (rb_apply_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.WallpaperMode = Wallpaper.Mode.DoNothing;
Profile.WallpaperBitmapFilename = "";
cb_set_wallpaper.Checked = false;
rb_apply_wallpaper.Checked = false;
}
this.Close();
}
private void cb_set_wallpaper_CheckedChanged(object sender, EventArgs e)
private void rb_apply_wallpaper_CheckedChanged(object sender, EventArgs e)
{
_profileSettingChanged = true;
if (cb_set_wallpaper.Checked)
if (rb_apply_wallpaper.Checked)
{
// Enable all the things
pb_wallpaper.Enabled = true;
@ -116,16 +135,6 @@ namespace DisplayMagician.UIForms
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)
@ -274,5 +283,37 @@ namespace DisplayMagician.UIForms
pb_wallpaper.Image = wallpaperImage;
}
}
private void rb_clear_wallpaper_CheckedChanged(object sender, EventArgs e)
{
_profileSettingChanged = true;
if (rb_clear_wallpaper.Checked)
{
// 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 rb_leave_wallpaper_CheckedChanged(object sender, EventArgs e)
{
_profileSettingChanged = true;
if (rb_leave_wallpaper.Checked)
{
// 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;
}
}
}
}

View File

@ -236,7 +236,7 @@ namespace DisplayMagicianShared
public string SavedProfileIconCacheFilename { get; set; }
public bool SetWallpaper { get; set; }
public Wallpaper.Mode WallpaperMode { get; set; }
public Wallpaper.Style WallpaperStyle { get; set; }
@ -356,7 +356,7 @@ namespace DisplayMagicianShared
profile.ProfileBitmap = ProfileBitmap;
profile.ProfileTightestBitmap = ProfileTightestBitmap;
profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers;
profile.SetWallpaper = SetWallpaper;
profile.WallpaperMode = WallpaperMode;
profile.WallpaperBitmapFilename = WallpaperBitmapFilename;
profile.WallpaperStyle = WallpaperStyle;
return true;

View File

@ -1051,10 +1051,10 @@ namespace DisplayMagicianShared
}
finally
{
// If the applying path info worked, then we attempt to set the desktop background
if (profile.SetWallpaper)
// If the applying path info worked, then we attempt to set the desktop background if needed
if (profile.WallpaperMode.Equals(Wallpaper.Mode.Apply) && !String.IsNullOrWhiteSpace(profile.WallpaperBitmapFilename))
{
if (Wallpaper.Set(profile.SavedProfileIconCacheFilename, profile.WallpaperStyle))
if (Wallpaper.Set(profile.WallpaperBitmapFilename, profile.WallpaperStyle))
{
SharedLogger.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!");
}
@ -1063,6 +1063,17 @@ namespace DisplayMagicianShared
SharedLogger.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 :(");
}
}
else if (profile.WallpaperMode.Equals(Wallpaper.Mode.Clear))
{
if (Wallpaper.Clear())
{
SharedLogger.logger.Trace($"Program/ApplyProfile: We attempted to clear the desktop wallpaper and it worked!");
}
else
{
SharedLogger.logger.Warn($"Program/ApplyProfile: We attempted to clear the desktop wallpaper and it failed :(");
}
}
// We stop the stop watch
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.

View File

@ -23,12 +23,19 @@ namespace DisplayMagicianShared
public enum Style : int
{
Tile,
Center,
Stretch,
Fill,
Fit,
Span
Fill = 0,
Fit = 1,
Stretch = 2,
Tile = 3,
Center = 4,
Span = 5
}
public enum Mode : int
{
DoNothing = 0,
Clear = 1,
Apply = 2
}
public static bool Set(String filename, Style style)
@ -83,5 +90,29 @@ namespace DisplayMagicianShared
return false;
}
}
public static bool Clear()
{
RegistryKey desktopKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
desktopKey.SetValue(@"WallPaper", "");
RegistryKey explorerKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers", true);
explorerKey.SetValue(@"BackgroundType", 1);
if (SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
"",
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE) > 0)
{
// applying desktop wallpaper worked!
return true;
}
else
{
// applying desktop wallpaper failed!
return false;
}
}
}
}