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 一个简单的颜色选择器的更多相关文章
- 从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器
先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我 ...
- WPF一个简单的垂直菜单样式的实现
以前制作类似于垂直菜单功能的控件我都是Listbox和一个Popup实现的,今天尝试着用Menu做了一个简单垂直菜单,就当是做了个小练习写了这篇随笔~: 有什么不对的地方希望大家指正,分享和记录也是一 ...
- 自定义实现简单的Android颜色选择器(附带源码)
在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几 ...
- 11个JavaScript颜色选择器插件
几年前,很难找到一个合适的颜色选择器.正好看到很多不错的JavaScript颜色选择器插件,故而把这些编译汇总.在本文,Web设计师和开发人员 Kevin Liew 选取了11个相应插件,有些会比较复 ...
- WPF 自己做一个颜色选择器
程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...
- Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块
原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...
- 【canvas系列】用canvas实现一个colorpicker(类似PS的颜色选择器)
每个浏览器都有自己的特点,比如今天要做的colorpicker就是,一千个浏览器,一千个哈姆雷特,一千个colorpicker.今天canvas系列就用canvas做一个colorpicker. ** ...
- WInform 创建一个简单的WPF应用
(一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...
- 【WPF】学习笔记(一)——做一个简单的电子签名板
参加实习(WPF)已经有两个多周的时间了,踩了一些坑,也算积累了一些小东西,准备慢慢拿出来分享一下.(●'◡'●) 这次呢就讲讲一个简单的电子签名板的实现. 先上张图(PS:字写得比较丑,不要太在意哈 ...
随机推荐
- css实现文字裁切效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- python,dict的setdefault方法
@dict的setdefault方法 先看看文档中的解释 setdefault(...) D.setdefault(k[,d]) -> D.get(k,d), also set D[k]= ...
- tcp 面向连接
TCP通信时通过三次握手建立连接,这个连接不是虚拟链路,每个IP报文是要寻址,通过路由转发的 那建立的这个连接能够起什么作用啊,感觉建立这个连接和不建立这个连接的效果是一样的啊!因为除去可靠性等机制, ...
- nginx开启gzip
gzip on; gzip_min_length 5k; gzip_buffers 4 16k; #gzip_http_version 1.0; gzip_comp_level 3; gzip_typ ...
- openssl生成iis需要的pfx格式的证书
合成.pfx证书 将私钥文件(server.key)和服务器crt证书文件(server.crt ),放到openssl安装目录的bin目录下. 控制台也进到此目录下,然后执行下面指令. openss ...
- SpringMVC转发页面405错误
需要在该方法前加上:@ResponseBody注解 加上这个注解后可能在转发页面的时候出现问题,则需要在方法的参数中增加HttpServletRequest 和HttpServletResponse参 ...
- java核心技术-多线程之基本使用
多线程程序好处就是可以提高cpu使用率和系统的性能.这里举个例子,民以食为天,咱们以餐馆为例(后面基本上都用餐馆作为对象),后面如果没有特殊说明均采用本节相关术语,围绕餐馆我们可以抽象出如下几个角色以 ...
- nginx+uwsgi+flask+supervisor 项目部署
环境 - Linux: Ubuntu 16.04 - uWSGI 2.0.18 - Flask 1.0.2 - supervisor 3.2.0 - nginx/1.8.1 首先区分几个概念 WSGI ...
- sublime Text3安装 markdownediting 报错 解决记录
看了一下官方文档,也是醉了,都怪自己的无知. 在安装时候不要打开.md的文件,因为你里面有可能有一些语法错误,所以会导致报错. 解决方法关闭其他文件,在重新安装! 官方解释: 参考文档:Trouble ...
- Ajax数据获取(未完待续。。。)
@:网站内容通过Ajax获取数据然后重新渲染产生 这种时候我们如何拿到自己想要的数据: 1:打开检查,在network选项中查看浏览器发送了哪些请求.(清空,再刷新,得到新的请求) 在截图的左下角可以 ...