博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF-21:WPF实现仿安卓的图案密码键盘(改进)
阅读量:5856 次
发布时间:2019-06-19

本文共 4137 字,大约阅读时间需要 13 分钟。

前面写了个简单的实现( ),不过效果不太好,各个点之间没有连接起来。这里增加连接各个按键。
其他代码不变,只改动PatternPasswordKeyboard类。
  
///      /// PatternPasswordKeyboard.xaml 的交互逻辑     ///      public partial class PatternPasswordKeyboard : UserControl     {         public string password = string.Empty;//最终密码           private bool isMouseDonw = false;//控制只有鼠标按下的滑动才有效           private List
keyButtons = new List
();//密码所在的控件 private List
points = new List
(); public PatternPasswordKeyboard() { InitializeComponent(); this.MouseUp += new MouseButtonEventHandler(MainWindow_MouseUp); this.MouseDown += new MouseButtonEventHandler(MainWindow_MouseDown); } ///
/// 重置 /// internal void PatternPasswordKeyboard_ResetPassword() { this.points.Clear(); this.password = string.Empty; foreach (KeyButton item in keyButtons) { item.SelfBacground = new SolidColorBrush(Colors.Transparent); } this.InvalidateVisual(); } void MainWindow_MouseDown(object sender, MouseButtonEventArgs e) { points.Clear(); PatternPasswordKeyboard_ResetPassword(); 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; } border.SelfBacground = new SolidColorBrush(Colors.Blue); keyButtons.Add(border); //将滑过的点保存起来 if (!password.Contains(key)) { password += key; GeneralTransform generalTransform = border.ellipseBorder.TransformToAncestor(this); Point point = generalTransform.Transform(this.TranslatePoint(new Point(0,0),this)); points.Add(point); this.InvalidateVisual(); } } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); if (points.Count < 2) { return; } //使用画复合图形画出已经保存的点 Pen p = new Pen(new SolidColorBrush(Colors.Red), 10); GeometryGroup group = new GeometryGroup(); group.FillRule = FillRule.EvenOdd; LineGeometry myLineGeometry = null; for (int i = 0; i < points.Count;i++ ) { if (i % 2 == 0) { if (myLineGeometry != null) { myLineGeometry.EndPoint = points[i]; } myLineGeometry = new LineGeometry(); myLineGeometry.StartPoint = points[i]; } else { myLineGeometry.EndPoint = points[i]; group.Children.Add(myLineGeometry); if (i + 1 < points.Count) { myLineGeometry = new LineGeometry(); myLineGeometry.StartPoint = points[i]; group.Children.Add(myLineGeometry); } else { myLineGeometry = null; } } } SolidColorBrush mySolidColorBrush = new SolidColorBrush(); drawingContext.DrawGeometry(mySolidColorBrush, new Pen(Brushes.Green, 10), group); } }
效果图:
代码下载:
你可能感兴趣的文章
Spark学习记录(二)Spark集群搭建
查看>>
短信猫JAVA二次开发包SMSLib,org.smslib.TimeoutException: No response from device解决方案...
查看>>
CloudStack 4.4学习总结之cloudstack-management安装
查看>>
VTSS Error code
查看>>
360提供的Php防注入代码
查看>>
windows phone (12) 小试自定义样式
查看>>
Linux后台启动脚本
查看>>
jna dll c
查看>>
CentOS 升级现有PHP版本
查看>>
(一) pyhon 基础语法(数值 字符串 元组 列表 字典)
查看>>
HDOJ 1003:求一串数字中和最大的连续子串
查看>>
RedHat 5.6_x86_64 + ASM + RAW+ Oracle 10g RAC (二)
查看>>
win7不能全屏
查看>>
MySQL/InnoDB的并发插入Concurrent Insert
查看>>
转两好文防丢:Debian 版本升级/降级 & Linux 应用程序失去输入焦点问题的解决...
查看>>
HDU - Pseudoforest
查看>>
Nexus杂
查看>>
Android --- GreenDao的实现(ORM框架)
查看>>
Linux平台Java调用so库-JNI使用例子
查看>>
Spring Data JPA
查看>>