WPF-21:WPF实现仿安卓的图案密码键盘(初级)
希望大家有这方面好的代码给提供下,谢谢了!
想用C#做一个和手机上一样的图形密码键盘,貌似这方面资料比较少,虽然winphone手机上也有但是网上也没有这方面的代码。只好用常规的思维去实现一下,当然是比较简单的,希望各路高手能给一些好的建议,这篇文章算是抛砖引玉吧,用WPF实现。
思路如下:
使用常用控件从最简单的图案绘制,然后放在相应的控件上,利用鼠标的Move事件,判断鼠标滑过哪些控件,然后将控件上的对应密码的数字收集,最终形成密码。
具体实现:
工程名:TestPicturePassword
一般常见的图案密码按键都是圆形的,所以利用重绘事件画一个圆形。
/// <summary>
/// 按键形状类
/// </summary>
public class KeyBorder:Border
{
public Brush SelfBacground
{
get { return (Brush)GetValue(SelfBacgroundProperty); } set
{
SetValue(SelfBacgroundProperty, value); this.InvalidateVisual();
}
} public static readonly DependencyProperty SelfBacgroundProperty =
DependencyProperty.Register("SelfBacground", typeof(Brush), typeof(KeyBorder), new UIPropertyMetadata()); /// <summary>
/// 使绘制区域为自定义形状,这里是圆形
/// </summary>
/// <param name="dc"></param>
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc); //画矩形的
//SolidColorBrush mySolidColorBrush = new SolidColorBrush(); //mySolidColorBrush.Color = Colors.LimeGreen; //Pen myPen = new Pen(Brushes.Blue, 10); //Rect myRect = new Rect(0, 0, 500, 500); //dc.DrawRectangle(mySolidColorBrush, myPen, myRect); //画圆形
EllipseGeometry ellipse = new EllipseGeometry(new Point(40, 40), 30, 30);//piont中的参数最好要设置属性进行外部设置 ellipse.RadiusX = 30; ellipse.RadiusY = 30; RectangleGeometry rect = new RectangleGeometry(new Rect(50, 50, 50, 20), 5, 5); GeometryGroup group = new GeometryGroup(); group.FillRule = FillRule.EvenOdd; group.Children.Add(ellipse); //group.Children.Add(rect); dc.DrawGeometry(SelfBacground, new Pen(Brushes.Green, 2), group);
}
}
再将这个圆形放在另一个容器中,将容器控件的背景设置为透明。
<UserControl x:Class="TestPicturePassword.KeyButton"
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:TestPicturePassword"
mc:Ignorable="d" Background="Transparent"
BorderThickness="0">
<Grid>
<local:KeyBorder x:Name="ellipseBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</UserControl>
/// <summary>
/// KeyButton.xaml 的交互逻辑
/// </summary>
public partial class KeyButton : UserControl
{
public Brush SelfBacground
{
get { return (Brush)GetValue(SelfBacgroundProperty); } set
{
SetValue(SelfBacgroundProperty, value); this.ellipseBorder.SelfBacground = value;
}
} public static readonly DependencyProperty SelfBacgroundProperty =
DependencyProperty.Register("SelfBacground", typeof(Brush), typeof(UserControl), new UIPropertyMetadata()); public KeyButton()
{
InitializeComponent();
}
}
最终将按键按要求排布,
如图,
<UserControl x:Class="TestPicturePassword.PatternPasswordKeyboard"
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:TestPicturePassword"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid HorizontalAlignment="Center" Name="grid1" VerticalAlignment="Center"
Background="Transparent">
<Grid.Resources>
<!--键盘按钮的样式-->
<Style x:Key="PasswordBorderStyle" TargetType="local:KeyButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="SelfBacground" Value="Gray"/>
<Setter Property="Width" Value="80" />
<Setter Property="Height" Value="80" />
<Setter Property="Margin" Value="10"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<EventSetter Event="Mouse.MouseMove" Handler="BorderMouseMove"/>
</Style>
</Grid.Resources> <Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<local:KeyButton Grid.Row="0" Grid.Column="0" x:Name="border1" Style="{StaticResource PasswordBorderStyle}" Tag="1" />
<local:KeyButton Grid.Row="0" Grid.Column="1" x:Name="border2" Style="{StaticResource PasswordBorderStyle}" Tag="2" />
<local:KeyButton Grid.Row="0" Grid.Column="2" x:Name="border3" Style="{StaticResource PasswordBorderStyle}" Tag="3" />
<local:KeyButton Grid.Row="1" Grid.Column="0" x:Name="border4" Style="{StaticResource PasswordBorderStyle}" Tag="4" />
<local:KeyButton Grid.Row="1" Grid.Column="1" x:Name="border5" Style="{StaticResource PasswordBorderStyle}" Tag="5" />
<local:KeyButton Grid.Row="1" Grid.Column="2" x:Name="border6" Style="{StaticResource PasswordBorderStyle}" Tag="6" />
<local:KeyButton Grid.Row="2" Grid.Column="0" x:Name="border7" Style="{StaticResource PasswordBorderStyle}" Tag="7" />
<local:KeyButton Grid.Row="2" Grid.Column="1" x:Name="border8" Style="{StaticResource PasswordBorderStyle}" Tag="8" />
<local:KeyButton Grid.Row="2" Grid.Column="2" x:Name="border9" Style="{StaticResource PasswordBorderStyle}" Tag="9" />
</Grid>
</UserControl>
后台代码,在这里实现密码收集。
/// <summary>
/// PatternPasswordKeyboard.xaml 的交互逻辑
/// </summary>
public partial class PatternPasswordKeyboard : UserControl
{
public string password = string.Empty;//最终密码 private bool isMouseDonw = false;//控制只有鼠标按下的滑动才有效 private List<KeyButton> keyButtons = new List<KeyButton>();//密码所在的控件 public PatternPasswordKeyboard()
{
InitializeComponent(); this.MouseUp += new MouseButtonEventHandler(MainWindow_MouseUp); this.MouseDown += new MouseButtonEventHandler(MainWindow_MouseDown);
} /// <summary>
/// 重置
/// </summary>
internal void PatternPasswordKeyboard_ResetPassword()
{
this.password = string.Empty; foreach (KeyButton item in keyButtons)
{
item.SelfBacground = new SolidColorBrush(Colors.Transparent);
}
} void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
isMouseDonw = true;
} void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
isMouseDonw = false;
} private void BorderMouseMove(object sender, MouseEventArgs e)
{
if (!isMouseDonw)
{
return;
} KeyButton border = sender as KeyButton; if (border == null)
{
return;
} string key = border.Tag.ToString(); if (string.IsNullOrEmpty(key))
{
return;
} if (!password.Contains(key))
{
password += key;
} border.SelfBacground = new SolidColorBrush(Colors.Blue); keyButtons.Add(border);
}
}
测试代码:
如图,
<Window x:Class="TestPicturePassword.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestPicturePassword"
Title="MainWindow" xmlns:my="clr-namespace:TestPicturePassword">
<Grid> <Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions> <local:PatternPasswordKeyboard Grid.Row="0" x:Name="pkeyboard"/> <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="您输入的密码是:" VerticalAlignment="Center" FontSize="20" FontFamily="Microsoft YaHei" Margin="0,0,20,0"/>
<TextBox Height="50" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="291" Margin="0,0,50,0"/>
<Button Content="重 置" Height="50" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="110" Click="button1_Click" />
</StackPanel>
</Grid>
</Window>
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.MouseUp += new MouseButtonEventHandler(MainWindow_MouseUp);
} void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
this.textBox1.Text = pkeyboard.password;
} /// <summary>
/// 重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
this.pkeyboard.PatternPasswordKeyboard_ResetPassword();
}
}
效果图
代码下载:http://download.csdn.net/detail/yysyangyangyangshan/5724829
WPF-21:WPF实现仿安卓的图案密码键盘(初级)的更多相关文章
- WPF-21:WPF实现仿安卓的图案密码键盘(改进)
前面写了个简单的实现( http://blog.csdn.net/yysyangyangyangshan/article/details/9280439),不过效果不太好,各个点之间没有连接起来.这里 ...
- WPF开发随笔收录-仿安卓Toast
一.前言 在项目中,经常需要用到消息提醒功能,在以前接触安卓开发那会使用过Toast,于是打算在WPF上也来模仿一个,话不多说,撸起袖子干起来! 二.正文 1.首先新建一个工程,工程的目录如下 2.编 ...
- WPF系列教程——(一)仿TIM QQ界面 - 简书
原文:WPF系列教程--(一)仿TIM QQ界面 - 简书 TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM QQ 1. 准备 阅读本文假设你已经有XAML布局的基 ...
- WPF C#截图功能 仿qq截图
原文:WPF C#截图功能 仿qq截图 先上效果图 源码下载地址:http://download.csdn.net/detail/candyvoice/9788099 描述:启动程序,点击窗口butt ...
- 【WPF】WPF截屏
原文:[WPF]WPF截屏 引言 .NET的截图控件在网上流传得不多啊,难得发现一个精品截图控件( 传送门),但是无奈是winform的.后来又找到一个周银辉做的WPF截图(继续传送门),发现截屏是实 ...
- 【WPF】wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例
原文:[WPF]wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例 用Binding并通过Converter转换的时候,可能偶尔会遇到传参的问题, ...
- 基于托管的C++来使用WPF - Using WPF with Managed C++
基于托管的C++来使用WPF - Using WPF with Managed C++ Posted by Zeeshan Amjad This article was originally publ ...
- WPF ”真正的“高仿QQ
时常可以在各种论坛 博客 看到 各种所谓的 高仿QQ. 说实话 越看越想笑呢.(PS:纯粹的 抨击 那些 不追求 UI 完美主义者) 例如: 本次模仿 采用 C# WPF XAML , 总 ...
- WPF实现截图(仿微信截图)
WPF开发者QQ群: 340500857 | 微信群 -> 进入公众号主页 加入组织 每日一笑 肚子疼,去厕所排便,结果什么都没拉出来.看着自己坐在马桶上痛苦又努力却一无所获的样子,仿佛看到了 ...
随机推荐
- poj 2184 Cow Exhibition(背包变形)
这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...
- 【分享】深入浅出WPF全系列教程及源码
本人10月份提出离职,可是交接非常慢,预计年底才会交接完,趁着交接之际,自学了一下WPF,由于这是微软未来的发展趋势,自WIN7以来包含前不久公布的WIN8,核心还是WPF,在此,将自己的学习成果做一 ...
- read table 时关键字TRANSPORTING NO FIELDS的用法
关键字TRANSPORTING NO FIELDS 用于read table with key 一般用于等读取内表的时候,只是判断该内表中是否有次数据 不需要读取到工作区中. READ TABLE g ...
- 管道实现进程间通讯 、WaitNamedPipe
一.管道实现进程间通讯 主要的理论知识 1.什么是管道以及分类 管道是两个头的东西,每一个头各连接一个进程或者同一个进程的不同代码,依照管道的类别分有两种管道,匿名的和命名的:依照管道的传输方向分也能 ...
- IDL 自己定义功能
function add,x,y return, x+y end pro sum x=1 y=2 print,add(x,y) end 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- java学习笔记10--枚举
java学习笔记10--枚举 在JDK1.5之前,java可以有两种方式定义新类型:类和接口.对于大部分面向对 象编程来说,这两种方法看起来似乎足够了,但是在一些特殊情况下,这些方法就不适合.例如,想 ...
- 主流JavaScript框架(Dojo、Google Closure、jQuery、Prototype、Mootools和YUI)的分析和对比
本文主要选取了目前比较流行的JavaScript框架Dojo.Google Closure.jQuery.Prototype.Mootools和YUI进行对比,主要是根据网上的资料整理而成,希望可以供 ...
- hdu 3405 world islands
求删点后最小的生成树,n<50....数据好弱,直接暴力枚举就行...删点的时候直接g[i][j]=INF就行了. #include<iostream> #include<al ...
- chmod u+s(转)
参看了 http://hi.baidu.com/hehongrong/item/b64a6d6b094cf634ac3e8382 里面说 -s :在文件执行时把进程的属主或组ID置为该文件的文件属主. ...
- HTML5系列之——applicationCache对象
ApplicationCache主要简单介绍: applicationCache对象实现HTML5相应WEB离线功能.以下我们来主要解说applicationCache对象的一些主要功能和方法 app ...