• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解如何用WPF图形解锁控件ScreenUnLock

c# 搞代码 4年前 (2022-01-09) 34次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了WPF图形解锁控件ScreenUnLock的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

ScreenUnLock 与智能手机上的图案解锁功能一样。通过绘制图形达到解锁或记忆图形的目的。

本人突发奇想,把手机上的图形解锁功能移植到WPF中。也应用到了公司的项目中。

在创建ScreenUnLock之前,先来分析一下图形解锁的实现思路。

1.创建九宫格原点(或更多格子),每个点定义一个坐标值

2.提供图形解锁相关扩展属性和事件,方便调用者定义。比如:点和线的颜色(Color),操作模式(Check|Remember),验证正确的颜色(RightColor), 验证失败的颜色(ErrorColor), 解锁事件 O

本文来源gaodai.ma#com搞#代!码网_

nCheckedPoint,记忆事件 OnRememberPoint 等;

3.定义MouseMove事件监听画线行为。 画线部分也是本文的核心。在画线过程中。程序需判断,线条从哪个点开始绘制,经过了哪个点(排除已经记录的点)。是否完成了绘制等等。

4.画线完成,根据操作模式处理画线完成行为。并调用相关自定义事件

大致思路如上,下面开始一步一步编写ScreenUnLock吧

创建ScreenUnLock

public partial class ScreenUnlock : UserControl

定义相关属性

/// <summary>  /// 验证正确的颜色  /// </summary>  private SolidColorBrush rightColor;  /// <summary>  /// 验证失败的颜色  /// </summary>  private SolidColorBrush errorColor;  /// <summary>  /// 图案是否在检查中  /// </summary>  private bool isChecking;  public static readonly DependencyProperty PointArrayProperty = DependencyProperty.Register("PointArray", typeof(IList<string>), typeof(ScreenUnlock));  /// <summary>  /// 记忆的坐标点   /// </summary>  public IList<string> PointArray  {   get { return GetValue(PointArrayProperty) as IList<string>; }   set { SetValue(PointArrayProperty, value); }  }  /// <summary>  /// 当前坐标点集合  /// </summary>  private IList<string> currentPointArray;  /// <summary>  /// 当前线集合  /// </summary>  private IList<Line> currentLineList;  /// <summary>  /// 点集合  /// </summary>  private IList<Ellipse> ellipseList;  /// <summary>  /// 当前正在绘制的线  /// </summary>  private Line currentLine;  public static readonly DependencyProperty OperationPorperty = DependencyProperty.Register("Operation", typeof(ScreenUnLockOperationType), typeof(ScreenUnlock), new FrameworkPropertyMetadata(ScreenUnLockOperationType.Remember));  /// <summary>  /// 操作类型  /// </summary>  public ScreenUnLockOperationType Operation  {   get { return (ScreenUnLockOperationType)GetValue(OperationPorperty); }   set { SetValue(OperationPorperty, value); }  }  public static readonly DependencyProperty PointSizeProperty = DependencyProperty.Register("PointSize", typeof(double), typeof(ScreenUnlock), new FrameworkPropertyMetadata(15.0));  /// <summary>  /// 坐标点大小   /// </summary>  public double PointSize  {   get { return Convert.ToDouble(GetValue(PointSizeProperty)); }   set { SetValue(PointSizeProperty, value); }  }  public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(SolidColorBrush), typeof(ScreenUnlock), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.White), new PropertyChangedCallback((s, e) =>  {   (s as ScreenUnlock).Refresh();  })));  /// <summary>  /// 坐标点及线条颜色  /// </summary>  public SolidColorBrush Color  {   get { return GetValue(ColorProperty) as SolidColorBrush; }   set { SetValue(ColorProperty, value); }  }     /// <summary>     /// 操作类型     /// </summary>     public enum ScreenUnLockOperationType     {      Remember = 0, Check = 1     }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解如何用WPF图形解锁控件ScreenUnLock

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址