如何获得控件的前景色?

时间:2013-08-11 16:07:32

标签: c# colors get label

如何获得控件的颜色,例如标签或矩形?我需要做这样的事情:

if (label.foreground == #FFFFFF)
    Messagebox.Show("Branco!").

有一段时间,我用这个来解决我的问题:

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromRgb(255, 255, 255);
if (rec.Fill == mySolidColorBrush)
    MessageBox.Show("Branco!");

以下是我在尝试已发布的答案时遇到的错误:

enter image description here


enter image description here


enter image description here

3 个答案:

答案 0 :(得分:0)

现在我知道你正在使用WPF,这里是如何设置标签上的颜色。使用Foreground属性并将其设置为有效的Brushes值。

if (label.Foreground = Brushes.White)
    MessageBox.Show("Branco!");

与矩形相同:

var rect = new Rectangle();
rect.Fill = Brushes.Green;

答案 1 :(得分:0)

if (label.ForeColor == System.Drawing.Color.Black) 
{
    // Do something here
}

答案 2 :(得分:0)

根据MSDNForeColor的类型为System.Drawing.Color,这意味着您需要将元素颜色与System.Drawing.SystemColors

中的预定义颜色进行比较

编辑

请检查这些问题 Changing background color of the form with hexadecimal code
convert hex code to color name

尝试这样的事情

if (label.ForeColor == Color.FromArgb(0xFFFFFF)) Messagebox.Show("Branco!");