wpf通过代码动态生成Label并通过ColorDialog修改颜色

在上一篇文章中,我用的调色板是popupcoloredit,要用这东西的前提是安装devexpress,这东西是要收费的,一开始我没注意到这点,
我之前在VS中写WPF程序的时候,在工具箱里面搜索color直接就跳出来了popupcoloredit,所以直接用了,作为一个程序员,我们当然是能不用收费的东西就不用啦,所以我现在把代码改了一下,调色板直接用的C#里面的ColorDialog。
效果如下,虽然不如devexpress控件库里面的popupcoloredit好看
但也能用得上:
在这里插入图片描述
这里需要注意的是,在WPF工程里面使用winform的ColorDialog的时候需要引用
System.Windows.Forms以及System.Drawing
源码下载连接:
https://download.csdn.net/download/weixin_43935474/12133488
下面是主要代码源码:
MainWindow.xaml文件代码如下:

<Window x:Class="TestWPFColorDialog.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWPFColorDialog"
        mc:Ignorable="d"
        Title="MainWindow" Height="175" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Border BorderThickness="1" BorderBrush="#FF737070">
            <DockPanel Name="myDockPanel" LastChildFill="False" Margin="1" Width="520"/>
        </Border>
        <TextBox Grid.Row="1" x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="158,0,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="71" TextChanged="textBox_TextChanged"/>
        <Label Grid.Row="1"  x:Name="label" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Content="色标尺分割段数(&lt;20)" Width="143" Height="26"/>

    </Grid>
</Window>

MainWindow.xaml.cs文件代码如下:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace TestWPFColorDialog
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private int nColorLabelNum = 0;//表示当前有几个label
        private int nCurrentLabelID = -1;//当前用户双击的那个label的ID
        public struct RulerColor//用来记录Label中的颜色
        {
            byte R;
            byte G;
            byte B;
            public RulerColor(byte r, byte g, byte b)
            {
                R = r;
                G = g;
                B = b;
            }
        }
        private List<RulerColor> lstColor;//用来记录界面上所有Label中的颜色
        public Dictionary<int, System.Windows.Controls.Label> dicColorLabel;//用来存放Label引用和其ID
        private ColorDialog myPopUpColorEdit;
        public MainWindow()
        {
            myPopUpColorEdit = new ColorDialog();
            lstColor = new List<RulerColor>();
            dicColorLabel = new Dictionary<int, System.Windows.Controls.Label>();
            InitializeComponent();
        }

        private void ColorSelected()
        {
            byte R = myPopUpColorEdit.Color.R;
            byte G = myPopUpColorEdit.Color.G;
            byte B = myPopUpColorEdit.Color.B;
            if (nCurrentLabelID != -1 && nCurrentLabelID < dicColorLabel.Count)
            {
                System.Windows.Media.Color col = System.Windows.Media.Color.FromArgb(255, R, G, B);
                dicColorLabel[nCurrentLabelID].Background = new SolidColorBrush(col);
                lstColor[nCurrentLabelID] = new RulerColor(R, G, B);
            }
        }
        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Regex rx = new Regex("^[0-9]*$");
            if (!rx.IsMatch(textBox.Text))//不是整数
            {
                return;
            }
            string str = textBox.Text.ToString();
            if (str == "")
            {
                return;
            }
            nColorLabelNum = Convert.ToInt32(str);
            if (nColorLabelNum > 20)
            {
                return;
            }
            if (dicColorLabel != null && dicColorLabel.Count > 0)
            {
                int nCount = dicColorLabel.Count;
                for (int i = 0; i < nCount; i++)
                {
                    dicColorLabel[i] = null;
                }
                dicColorLabel.Clear();
                myDockPanel.Children.Clear();

            }
            if (lstColor.Count > 0)
            {
                lstColor.Clear();
                //lstColor = null;//如果让lstColor = null,则后面需要用到lstColor的时候必须重新new
            }
            for (int i = 0; i < nColorLabelNum; i++)
            {
                byte B = Convert.ToByte(50 + i * 10);
                System.Windows.Controls.Label label = new System.Windows.Controls.Label();
                label.MouseDoubleClick += Label_MouseDoubleClick;
                System.Windows.Media.Color col = System.Windows.Media.Color.FromArgb(255, 0, B, 0);
                label.Background = new SolidColorBrush(col);
                lstColor.Add(new RulerColor(0, 0, B));
                label.Width = (myDockPanel.Width - 8) * 1.0f / nColorLabelNum - 2;
                label.Margin = new Thickness(1, 0, 1, 0);
                //将label添加到myDockPanel上
                IAddChild container = myDockPanel;//建立一个容器,将panel赋给他
                container.AddChild(label);//将label按钮添加到panel中
                dicColorLabel.Add(i, label);
            }
        }

        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Controls.Label mlabel = (System.Windows.Controls.Label)sender;
            for (int i = 0; i < dicColorLabel.Count; i++)
            {
                if (dicColorLabel[i] == mlabel)//找到用户双击的label在dicColorLabel字典中的key(即label的ID号)
                {
                    nCurrentLabelID = i;
                }
            }
            myPopUpColorEdit.ShowDialog();
            ColorSelected();
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreenHandBruce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值