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:字写得比较丑,不要太在意哈 ...
随机推荐
- 有关js弹出提示框几种方法
1直接提示只有确定功能的提示框 只显示提示信息 alert(“提示信息”); alert ();的参数只有一个就是提示信息,无返回值 2 弹出输入框让你输入内容 prompt() ; 有两个参数:第一 ...
- Shell传递参数【转载】
Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以 ...
- vue 项目搭建笔记1
1.首先安装node.js(傻瓜式安装,安装路径默认C盘) 2.打开node.js command prompt 3.进入想放项目的文件夹.如D: -->回车 4.进入具体文件夹,如cd wo ...
- iOS UI(绘图)的几张原理图
Core Animation是对OpenGL ES的Objective-C封装,具有与OpenGL ES几乎等价的高性能,却隐藏了OpenGL ES的复杂性. https://www.cnblogs. ...
- 揭秘:C++编译器的函数编译流程
http://www.cnblogs.com/zhenjing/archive/2010/10/20/1856309.html C++中的类型查找过程相对简单,基本上就是名字查找,这里不再介绍. 对于 ...
- Odoo中要根据某字段值作为条件判断值,但又不想在界面显示
在视图标签中包含这个field(包含这个字段才可以使用该字段),但其invisible属性设置为1,即:该字段不可见. <field name="字段" invisible= ...
- 在64位Ubuntu上编译32位程序常见错误
问 题1: 找不到头文件 asm/errno.h 解决办法 : [/usr/lib/gcc$ ]sudo ln -s x86_64-linux-gnu/asm asm 问题2:找不到gcc ...
- 浅谈MVP架构及开发模式
Model-View-Presenter(MVP)概述 MVC模式已经出现了几十年了,在GUI领域已经得到了广泛的应用,由于微软ASP.NET MVC Framework的出现,致使MVC一度成 ...
- PAT——1072. 开学寄语(20)
下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其QQ,封其电脑,夺其手机,收其ipad,断其wifi,使其百无聊赖,然后,净面.理发.整衣,然后思过.读书.锻炼.明智.开悟.精进 ...
- 框架 Hibernate
Hibernate 在test01右键新建其他找到hibernate文件夹下的Hibernate Configuration File(cfg.xml) <?xml version=" ...