2

Android パターンのロック画面を模倣する Visual Studio C# でアプリケーションを作成しようとしていますが、そのためのヒントがあればと思っていました。

これは商業的なプロジェクトではなく、お金を稼ぐことはありません。ほんの少しの楽しみです。

私は 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 に答える