【问题标题】:WPF Color Picker ImplementationWPF 颜色选择器实现
【发布时间】:2013-06-13 14:14:47
【问题描述】:

我必须在我的 WPF 应用程序中创建一个颜色选择器。当我单击任何颜色时,该颜色的代码应该出现在文本框中。我用谷歌搜索了很多,但没有找到符合我要求的东西。如果您以前这样做过,请分享。

【问题讨论】:

  • 这个控件,如下所述,已经存在,所以最好使用它而不是尝试重新创建它,除非你有一些你无法解决的非常具体的问题。
  • “我搜索了,但没有一个是即插即用代码,我不想做任何实际的编程,所以可以帮我做吗?”
  • 建议在 NuGet 中搜索“wpf colorpicker”和“wpf color picker”以获得可能的解决方案。这里的答案很有用,但对于较新的平台可能会过时,或者可能不是免费的。

标签: c# wpf


【解决方案1】:

正如 Jodha 所说,您应该使用 WpfToolkit Extended 中的颜色选择器控件。实现颜色选择器控件很容易,只需执行以下操作:

把它放在你的 Window 对象中:

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

这就是你想要你的颜色选择器的任何地方。

<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged"></xctk:ColorPicker>

然后,您所要做的就是使用 SelectedColorChanged 事件来更改文本框中的文本,如下所示:

private void ClrPcker_Background_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
    TextBox.Text = "#" + ClrPcker_Background.SelectedColor.R.ToString() + ClrPcker_Background.SelectedColor.G.ToString() + ClrPcker_Background.SelectedColor.B.ToString();
}

希望这会有所帮助!

【讨论】:

  • 您还可以将ColorPicker.SelectedColorProperty 绑定到System.Windows.Media.Color 属性并将TextBox 绑定到该属性并使用Converter 将其更改为ARGB 值,这样您就可以使用TextBox 也可以更改颜色。
  • 出现错误:我的 xaml 文件中的 xmlns:xctk="schemas.xceed.com/wpf/xaml/toolkit" 中不存在 Colorpicker
  • @NareshKumar 是的,你真的下载并安装了工具包吗?
  • 是否可以通过单击菜单项或单击按钮来打开 ColorPicker 对话框?谢谢。
  • 好的,@Bob。元素命名和使用事件让这个 MVVM 死忠粉有点不舒服。
【解决方案2】:

您可以查看Color Picker ControlWpfToolKit Extended。这个工具包有很多有用的控件。

【讨论】:

  • 对于寻找此控件的人,该项目现已在 GitHub 上提供,地址为 github.com/xceedsoftware/wpftoolkit
  • 不幸的是,他们的许可证上写着:仅用于非商业用途。如果您想在商业上使用它,您必须购买订阅。
【解决方案3】:

最简单的方法是使用 WinForms 中使用的 ColorDialog

 System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog();
        if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            DrawBallColorBtn.Background = new SolidColorBrush(Color.FromArgb(colorDialog.Color.A, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B));
            _drawBallColor = colorDialog.Color.Name.ToLower();
        }

如果您的目标是 .NetCore 3.x,请将休闲添加到 .csproj 文件中

<PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms> 
</PropertyGroup>

【讨论】:

    【解决方案4】:

    看看ColorBox 在 codeplex 的控制。您还可以使用它创建线性和径向渐变。

    【讨论】:

      【解决方案5】:

      我认为您可以使用我编写的这段代码。 Here is the full code

      UserControl.xaml

      <StackPanel x:Name="PickerPanel" Orientation="Vertical" Background="White">
              <Grid HorizontalAlignment="Stretch" Height="210" MouseDown="RgbGradient_MouseDown">
                  <Grid.Background>
                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                          <GradientStop Color="Black" Offset="1"/>
                          <GradientStop Color="White"/>
                          <GradientStop x:Name="MiddleStop" Color="#FF787878" Offset="0.5"/>
                      </LinearGradientBrush>
                  </Grid.Background>
              </Grid>
              <Grid x:Name="SpectrumGrid" Height="40" MouseLeftButtonDown="SpectrumGrid_MouseLeftButtonDown">
                  
              </Grid>
              <TextBlock x:Name="HexCode" TextWrapping="Wrap"/>
      
          </StackPanel>
      

      UserControl.xaml.cs

          public partial class ColorPicker : UserControl
          {
              public RGB Selected = new RGB();
              private double currH = 360;
      
              public ColorPicker()
              {
                  InitializeComponent();
      
                  var g6 = HSV.GradientSpectrum();
      
                  LinearGradientBrush gradientBrush = new LinearGradientBrush();
                  gradientBrush.StartPoint = new Point(0, 0);
                  gradientBrush.EndPoint = new Point(1, 0);
                  for (int i = 0; i < g6.Length; i++) {
                      GradientStop stop = new GradientStop(g6[i].Color(), (i) * 0.16);
                      gradientBrush.GradientStops.Add(stop);
                  }
                  SpectrumGrid.Opacity = 1;
                  SpectrumGrid.Background = gradientBrush;
      
                  MiddleStop.Color = HSV.RGBFromHSV(0, 1f, 1f).Color();
              }
      
              private void RgbGradient_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
              {
                  if(e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                  {
                      var pos = e.GetPosition(sender as Canvas);
                      var x = pos.X;
                      var y = pos.Y;
                      RGB c; 
                      if (x < Width / 2)
                      {
                          c = HSV.RGBFromHSV(currH, 1f, x / (Width / 2));
                      }
                      else
                      {
                          c = HSV.RGBFromHSV(currH, (Width / 2 - (x - Width / 2)) / Width, 1f);
                      }
                      HexCode.Background = new SolidColorBrush(c.Color());
                      HexCode.Text = "#" + c.Hex();
                      Selected = c;
                  }
                  
              }
      
              private void SpectrumGrid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
              {
                  if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                  {
                      var x = e.GetPosition(SpectrumGrid).X;
                      SpectrumGridBar.Margin = new Thickness(x, 0, 0, 0);
                      currH = 360 * (x / this.Width);
                      Debug.WriteLine(currH);
                      MiddleStop.Color = HSV.RGBFromHSV(currH, 1f, 1f).Color();
                  }
              }
          }
      
          public class RGB
          {
              public byte R { get; set; }
              public byte G { get; set; }
              public byte B { get; set; }
              public RGB() 
              {
                  R = 0xff;
                  G = 0xff;
                  B = 0xff;
              }
              public RGB(double r, double g, double b)
              {
                  if (r > 255 || g > 255 || b > 255) throw new Exception("RGB must be under 255 (1byte)");
                  R = (byte)r;
                  G = (byte)g;
                  B = (byte)b;
              }
              public string Hex()
              {
                  return BitConverter.ToString(new byte[] { R, G, B }).Replace("-", string.Empty);
              }
              public Color Color()
              {
                  var color = new Color();
                  color.R = R;
                  color.G = G;
                  color.B = B;
                  color.A = 255;
                  return color;
              }
          }
          public static class HSV
          {
              public static RGB[] GetSpectrum()
              {
                  RGB[] rgbs = new RGB[360];
      
                  for (int h = 0; h < 360; h++)
                  {
                      rgbs[h] = RGBFromHSV(h, 1f, 1f);
                  }
                  return rgbs;
              }
              public static RGB[] GradientSpectrum()
              {
                  RGB[] rgbs = new RGB[7];
      
                  for (int h = 0; h < 7; h++)
                  {
                      rgbs[h] = RGBFromHSV(h*60, 1f, 1f);
                  }
                  return rgbs;
              }
              public static RGB RGBFromHSV(double h, double s, double v)
              {
                  if (h > 360 || h < 0 || s > 1 || s < 0 || v > 1 || v < 0)
                      return null;
      
                  double c = v * s;
                  double x = c *(1 - Math.Abs((h / 60 % 2) - 1));
                  double m = v - c;
      
                  double r = 0, g = 0, b = 0;
      
                  if (h < 60)
                  {
                      r = c;
                      g = x;
                  }
                  else if(h < 120)
                  {
                      r = x;
                      g = c;
                  }
                  else if(h < 180)
                  {
                      g = c;
                      b = x;
                  }
                  else if(h < 240)
                  {
                      g = x;
                      b = c;
                  }
                  else if(h < 300)
                  {
                      r = x;
                      b = c;
                  }
                  else if(h <= 360)
                  {
                      r = c;
                      b = x;
                  }
      
                  return new RGB((r+m)*255, (g + m) * 255, (b + m) * 255);
              }
          }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-29
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多