原文:WPF 一个简单的颜色选择器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BYH371256/article/details/83409995

本章讲述:WPF中,一个简单的颜色选择器的实现;

使用外部依赖库文件:“ColorPicker.dll”;

下载地址:https://download.csdn.net/download/byh371256/10745273

1、XAML界面代码

<Window x:Class="ColorInput.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ColorPicker;assembly=ColorPicker"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="cprct" TargetType="{x:Type Rectangle}">
<Setter Property="Width" Value="25"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Stroke" Value="Gray"/>
<Setter Property="StrokeThickness" Value="1"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Rectangle_PreviewMouseLeftButtonDown"/>
</Style>
</Window.Resources>
<Border VerticalAlignment="Center" Width="280" Background="#f0f0f0">
<StackPanel>
<WrapPanel HorizontalAlignment="Center">
<Rectangle Style="{StaticResource cprct}" Fill="White"/>
<Rectangle Style="{StaticResource cprct}" Fill="Gray"/>
<Rectangle Style="{StaticResource cprct}" Fill="Yellow"/>
<Rectangle Style="{StaticResource cprct}" Fill="Orange"/>
<Rectangle Style="{StaticResource cprct}" Fill="HotPink"/>
<Rectangle Style="{StaticResource cprct}" Fill="Peru"/>
<Rectangle Style="{StaticResource cprct}" Fill="RoyalBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="SkyBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Teal"/>
<Rectangle Style="{StaticResource cprct}" Fill="Tomato"/>
<Rectangle Style="{StaticResource cprct}" Fill="Black"/>
<Rectangle Style="{StaticResource cprct}" Fill="Sienna"/>
<Rectangle Style="{StaticResource cprct}" Fill="Gold"/>
<Rectangle Style="{StaticResource cprct}" Fill="DarkBlue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Magenta"/>
<Rectangle Style="{StaticResource cprct}" Fill="LimeGreen"/>
<Rectangle Style="{StaticResource cprct}" Fill="Lime"/>
<Rectangle Style="{StaticResource cprct}" Fill="Blue"/>
<Rectangle Style="{StaticResource cprct}" Fill="Green"/>
<Rectangle Style="{StaticResource cprct}" Fill="Red"/>
</WrapPanel>
<StackPanel Orientation="Horizontal" Height="30">
<TextBlock Text="ARGB:" VerticalAlignment="Center" Margin="16,0,10,0"/>
<TextBox Text="{Binding ElementName=cpicker, Path=SelectedColor, Mode=TwoWay}" VerticalAlignment="Center" Width="90"/>
<Rectangle Stroke="Gray" StrokeThickness="1" Width="80" Height="22" Margin="10,4,4,4"
Fill="{Binding ElementName=cpicker, Path=SelectedBrush}"/>
</StackPanel>
<local:ColorPicker x:Name="cpicker" SelectedColorChanged="cpicker_SelectedColorChanged"
SelectedColor="{Binding ElementName=clrctrl, Path= ExSelectedColor,Mode=OneWayToSource}"
SelectedBrush="{Binding ElementName= clrctrl,Path=ExSelectedBrush, Mode=TwoWay}" />
</StackPanel>
</Border>
</Window>

2、后台逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace ColorInput
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private void Rectangle_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Brush brush = (sender as Rectangle).Fill;
Color c = (Color)ColorConverter.ConvertFromString(brush.ToString());
cpicker.SelectedColor = c;
if (ColorChangedEvent != null)
ColorChangedEvent(this, c);
} public event ColorChangedHandler ColorChangedEvent; private void cpicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
if (this.IsLoaded == false)
return;
if (ColorChangedEvent != null)
ColorChangedEvent(this, e.NewValue);
} public static readonly DependencyProperty ExSelectedColorProperty = DependencyProperty.Register(
"ExSelectedColor",
typeof(Color),
typeof(MainWindow),
new PropertyMetadata(Colors.Black)); public Color ExSelectedColor
{
get { return (Color)GetValue(ExSelectedColorProperty); }
set { SetValue(ExSelectedColorProperty, value); }
} public static readonly DependencyProperty ExSelectedBrushProperty = DependencyProperty.Register(
"ExSelectedBrush",
typeof(Brush),
typeof(MainWindow),
new PropertyMetadata(Brushes.Black)); public Brush ExSelectedBrush
{
get { return (Brush)GetValue(ExSelectedBrushProperty); }
set { SetValue(ExSelectedBrushProperty, value); }
}
} public delegate void ColorChangedHandler(object sender, Color newColor);
}

3、效果图

WPF 一个简单的颜色选择器的更多相关文章

  1. 从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器

    先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我 ...

  2. WPF一个简单的垂直菜单样式的实现

    以前制作类似于垂直菜单功能的控件我都是Listbox和一个Popup实现的,今天尝试着用Menu做了一个简单垂直菜单,就当是做了个小练习写了这篇随笔~: 有什么不对的地方希望大家指正,分享和记录也是一 ...

  3. 自定义实现简单的Android颜色选择器(附带源码)

    在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几 ...

  4. 11个JavaScript颜色选择器插件

    几年前,很难找到一个合适的颜色选择器.正好看到很多不错的JavaScript颜色选择器插件,故而把这些编译汇总.在本文,Web设计师和开发人员 Kevin Liew 选取了11个相应插件,有些会比较复 ...

  5. WPF 自己做一个颜色选择器

    程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...

  6. Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块

    原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...

  7. 【canvas系列】用canvas实现一个colorpicker(类似PS的颜色选择器)

    每个浏览器都有自己的特点,比如今天要做的colorpicker就是,一千个浏览器,一千个哈姆雷特,一千个colorpicker.今天canvas系列就用canvas做一个colorpicker. ** ...

  8. WInform 创建一个简单的WPF应用

    (一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...

  9. 【WPF】学习笔记(一)——做一个简单的电子签名板

    参加实习(WPF)已经有两个多周的时间了,踩了一些坑,也算积累了一些小东西,准备慢慢拿出来分享一下.(●'◡'●) 这次呢就讲讲一个简单的电子签名板的实现. 先上张图(PS:字写得比较丑,不要太在意哈 ...

随机推荐

  1. java多态实例

    学校有两个打印机,一个彩印,一个黑白印,都打印输出 public class printerDemo { public static void main(String[] args) { colorP ...

  2. python的sys和os模块

    一.sys sys.argv:实现从程序外部向程序传递参数.  其中sys.argv[0]为脚本的名称,所以要判断是否有参数传入可以:if len(sys.argv) > 1.  sys.exi ...

  3. CSS加载性能优化

    将首屏页面要用到的CSS文件,放在页面头部加载,其他模块的CSS可以使用异步加载:loadCSS 和 Preload. 关于preload,推进2篇文章给大家看下: 1.通过rel="pre ...

  4. css3—产品列表之鼠标滑过效果

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" /> <title&g ...

  5. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165322

    Exp0 Kali安装 由于VMWare已经装好,镜像也是从上届拷下来的.下载部分就省略.. 新建虚拟机 安装好VM后点击新建虚拟机 选择稍后安装操作系统,系统选择Linux-ubuntu 接下来根据 ...

  6. BZOJ1259:[CQOI2007]矩形rect(DFS)

    Description 给一个a*b矩形,由a*b个单位正方形组成.你需要沿着网格线把它分成分空的两部分,每部分所有格子连通,且至少有一个格子在原矩形的边界上.“连通”是指任两个格子都可以通过水平或者 ...

  7. 【[APIO2008]免费道路】

    \(kruskal\)好题 \(0\)边的数量在某些情况下是可以无限制的调控的,前提是所有必须存在的边都在生成树里了 所以应该分别求出有哪些边是必须在生成树里的,我们可以先从大到小排序,求出有哪些\( ...

  8. Golang 单元测试和性能测试

    开发程序其中很重要的一点是测试,我们如何保证代码的质量,如何保证每个函数是可运行,运行结果是正确的,又如何保证写出来的代码性能是好的,我们知道单元测试的重点在于发现程序设计或实现的逻辑错误,使问题及早 ...

  9. FLV封装格式分析

    FLV官方文档:https://github.com/jiayayao/DataSheet/tree/master/media%20format/flv 一.FLV格式 FLV包括文件头(FLV he ...

  10. Unity设置相机正交相机和透视相机的动态切换

    Camera.main.orthographic = true;    Camera.main.orthographicSize = 4;    Camera.main.orthographic = ...