WPF 仿安卓手势解锁 图案解锁

您所在的位置:网站首页 Oracle账号解锁 WPF 仿安卓手势解锁 图案解锁

WPF 仿安卓手势解锁 图案解锁

2023-04-09 01:53| 来源: 网络整理| 查看: 265

定义一个控件

继承一下UserControl

namespace LeakDetection.CombustibleGasMonitoring.Pages.FirstGradePage.Views { public partial class BorderCheck : UserControl { public Boolean IsChecked { get { return (Boolean)GetValue(IsCheckedProperty); } set { SetValue(IsCheckedProperty, value); } } public static DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(Boolean), typeof(BorderCheck), new PropertyMetadata(new PropertyChangedCallback(OnIsCheckedPropertyChanged))); private static void OnIsCheckedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { BorderCheck bc = sender as BorderCheck; if ((Boolean)e.NewValue == (Boolean)e.OldValue) { return; } if (bc.IsChecked) { bc.Bd.BorderThickness = new Thickness(10,10,10,10); } else { bc.Bd.BorderThickness = new Thickness(0); } } public BorderCheck() { InitializeComponent(); this.MouseEnter += delegate { this.Cursor = Cursors.Hand; }; this.MouseLeave += delegate { this.Cursor = Cursors.Arrow; }; } } }

找个地方用它

事件以及操作都在这里了

namespace LeakDetection.CombustibleGasMonitoring.Pages.FirstGradePage.Views { public enum Grade { ONE = 0, TWO = 1 } /// /// FirstGradePageView.xaml 的交互逻辑 /// public partial class FirstGradePageView : Page { public FirstGradePageView() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } private Grade thisGrade = Grade.ONE; private EnumImpower thisEnumImpower = EnumImpower.Null; public void SetGrade(Grade grade, EnumImpower enumImpower) { thisGrade = grade; thisEnumImpower = enumImpower; } private static SolidColorBrush LineColorBrush = new SolidColorBrush(Color.FromRgb(22, 114, 186)) { Opacity = 0.8 }; private String Password { get; set; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.Bd.MouseLeftButtonDown += new MouseButtonEventHandler(Bd_MouseLeftButtonDown); this.Bd.MouseMove += new MouseEventHandler(Bd_MouseMove); this.Bd.MouseLeftButtonUp += new MouseButtonEventHandler(Bd_MouseLeftButtonUp); this.Bd.MouseLeave += new MouseEventHandler(Bd_MouseLeave); } private void Bd_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.Source == null || e.Source.GetType().Name != "BorderCheck") { Password = String.Empty; return; } Point startPoint = GetPointByKey((e.Source as BorderCheck).Tag.ToString()); Line line = new Line(); line.Stroke = LineColorBrush; line.SnapsToDevicePixels = true; line.StrokeThickness = 2; line.X1 = startPoint.X; line.Y1 = startPoint.Y; line.X2 = startPoint.X; line.Y2 = startPoint.Y; this.Bd.Children.Add(line); (e.Source as BorderCheck).IsChecked = true; Password = (e.Source as BorderCheck).Tag.ToString(); } private void Bd_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton != MouseButtonState.Pressed) { return; } try { Point tmpPoint = e.GetPosition(Bd); int i = this.Bd.Children.Count; var asLine = this.Bd.Children[this.Bd.Children.Count - 1] as Line; if (asLine != null) { (this.Bd.Children[this.Bd.Children.Count - 1] as Line).X2 = tmpPoint.X; (this.Bd.Children[this.Bd.Children.Count - 1] as Line).Y2 = tmpPoint.Y; if ((e.Source != null && e.Source.GetType().Name != "BorderCheck") || (e.Source as BorderCheck).IsChecked) { return; } (e.Source as BorderCheck).IsChecked = true; Password += (e.Source as BorderCheck).Tag.ToString(); //修改原直线的终点坐标 Point bdPoint = GetPointByKey((e.Source as BorderCheck).Tag.ToString()); (this.Bd.Children[this.Bd.Children.Count - 1] as Line).X2 = bdPoint.X; (this.Bd.Children[this.Bd.Children.Count - 1] as Line).Y2 = bdPoint.Y; //重新画一条直线 Line line = new Line(); line.Stroke = LineColorBrush; line.SnapsToDevicePixels = true; line.StrokeThickness = 2; line.X1 = bdPoint.X; line.Y1 = bdPoint.Y; line.X2 = tmpPoint.X; line.Y2 = tmpPoint.Y; this.Bd.Children.Add(line); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } private void Bd_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (!String.IsNullOrEmpty(Password)) { Login(Password); ResetLine(); } } private void Bd_MouseLeave(object sender, MouseEventArgs e) { ResetLine(); } private Point GetPointByKey(String key) { Point point = new Point(); Double oneWidth = this.gridCC.ActualWidth / this.gridCC.RowDefinitions.Count; //每个单元格的宽度 Double oneHeight = this.gridCC.ActualHeight / this.gridCC.ColumnDefinitions.Count; //每个单元格的高度 switch (key) { case "1": point.X = oneWidth * 0 + oneWidth / 2; point.Y = oneHeight * 0 + oneHeight / 2; break; case "2": point.X = oneWidth * 1 + oneWidth / 2; point.Y = oneHeight * 0 + oneHeight / 2; break; case "3": point.X = oneWidth * 2 + oneWidth / 2; point.Y = oneHeight * 0 + oneHeight / 2; break; case "4": point.X = oneWidth * 0 + oneWidth / 2; point.Y = oneHeight * 1 + oneHeight / 2; break; case "5": point.X = oneWidth * 1 + oneWidth / 2; point.Y = oneHeight * 1 + oneHeight / 2; break; case "6": point.X = oneWidth * 2 + oneWidth / 2; point.Y = oneHeight * 1 + oneHeight / 2; break; case "7": point.X = oneWidth * 0 + oneWidth / 2; point.Y = oneHeight * 2 + oneHeight / 2; break; case "8": point.X = oneWidth * 1 + oneWidth / 2; point.Y = oneHeight * 2 + oneHeight / 2; break; case "9": point.X = oneWidth * 2 + oneWidth / 2; point.Y = oneHeight * 2 + oneHeight / 2; break; default: break; } return point; } private void ResetLine() { foreach (UIElement element in this.gridCC.Children) { if (element.GetType().Name == "BorderCheck") { (element as BorderCheck).IsChecked = false; } } for (int i = this.Bd.Children.Count - 1; i >= 0; i--) { if (this.Bd.Children[i].GetType().Name != "Grid") { this.Bd.Children.RemoveAt(i); } } } private void Login(string passWord) { bool IsLogin = false; if(passWord.Equals("123")) { IsLogin = true; } else { } if (IsLogin==true) { Window win = (Window)this.Parent; win.Close(); } } private void Button_Click(object sender, RoutedEventArgs e) { Login(password.Password); } } }

测试源码下载

https://download.csdn.net/download/g313105910/12879206



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3