Winform:允许用户动态更改应用程序的颜色

时间:2021-02-14 10:53:08

标签: c# winforms

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

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;

感谢您的帮助,我很乐意提供任何有关如何取得进展的线索,甚至是 google 的内容。

1 个答案:

答案 0 :(得分:0)

流程说明:

// 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. 将数字 1 分配给 var1
  2. 将 var1 值分配给 var2。现在 var2 值为 1
  3. 将数字 2 分配给 var2。 var1 值改变了吗?不,它仍然是 1。

结构在 c# 中是作为整数的值类型,因此适用相同的规则。

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

相关问题