2

我希望在 Visual Studio C# 中创建一个模仿 Android 模式锁定屏幕的应用程序,并且想知道是否有人对此有任何提示。

这不是商业项目,不会赚钱,只是有点乐趣。

我一直在 WPF 项目中玩耍,现在不知所措。我想到的唯一方法是在MouseDown事件上跟踪鼠标,并使用 C# 中的绘制功能来“绘制”鼠标所在的位置,但我不认为这是最好的解决方案。

有什么想法吗?

4

1 回答 1

1

看看这是否可以帮助您是粗略的代码。

public partial class PatternLock : UserControl
    {
        bool isMouseDown = false;

        private ObservableCollection<ToggleButton> selectedobject = new ObservableCollection<ToggleButton>();
        public PatternLock()
        {
            InitializeComponent();

        }

        internal ObservableCollection<int> buttons = new ObservableCollection<int>();

        private void layoutroot_Checked(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is ToggleButton)
                buttons.Add(Convert.ToInt32(((ToggleButton)e.OriginalSource).Content));
        }

        private void layoutroot_Unchecked(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is ToggleButton)
            {
                int i = Convert.ToInt32(((ToggleButton)e.OriginalSource).Content);
                buttons.Remove(i);

            }
        }

        internal void ResetPattern()
        {
            if (buttons != null)
            {
                buttons.Clear();
                foreach (ToggleButton item in layoutroot.Children)
                {
                    item.IsChecked = false;
                }
            }

        }
    }

XAML

<UserControl x:Class="WPFTestings.LockPattern.PatternLock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>



        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}" />

            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Padding" Value="1" />
            <Setter Property="FontSize" Value="50" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="HoverOn">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="HoverBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.5" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="HoverShineBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="HoverOff">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="HoverBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="HoverShineBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="CheckedOn">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="CheckedBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.5" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="CheckedOff">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="CheckedBorder"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="PressedOn">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="Pressed"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                                </DoubleAnimationUsingKeyFrames>

                            </Storyboard>
                            <Storyboard x:Key="PressedOff">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                               Storyboard.TargetName="Pressed"
                                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </ControlTemplate.Resources>
                        <Grid x:Name="grid">

                            <Border x:Name="Border"
                                    Background="{DynamicResource NormalBrush}"
                                    BorderBrush="{DynamicResource NormalBorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="50"
                                    Padding="{TemplateBinding Padding}">
                                <Border.BitmapEffect>
                                    <OuterGlowBitmapEffect GlowColor="Red" GlowSize="10" />
                                </Border.BitmapEffect>
                            </Border>
                            <Border x:Name="CheckedBorder"
                                    Background="YellowGreen"
                                    BorderBrush="Green"
                                    BorderThickness="2"
                                    CornerRadius="50"
                                    Opacity="0"
                                    Padding="{TemplateBinding Padding}">
                                <Border.BitmapEffect>
                                    <OuterGlowBitmapEffect GlowColor="YellowGreen" GlowSize="10" />
                                </Border.BitmapEffect>
                            </Border>
                            <Border x:Name="HoverBorder"
                                    Background="YellowGreen"
                                    BorderBrush="{DynamicResource NormalBorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="50"
                                    Opacity="0"
                                    Padding="{TemplateBinding Padding}" />
                            <Border x:Name="HoverShineBorder"
                                    Background="{DynamicResource HoverShineBrush}"
                                    BorderBrush="{DynamicResource NormalBorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="50"
                                    Opacity="0"
                                    Padding="{TemplateBinding Padding}" />
                            <Border x:Name="Pressed"
                                    Background="YellowGreen"
                                    BorderBrush="Green"
                                    BorderThickness="2"
                                    CornerRadius="50"
                                    Opacity="0"
                                    Padding="{TemplateBinding Padding}" />
                            <Rectangle x:Name="Shine"
                                       Height="Auto"
                                       Margin="2,2,2,2"
                                       VerticalAlignment="Stretch"
                                       Opacity="1"
                                       RadiusX="3"
                                       RadiusY="3"
                                       Stroke="{x:Null}">
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0.5,0.042" EndPoint="0.5,0.971">
                                        <GradientStop Offset="0" Color="#26FFFFFF" />
                                        <GradientStop Offset="1" Color="#00FFFFFF" />
                                        <GradientStop Offset="0.467" Color="#26FFFFFF" />
                                        <GradientStop Offset="0.475" Color="#00FFFFFF" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>

                            <ContentPresenter Margin="4,4,4,4"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Trigger.ExitActions>
                                    <BeginStoryboard x:Name="HoverOff_BeginStoryboard" Storyboard="{StaticResource HoverOff}" />
                                </Trigger.ExitActions>
                                <Trigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource HoverOn}" />
                                </Trigger.EnterActions>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Trigger.ExitActions>
                                    <BeginStoryboard x:Name="PressedOff_BeginStoryboard" Storyboard="{StaticResource PressedOff}" />
                                </Trigger.ExitActions>
                                <Trigger.EnterActions>
                                    <BeginStoryboard x:Name="PressedOn_BeginStoryboard" Storyboard="{StaticResource PressedOn}" />
                                </Trigger.EnterActions>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true" />
                            <Trigger Property="IsChecked" Value="true">
                                <Trigger.ExitActions>
                                    <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
                                </Trigger.ExitActions>
                                <Trigger.EnterActions>
                                    <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
                                </Trigger.EnterActions>
                                <Setter TargetName="Pressed" Property="BitmapEffect">
                                    <Setter.Value>
                                        <OuterGlowBitmapEffect GlowColor="YellowGreen" GlowSize="10" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD" />
                                <Setter TargetName="Border" Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" />
                                <Setter TargetName="grid" Property="Opacity" Value="0.5" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource BlackColor}" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

    </UserControl.Resources>
    <UniformGrid x:Name="layoutroot"
                 Columns="3"
                 Rows="3"
                 ToggleButton.Checked="layoutroot_Checked"
                 ToggleButton.Unchecked="layoutroot_Unchecked">
        <ToggleButton x:Name="btn1"
                      Margin="5"
                      Content="1" />
        <ToggleButton x:Name="btn2"
                      Margin="5"
                      Content="2" />
        <ToggleButton x:Name="btn3"
                      Margin="5"
                      Content="3" />
        <ToggleButton x:Name="btn4"
                      Margin="5"
                      Content="4" />
        <ToggleButton x:Name="btn5"
                      Margin="5"
                      Content="5" />
        <ToggleButton x:Name="btn6"
                      Margin="5"
                      Content="6" />
        <ToggleButton x:Name="btn7"
                      Margin="5"
                      Content="7" />
        <ToggleButton x:Name="btn8"
                      Margin="5"
                      Content="8" />
        <ToggleButton x:Name="btn9"
                      Margin="5"
                      Content="9" />
    </UniformGrid>
</UserControl>

使用它是添加对话框和按钮来匹配它

   if (string.Concat(patternLock1.buttons).ToString() == "1359")
           {
               this.Close();
           }
           else
           {
               patternLock1.ResetPattern();
           }
于 2012-06-06T12:44:07.040 回答