<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
程式碼如下
一、建立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驗證碼控制元件的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45