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

具体简单的界面显示引用可以参考地址:https://blog.csdn.net/BYH371256/article/details/90520134

使用外部依赖库文件:“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. android简单的颜色选择器制作

    前两天需要开发一个蓝牙通信控制灯的颜色的项目,上网找了一个关于颜色选择器制作的帖子. 众所周知,android的控件只完成了基本的功能,对于像颜色选择的功能则需要自定义控件的使用. 网上的帖子主要都是 ...

  3. WPF实现简单的颜色调色板功能

    Winform有自带的ColorDialog功能,WPF可以通过引用System.Windows.Forms的方式来创建调色板,在这里仅使用Canvas等控件来实现简单的调色板.效果如下: gif录制 ...

  4. 基于opencv做一个HSV的颜色选择器

    From sztu 自动化专业的小菜鸡. 本篇将介绍计算机视觉的HSV颜色选择器,基于python的opencv. 众所周知,每个物体的HSV值都是不同的,并且每个色系所在的HSV的颜色范围也都不同, ...

  5. android类中定义颜色,自定义实现简单的Android颜色选择器(附带源码)

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

  6. Ubuntu中的颜色选择器实用程序(彩色移液器)[关闭]

    本文翻译自:Color picker utility (color pipette) in Ubuntu [closed] I'am looking for a color picker utilit ...

  7. html简易颜色选择器,HTML颜色选择器实现代码

    HTML颜色选择器实现代码 HTML颜色选择器 范围:#000 - #FFF //  '); } document.writeln(' '); var begin = 0; for (var i = ...

  8. python 简单的颜色序列生成器

    2021/04/21:我火星了???? python Seaborn库 调色板 所以下面的东西都别看了 . . . . . . . . . . 由来 昨天画了这张图,自动分配的颜色比较深,而且总颜色数 ...

  9. OPenCV4-颜色识别(一)调色板和简单的颜色识别

    OPenCV4-颜色识别(一)调色板和简单的颜色识别 使用 OPenCV4 做颜色识别十分简单.本文章使用 python 语言来实现一个调色板和简单的颜色识别. 1.调色板 绘制一个调色板对颜色识别非 ...

  10. 【WPF】一个简单的ColorPicker控件

    在斯克迪亚看到一篇WPF动态改变主题颜色的文章,来了兴趣,于是自己搞了个简单的ColorPicker控件. 控件其实很简单,定义了5个依赖属性 FinalBrushProperty, APropert ...

最新文章

  1. 2022-2028年中国石油套管行业市场研究及前瞻分析报告
  2. 在windows上解压linux文件夹,Win10如何使用命令行来解压缩文件?
  3. 一文搞定深度优先搜索(DFS)与广度优先搜索(BFS)【含完整源码】
  4. python模拟火车订票系统_如何用python编写火车抢票助手
  5. 微信小程序的setData
  6. 浅谈智能电网的建设 软件开发
  7. jcifs java_通过jcifs实现java访问网络共享文件
  8. 浅谈Entity Framework中的数据加载方式
  9. mysql5.7bka_mysql 5.7中的MRR和BKA算法
  10. ORA-07445ORA-00108错误案例
  11. 基于AT89C51的多层电梯控制系统
  12. 让coreseek支持拼音检索
  13. Hive架构及相关函数
  14. mac装node_Mac环境下node安装与卸载方法
  15. 苹果2021新品发布会,iMac全新设计你GET到了吗
  16. z自建服务器,《守望先锋》将加入自建服务器 自定规则
  17. GBase XDM使用场景
  18. 电力系统的遥测、遥信、遥控、遥调的含义
  19. [Cherno C++ 笔记 P1~P10]安装、链接器、变量、函数、头文件
  20. “Git 是我用过最笨重的软件”!喷完 C++ 喷 Git,这位 Azure CTO 到底何许人也?...

热门文章

  1. 无人驾驶安全报告分析
  2. win10如何打开摄像头_如何解决:Win10打开软件报错“应用程序无法正常启动 0xc0150002”...
  3. Tomcat:JAVA_HOME should point to a JDK not a JRE解决
  4. Ae:时间轴面板(时间线区域)
  5. 加载配置文件(xml文件,properties文件)demo
  6. 标准化的EPLAN电气绘图
  7. 视频监控系统的软件设计开发方案
  8. css中怎么改变图片尺寸,CSS也可以改变图片幅面尺寸
  9. 【推荐】智慧城市建设及发展资料合集
  10. 关于雷霄骅博士的博客FFMPEG+SDL的音频播放器播放有杂音的问题