Winform:允许用户动态改变应用的颜色



我是大型编程社区的新手,我的应用程序遇到了一个小问题。我希望用户能够点击一个按钮,并设置整个应用程序的颜色,就像一个主题。下面是它现在的样子,以及我尝试过的一些代码片段:

https://imgur.com/eKAga22

可以看到,左边使用color1的按钮(在Form1.cs中),甚至子表单中的按钮(Form_Settings.cs)都没有改变颜色。

Form_Settings.cs

private void trackBarR_Scroll(object sender, EventArgs e)
{
buttonRGB1.BackColor = Color.FromArgb(trackBarR1.Value, trackBarG1.Value, trackBarB1.Value);
}
// my click event not working
private void buttonRGB1_Click(object sender, EventArgs e)
{
ColorThemePicker.RGBColors.color1 = buttonRGB1.BackColor; // change variable from struct to the color set but doesn't work
ActiveForm.Update(); // I tried to update the form but this doesn't seem to work
}

ColorThemePicker.cs

public struct RGBColors
{
// RGBColors.color1
public static Color color1 = Color.FromArgb(173, 112, 133); // (Color 1) Very dark pink
}

我错过了什么,还是做错了什么?我想要改变的颜色的所有引用都设置为color1,例如这里的按钮在子表单(Form_Settings.cs):

Form_Settings.Designer.cs

this.color1.BackColor = RGBColors.color1; // here you can see it's set as the color1 from the struct and not the usual system colors
this.color1.FlatAppearance.BorderColor = System.Drawing.Color.Silver;
this.color1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.color1.Location = new System.Drawing.Point(407, 297);
this.color1.Name = "btn_color1";
this.color1.Size = new System.Drawing.Size(75, 67);
this.color1.TabIndex = 12;
this.color1.Text = "I'm Color1";
this.color1.UseVisualStyleBackColor = false;

谢谢你的帮助,我很高兴有任何关于如何进步的线索,甚至谷歌什么。

流程说明:

// 1. Initialize the color1 to a value
public static Color color1 = Color.FromArgb(173, 112, 133); // (Color 1) Very dark pink
// 2. Take the color from RGBColors and assign very dark pink value to color1
this.color1.BackColor = RGBColors.color1; // here you can see it's set as the color1 from the struct and not the usual system colors
// 3. Change the RGBColors.color1 from very dark pink to some other value           
ColorThemePicker.RGBColors.color1 = buttonRGB1.BackColor; // change variable from struct to the color set but doesn't work
// 4. Why would this assignment change the other variables value? (step 2)

现在想象这些是数字而不是颜色

  1. 给var1赋1
  2. 给var1赋值给var2。现在var2的值是1
  3. 将号码2分配给var2。var1的值改变了吗?不,还是1。

struct是c#中的值类型,所以同样的规则也适用。

如前所述,您应该更改特定控件的值以使此工作

最新更新