原文: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一些使用

    随机数.输入.byte数组和string转换 一些可能会使用到的方法.供及时查找 ########################random类使用 Random random = new Rando ...

  2. IntelliJ IDEA常用设置(转)

    IntelliJ IDEA是一款非常优秀的JAVA编辑器,初学都可会对其中的一些做法感到很别扭,刚开始用的时候我也感到很不习惯,在参考了网上一些文章后在这里把我的一些经验写出来,希望初学者能快速适应它 ...

  3. jq实现鼠标经过出现上拉菜单

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. python一键对应多个值

    背景:目前是想让一个取出来的数据,数据有2个或多个值,对应一个key,或者这样说:让一个键对应2个值或者3个值 实现方式,如下:目前我以读取xml文件为案例,读取xml中两个配置文件,存放到一个key ...

  5. cocos2d::CCFileUtils::sharedFileUtils()->getFileData(szFile, "r", &bufferSize) 不同平台返回值不一样

    string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName); unsigned char* pB ...

  6. Java并发案例03---生产者消费者问题02

    生产者消费者第二种情形 package com.maple.msb.one; public class ProducerConsumer { public static void main(Strin ...

  7. P2149 [SDOI2009]Elaxia的路线

    题目描述 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia和w**每天都要奔波于宿舍和实验室之间,他们 希望在 ...

  8. showDoc项目文档管理工具

    1:下载showdoc代码文件 https://github.com/star7th/showdoc https://www.showdoc.cc/help?page_id=14 官方帮助文档 2:安 ...

  9. Spring(十三)之SQL存储过程

    SimpleJdbcCall 类可以被用于调用一个包含 IN 和 OUT 参数的存储过程.你可以在处理任何一个 RDBMS 时使用这个方法,就像 Apache Derby, DB2, MySQL, M ...

  10. ThinkPHP5入门(三)----模型篇

    一.操作数据库 1.数据库连接配置 数据库默认的相关配置在项目的application\database.php中已经定义好. 只需要在模块的数据库配置文件中配置好当前模块需要连接的数据库的配置参数即 ...