【问题标题】:Call Winforms ControlPaint.Light() in WPF project在 WPF 项目中调用 Winforms ControlPaint.Light()
【发布时间】:2014-03-24 14:10:00
【问题描述】:

我有一个 Brush 对象,我想使用 Windows 窗体 ControlPaint.Light() 方法来减轻它。

我想将 System.Windows.Media.Brush 对象转换为 System.Drawing.Color 以便在单击按钮时将任何 XAML 按钮背景颜色更改为其“浅色”,因为 System.Windows.Forms.ControlPaint.Light() 仅将颜色作为参数。

由于我的Brush 对象不是SolidColorBrush,因此没有Color 属性,是否有其他方法可以实现这一目标?

【问题讨论】:

    标签: c# wpf winforms xaml colors


    【解决方案1】:

    试试这个:

            Brush brush = Brushes.Yellow;
            Color color = ((SolidColorBrush) brush).Color;
    

    【讨论】:

      【解决方案2】:

      您可以尝试获取画笔的 A,RGB 值,然后将它们传递给System.Drawing.Color.FromARGB() Pseudo-Code:

      Brush br = Brushes.Green;
      byte a = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).A;
      byte g = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).G;
      byte r = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).R;
      byte b = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).B;
      System.Windows.Forms.ControlPaint.Light(
          System.Drawing.Color.FromArgb((int)a,(int)r,(int)g,(int)b));
      

      我不是 WPF 专家,但我认为您需要牢记的主要事情是使用System.Drawing.Color.FromArgb() 或什至System.Drawing.Color.FromName() 进行尝试的最简单方法。

      【讨论】:

        【解决方案3】:

        你不需要引用巨大的Windows.Forms dll 只是来减轻Color。用最简单的术语来说,您只是将每个值乘以相同的因子:

        private Color AdjustBrightness(double brightnessFactor)
        {
            Color originalColour = Color.Red;
            Color adjustedColour = Color.FromArgb(originalColour.A, 
                (int)(originalColour.R * brightnessFactor), 
                (int)(originalColour.G * brightnessFactor), 
                (int)(originalColour.B * brightnessFactor));
            return adjustedColour;
        }
        

        这当然可以通过多种方式(并且应该)进行改进,但你明白了。事实上,如果一个值超过 255,这将抛出一个 Exception,但我相信你可以解决这个问题。现在你只需要检查你需要点亮什么类型的Brush

        if (brush is SolidColorBrush) 
            return new SolidColorBrush(AdjustBrightness(((SolidColorBrush)brush).Color));
        else if (brush is LinearGradientBrush || brush is RadialGradientBrush) 
        {
            // Go through each `GradientStop` in the `Brush` and brighten its colour
        }
        

        【讨论】:

          【解决方案4】:

          就我而言,我是这样做的(扩展方法):

          public static class BrushExtension
          {
          
              public static Color GetColor(this Brush brush)
              {
                  return new Pen(brush).Color;
              }
          
          }
          

          并称它为Color brushColor = myBrush.GetColor();

          【讨论】:

            【解决方案5】:

            如果需要将 WPF System.Windows.Media.Brush 转换为 System.Drawing.Color

            public static System.Drawing.Color BrushToColor(System.Windows.Media.Brush brush)
            {
                var col = ((SolidColorBrush) brush).Color;
                return System.Drawing.Color.FromArgb(col.A, col.R, col.G, col.B);
            }
            

            【讨论】:

            猜你喜欢
            • 2014-11-07
            • 2011-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-21
            相关资源
            最近更新 更多