iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >基于WPF实现验证码控件
  • 855
分享到

基于WPF实现验证码控件

WPF验证码控件WPF验证码WPF控件 2022-11-13 14:11:59 855人浏览 独家记忆
摘要

代码如下 一、创建CheckCode.xaml代码如下 <ResourceDictionary xmlns="Http://schemas.microsoft.com/winf

代码如下

一、创建CheckCode.xaml代码如下

<ResourceDictionary xmlns="Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:WPFDevelopers.Controls">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    
    <Style TargetType="{x:Type controls:CheckCode}" BasedOn="{StaticResource ControlBasicStyle}">
        <Setter Property="Background" Value="{x:Null}"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:CheckCode}">
                    <Image x:Name="PART_Image" Stretch="Fill" Source="{TemplateBinding ImageSource}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

二、CheckCode.cs代码如下

using System;
using System.windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WPFDevelopers.Controls
{
    [TemplatePart(Name = ImageTemplateName, Type = typeof(Image))]
    public class CheckCode : Control
    {

        private const string ImageTemplateName = "PART_Image";
        private Image _image;
        private Size _size = new Size(70, 23);
        private const string strCode = "abcdefhkmnprstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; 

        public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.ReGISter("ImageSource", typeof(ImageSource), typeof(CheckCode),new PropertyMetadata(null));
        /// <summary>
        /// 随机生成的验证码
        /// </summary>
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        /// <summary>
        /// 字体颜色
        /// </summary>
        public Brush SizeColor
        {
            get { return (Brush)GetValue(SizeColorProperty); }
            set { SetValue(SizeColorProperty, value); }
        }

        public static readonly DependencyProperty SizeColorProperty =
            DependencyProperty.Register("SizeColor", typeof(Brush), typeof(CheckCode), new PropertyMetadata(DrawinGContextHelper.Brush));

        public CheckCode()
        {
            this.Loaded += CheckCode_Loaded;
        }

        private void CheckCode_Loaded(object sender, RoutedEventArgs e)
        {
            ImageSource = CreateCheckCodeImage(CreateCode(4), (int)this.ActualWidth, (int)this.ActualHeight);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _image = GetTemplateChild(ImageTemplateName) as Image;
            if (_image != null)
                _image.PreviewMouseDown += _image_PreviewMouseDown;


        }

        private void _image_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsLoaded)
                return;

            ImageSource = CreateCheckCodeImage(CreateCode(4), (int)this.ActualWidth, (int)this.ActualHeight);
        }

        private string CreateCode(int strLength)
        {
            var _charArray = strCode.ToCharArray();
            var randomCode = "";
            int temp = -1;
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < strLength; i++)
            {
                if (temp != -1)
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                int t = rand.Next(strCode.Length - 1);
                if (!string.IsNullOrWhiteSpace(randomCode))
                {
                    while (randomCode.ToLower().Contains(_charArray[t].ToString().ToLower()))
                        t = rand.Next(strCode.Length - 1);
                }
                if (temp == t)
                    return CreateCode(strLength);
                temp = t;

                randomCode += _charArray[t];
            }
            return randomCode;
        }
        private ImageSource CreateCheckCodeImage(string checkCode, int width, int height)
        {
            if (string.IsNullOrWhiteSpace(checkCode))
                return null;
            if (width <= 0 || height <= 0)
                return null;
            var drawingVisual = new DrawingVisual();
            var random = new Random(Guid.NewGuid().GetHashCode());
            using (DrawingContext dc = drawingVisual.RenderOpen())
            {
                dc.DrawRectangle(Brushes.White, new Pen(SizeColor, 1), new Rect(_size));
                var fORMattedText = DrawingContextHelper.GetFormattedText(checkCode,color:SizeColor, flowDirection: FlowDirection.LeftToRight,textSize:20, fontWeight: FontWeights.Bold);
                dc.DrawText(formattedText, new Point((_size.Width - formattedText.Width) / 2, (_size.Height - formattedText.Height) / 2));

                for (int i = 0; i < 10; i++)
                {
                    int x1 = random.Next(width - 1);
                    int y1 = random.Next(height - 1);
                    int x2 = random.Next(width - 1);
                    int y2 = random.Next(height - 1);

                    dc.DrawGeometry(Brushes.Silver, new Pen(Brushes.Silver, 0.5D), new LineGeometry(new Point(x1, y1), new Point(x2, y2)));
                }

                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(width - 1);
                    int y = random.Next(height - 1);
                    SolidColorBrush c = new SolidColorBrush(Color.FromRgb((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)));
                    dc.DrawGeometry(c, new Pen(c, 1D), new LineGeometry(new Point(x - 0.5, y - 0.5), new Point(x + 0.5, y + 0.5)));
                }

                dc.Close();
            }

            var renderBitmap = new RenderTargetBitmap(70, 23, 96, 96, PixelFormats.Pbgra32);
            renderBitmap.Render(drawingVisual);
            return BitmapFrame.Create(renderBitmap);
        }
    }
}

三、新建CheckCodeExample.cs代码如下

<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.CheckCodeExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
             xmlns:wpfdev="https://GitHub.com/WPFDevelopersOrg/WPFDevelopers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UniformGrid Rows="2" Columns="2">
        <wpfdev:CheckCode SizeColor="LimeGreen"/>
        <wpfdev:CheckCode SizeColor="Red"/>
        <wpfdev:CheckCode SizeColor="DodgerBlue"/>
        <wpfdev:CheckCode SizeColor="HotPink"/>
    </UniformGrid>
</UserControl>

效果预览

源码地址如下

github:https://github.com/WPFDevelopersOrg

Gitee:https://gitee.com/WPFDevelopersOrg

以上就是基于WPF实现验证码控件的详细内容,更多关于WPF验证码控件的资料请关注编程网其它相关文章!

--结束END--

本文标题: 基于WPF实现验证码控件

本文链接: https://www.lsjlt.com/news/171495.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
iis服务器助手广告
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作