WPF 有颜色对话框吗?

问题描述 投票:0回答:5

我正在寻找 WPF 颜色对话框? 有内置的吗? 我应该建立自己的吗? 或者Win 32 互操作吗? 如果是的话,怎么办?

wpf dialog
5个回答
5
投票

我不久前编写了一个简单的 WPF 颜色选择器,它支持以下功能

  • 3 种不同类型的色样
  • 不透明度滑块
  • 用于选择颜色的鼠标移动器控制
  • 打开时设置为当前颜色
  • 标准对话框按钮

这里是文章,以防您需要它:http://www.codeproject.com/Articles/33001/WPF-A-Simple-Color-Picker-With-Preview


4
投票

这是一个使用附加属性的简单颜色选择器,如果有人正在寻找示例代码

public static class BrushExtender
{
    public readonly static DependencyProperty BrushProperty = DependencyProperty.RegisterAttached("Brush", typeof(Brush), typeof(BrushExtender), new PropertyMetadata(Brushes.Black,DoBrushChanged));

    public readonly static DependencyProperty RedChannelProperty = DependencyProperty.RegisterAttached("RedChannel", typeof(int), typeof(BrushExtender), new PropertyMetadata(DoColorChangedRed));

    public readonly static DependencyProperty GreenChannelProperty = DependencyProperty.RegisterAttached("GreenChannel", typeof(int), typeof(BrushExtender), new PropertyMetadata(DoColorChangedGreen));

    public readonly static DependencyProperty BlueChannelProperty = DependencyProperty.RegisterAttached("BlueChannel", typeof(int), typeof(BrushExtender), new PropertyMetadata(DoColorChangedBlue));

    public readonly static DependencyProperty AlphaChannelProperty = DependencyProperty.RegisterAttached("AlphaChannel", typeof(int), typeof(BrushExtender), new PropertyMetadata(DoColorChangedAlpha));

    public readonly static DependencyProperty ColourValueProperty = DependencyProperty.RegisterAttached("ColourValue", typeof(string), typeof(BrushExtender), new PropertyMetadata(DoValueChanged));

    public static void SetRedChannel(DependencyObject o, int value)
    {
        o.SetValue(RedChannelProperty, value);
    }

    public static void SetGreenChannel(DependencyObject o, int value)
    {
        o.SetValue(GreenChannelProperty, value);
    }

    public static void SetBlueChannel(DependencyObject o, int value)
    {
        o.SetValue(BlueChannelProperty, value);
    }

    public static void SetAlphaChannel(DependencyObject o, int value)
    {
        o.SetValue(AlphaChannelProperty, value);
    }

    public static void SetBrush(DependencyObject o, SolidColorBrush brush)
    {
        o.SetValue(BrushProperty, brush);
    }

    public static void SetColourValue(DependencyObject o, string value)
    {
        o.SetValue(ColourValueProperty, value);
    }

    private static void DoColorChangedRed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        DoColorChange(d, (int)e.NewValue, c => c.R, () => Color.FromArgb(color.A, ((byte)(int)e.NewValue), color.G , color.B));
    }

    private static void DoColorChangedGreen(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        DoColorChange(d, (int)e.NewValue, c => c.G, () => Color.FromArgb(color.A, color.R, ((byte)(int)e.NewValue), color.B));
    }

    private static void DoColorChangedBlue(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        DoColorChange(d, (int)e.NewValue, c => c.B, () => Color.FromArgb(color.A, color.R, color.G, (byte)(int)e.NewValue));
    }

    private static void DoColorChangedAlpha(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        DoColorChange(d, (int)e.NewValue, c => c.A, () => Color.FromArgb((byte)(int)e.NewValue, color.R, color.G, color.B));
    }

    private static void DoColorChange(DependencyObject d, int newValue, Func<Color, int> colorCompare, Func<Color> getColor)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        if (colorCompare(color) == newValue)
            return;
        var newBrush = new SolidColorBrush(getColor());
        d.SetValue(BrushProperty, newBrush);
        d.SetValue(ColourValueProperty, newBrush.Color.ToString());
    }

    private static void DoValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var color = ((SolidColorBrush)d.GetValue(BrushProperty)).Color;
        if (color.ToString() == (string)e.NewValue)
            return;
        Color? newColour = null;
        try
        {
            newColour = (Color) ColorConverter.ConvertFromString((string) e.NewValue);
        }
        catch{}
        if (newColour == null)
            return;
        var newBrush = new SolidColorBrush(newColour.Value);
        d.SetValue(BrushProperty, newBrush);
    }


    private static void DoBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == e.OldValue)
            return;
        var colour = ((SolidColorBrush)e.NewValue).Color;
        d.SetValue(RedChannelProperty,(int)colour.R);
        d.SetValue(GreenChannelProperty,(int)colour.G);
        d.SetValue(BlueChannelProperty,(int)colour.B);
        d.SetValue(AlphaChannelProperty,(int)colour.A);
        d.SetValue(ColourValueProperty,colour.ToString());
    }
}

这里正在使用它

<Window x:Class="ChannelColourBrush.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:ChannelColourBrush" Title="Window1" Height="300" Width="300">
<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>

  <TextBlock Text="Red" />
  <TextBlock Text="Green" Grid.Row="1" />
  <TextBlock Text="Blue" Grid.Row="2" />
  <TextBlock Text="Alpha" Grid.Row="3" />

  <Slider Name="redSlider" Grid.Column="1" Minimum="0" Maximum="255"  Width="200" Height="20" Grid.ColumnSpan="2" Value="{Binding ElementName=rect, Path=(c:BrushExtender.RedChannel), Mode=TwoWay}" />
  <Slider Name="greenSlider" Grid.Column="1" Grid.Row="1" Minimum="0" Maximum="255" Width="200" Height="20" Grid.ColumnSpan="2" Value="{Binding ElementName=rect, Path=(c:BrushExtender.GreenChannel), Mode=TwoWay}"  />
  <Slider Name="blueSlider" Grid.Column="1" Grid.Row="2" Minimum="0" Maximum="255" Width="200" Height="20" Grid.ColumnSpan="2" Value="{Binding ElementName=rect, Path=(c:BrushExtender.BlueChannel), Mode=TwoWay}"  />
  <Slider Name="alphaSlider" Grid.Column="1" Grid.Row="3" Minimum="0" Maximum="255" Width="200" Height="20" Grid.ColumnSpan="2" Value="{Binding ElementName=rect, Path=(c:BrushExtender.AlphaChannel), Mode=TwoWay}"  />

  <Rectangle Fill="SandyBrown" Name="rect" Width="200" Height="50" Grid.Row="4" Grid.ColumnSpan="3" Margin="0,20,0,10"
    c:BrushExtender.Brush="{Binding RelativeSource={RelativeSource Self}, Path=Fill, Mode=TwoWay}"/>
  <TextBlock Text="Colour Value" Margin="5,0,5,0" Grid.Row="5" HorizontalAlignment="Center"  />
  <TextBox Text="{Binding ElementName=rect, Path=(c:BrushExtender.ColourValue), Mode=TwoWay}" Margin="0,0,0,0" Grid.Row="5" Grid.Column="1" Width="100" HorizontalAlignment="Center" />
  <Button Content="Update" IsEnabled="{Binding ElementName=grid, Path=SelectedItem.SomeValue}"/>
</Grid>


4
投票

您可以轻松使用

ColorDialog
中提供的经典Windows窗体

using System.Windows.Forms;

ColorDialog colorDialog = new ColorDialog();

if (colorDialog.ShowDialog() == DialogResult.OK)
{
    // do some stuff with colors...
}


0
投票

我自己被迫进行这样的对话。请查看 WPFColorLib - 它是 FOSS。


0
投票

所以我写了上面的控件,正如我所说,我所做的就是

  • 解压代码
  • 在 Vs2022 中打开。它确实升级了。我让了。
  • 然后我构建了 WPFColorPickerLib
  • 然后我运行了它,它第一次对我有用

enter image description here

当我选择颜色时,正如预期的那样

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.