WPF 一個訓練記憶力的小遊戲——記憶聯盟

                                   WPF 一個訓練記憶力的小遊戲——記憶聯盟

一、簡述

      記--一個用來訓練記憶力的小遊戲——記憶聯盟。

      例子打包:鏈接: https://pan.baidu.com/s/1VPpJ6WLKpSRpYaVClvynyQ 提取碼: af1u

二、效果

        挑戰模式:有挑戰時間限制,段位制

        

        訓練模式:   沒有訓練時間限制,沒有段位制。

          

        訓練區2:

         

三、工程結構

         使用VS2015專業版進行開發。          

四、源文件

        MainWindow.xaml文件

<Window x:Name="mainWindow" x:Class="MemoryAlliance.MainWindow"
        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"
        xmlns:local="clr-namespace:MemoryAlliance"
        mc:Ignorable="d"
        Title="記憶聯盟" Height="569" Width="602.934" WindowStartupLocation="CenterOwner" Loaded="Window_Loaded" Closing="Window_Closing" ResizeMode="CanMinimize" Icon="MemoryAlliance.ico">
    <Grid Margin="0,0,-0.2,3">
        <Button x:Name="button1" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40"/>
        <Button x:Name="button2" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,0,0,0"/>
        <Button x:Name="button3" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,0,0,0"/>
        <Button x:Name="button4" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,0,0,0"/>
        <Button x:Name="button5" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,0,0,0"/>
        <Button x:Name="button6" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,0,0,0"/>
        <Button x:Name="button7" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,0,0,0"/>
        <Button x:Name="button8" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,0,0,0"/>
        <Button x:Name="button9" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,0,0,0"/>
        <Button x:Name="button10" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,0,0,0"/>
        <Button x:Name="button11" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,39,0,0"/>
        <Button x:Name="button12" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,39,0,0"/>
        <Button x:Name="button13" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,39,0,0"/>
        <Button x:Name="button14" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,39,0,0"/>
        <Button x:Name="button15" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,39,0,0"/>
        <Button x:Name="button16" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,39,0,0"/>
        <Button x:Name="button17" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,39,0,0"/>
        <Button x:Name="button18" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,39,0,0"/>
        <Button x:Name="button19" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,39,0,0"/>
        <Button x:Name="button20" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,39,0,0"/>
        <Button x:Name="button21" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,78,0,0"/>
        <Button x:Name="button22" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,78,0,0"/>
        <Button x:Name="button23" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,78,0,0"/>
        <Button x:Name="button24" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,78,0,0"/>
        <Button x:Name="button25" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,78,0,0"/>
        <Button x:Name="button26" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,78,0,0"/>
        <Button x:Name="button27" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,78,0,0"/>
        <Button x:Name="button28" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,78,0,0"/>
        <Button x:Name="button29" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,78,0,0"/>
        <Button x:Name="button30" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,78,0,0"/>
        <Button x:Name="button31" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,117,0,0"/>
        <Button x:Name="button32" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,117,0,0"/>
        <Button x:Name="button33" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,117,0,0"/>
        <Button x:Name="button34" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,117,0,0"/>
        <Button x:Name="button35" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,117,0,0"/>
        <Button x:Name="button36" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,117,0,0"/>
        <Button x:Name="button37" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,117,0,0"/>
        <Button x:Name="button38" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,117,0,0"/>
        <Button x:Name="button39" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,117,0,0"/>
        <Button x:Name="button40" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,117,0,0"/>
        <Button x:Name="button41" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,156,0,0"/>
        <Button x:Name="button42" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,156,0,0"/>
        <Button x:Name="button43" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,156,0,0"/>
        <Button x:Name="button44" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,156,0,0"/>
        <Button x:Name="button45" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,156,0,0"/>
        <Button x:Name="button46" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,156,0,0"/>
        <Button x:Name="button47" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,156,0,0"/>
        <Button x:Name="button48" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,156,0,0"/>
        <Button x:Name="button49" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,156,0,0"/>
        <Button x:Name="button50" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,156,0,0"/>
        <Button x:Name="button51" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,195,0,0"/>
        <Button x:Name="button52" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,195,0,0"/>
        <Button x:Name="button53" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,195,0,0"/>
        <Button x:Name="button54" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,195,0,0"/>
        <Button x:Name="button55" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,195,0,0"/>
        <Button x:Name="button56" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,195,0,0"/>
        <Button x:Name="button57" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,195,0,0"/>
        <Button x:Name="button58" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,195,0,0"/>
        <Button x:Name="button59" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,195,0,0"/>
        <Button x:Name="button60" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,195,0,0"/>
        <Button x:Name="button61" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,234,0,0"/>
        <Button x:Name="button62" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,234,0,0"/>
        <Button x:Name="button63" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,234,0,0"/>
        <Button x:Name="button64" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,234,0,0"/>
        <Button x:Name="button65" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,234,0,0"/>
        <Button x:Name="button66" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,234,0,0"/>
        <Button x:Name="button67" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,234,0,0"/>
        <Button x:Name="button68" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,234,0,0"/>
        <Button x:Name="button69" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,234,0,0"/>
        <Button x:Name="button70" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,234,0,0"/>
        <Button x:Name="button71" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,272,0,0"/>
        <Button x:Name="button72" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,272,0,0"/>
        <Button x:Name="button73" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,272,0,0"/>
        <Button x:Name="button74" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,272,0,0"/>
        <Button x:Name="button75" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,272,0,0"/>
        <Button x:Name="button76" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,272,0,0"/>
        <Button x:Name="button77" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,272,0,0"/>
        <Button x:Name="button78" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,272,0,0"/>
        <Button x:Name="button79" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,272,0,0"/>
        <Button x:Name="button80" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,272,0,0"/>
        <Button x:Name="button81" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,311,0,0"/>
        <Button x:Name="button82" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,311,0,0"/>
        <Button x:Name="button83" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,311,0,0"/>
        <Button x:Name="button84" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,311,0,0"/>
        <Button x:Name="button85" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,311,0,0"/>
        <Button x:Name="button86" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,311,0,0"/>
        <Button x:Name="button87" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,311,0,0"/>
        <Button x:Name="button88" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,311,0,0"/>
        <Button x:Name="button89" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,311,0,0"/>
        <Button x:Name="button90" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,311,0,0"/>
        <Button x:Name="button91" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="0,350,0,0"/>
        <Button x:Name="button92" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="40,350,0,0"/>
        <Button x:Name="button93" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="80,350,0,0"/>
        <Button x:Name="button94" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="120,350,0,0"/>
        <Button x:Name="button95" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="160,350,0,0"/>
        <Button x:Name="button96" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="200,350,0,0"/>
        <Button x:Name="button97" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="240,350,0,0"/>
        <Button x:Name="button98" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="280,350,0,0"/>
        <Button x:Name="button99" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="320,350,0,0"/>
        <Button x:Name="button100" BorderThickness="3,3,3,3" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="360,350,0,0"/>
        <Button x:Name="buttonStartTrain" Content="開始訓練" HorizontalAlignment="Left" Margin="481,344,0,0" VerticalAlignment="Top" Width="78" Height="37" Click="buttonStart_Click"/>
        <ComboBox x:Name="comboBoxTrainLevel" HorizontalAlignment="Left" Margin="481,319,0,0" VerticalAlignment="Top" Width="59" SelectedIndex="2" RenderTransformOrigin="-0.109,0.628"/>
        <Label x:Name="labelTrainLevel" Content="訓練難度:" HorizontalAlignment="Left" Margin="417,318,0,0" VerticalAlignment="Top" Width="61"/>
        <Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="545,318,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
        <Label x:Name="labelTrainLevelComment" Content="i" HorizontalAlignment="Left" Margin="548,316,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="數值越大難度越大,結果不計入段位" RenderTransformOrigin="-0.596,0.394"/>
        <Label x:Name="labelCurrentLevel" Content="當前段位:" HorizontalAlignment="Left" Margin="423,21,0,0" VerticalAlignment="Top" Width="64"/>
        <Label x:Name="labelCurrentLevelVal" Content="鉑金-3" HorizontalAlignment="Left" Margin="486,21,0,0" VerticalAlignment="Top" Width="45"/>
        <ComboBox x:Name="comboBoxChallengeLevel" HorizontalAlignment="Left" Margin="481,166,0,0" VerticalAlignment="Top" Width="53" SelectedIndex="0" RenderTransformOrigin="-0.109,0.628"/>
        <Label x:Name="labelChallengeLevel" Content="挑戰難度:" HorizontalAlignment="Left" Margin="418,164,0,0" VerticalAlignment="Top" Width="60"/>
        <Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="539,165,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
        <Label x:Name="labelChallengeLevelComment" Content="i" HorizontalAlignment="Left" Margin="543,163,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="" RenderTransformOrigin="36.693,0.564"/>
        <Label x:Name="labelTrainMemoryTime" Content="記憶時間:" HorizontalAlignment="Left" Margin="420,259,0,0" VerticalAlignment="Top" Width="61"/>
        <Border BorderBrush="#FFA39696" BorderThickness="1" HorizontalAlignment="Left" Height="21" Margin="537,259,0,0" VerticalAlignment="Top" Width="21" Background="#FFBAB6B6" CornerRadius="10" ToolTip=""/>
        <Label x:Name="labelTrainMemoryTimeCommnt" Content="i" HorizontalAlignment="Left" Margin="540,256,0,0" VerticalAlignment="Top" Width="15" Height="22" FontSize="13.333" FontWeight="Bold" ToolTip="難度爲n則默認記憶時間爲n+(n/7)*3秒" RenderTransformOrigin="36.587,0.5"/>
        <Label x:Name="labelMaxLevel" Content="最高段位:" HorizontalAlignment="Left" Margin="423,46,0,0" VerticalAlignment="Top" Width="64"/>
        <Label x:Name="labelTopLevelVal" Content="大師-3" HorizontalAlignment="Left" Margin="486,46,0,0" VerticalAlignment="Top" Width="51" ToolTip="曾經達到的最高段位"/>
        <Label x:Name="labelTrainMemoryTimeUint" Content="秒" HorizontalAlignment="Left" Margin="566,283,0,0" VerticalAlignment="Top" Width="15" Height="16" RenderTransformOrigin="1.104,0.215" Padding="0"/>
        <GroupBox x:Name="groupBox" Header="戰績" HorizontalAlignment="Left" Margin="414,1,0,0" VerticalAlignment="Top" Height="97" Width="172" Panel.ZIndex="-1" Grid.ColumnSpan="2"/>
        <Label x:Name="labelChallengeSuccess" Content="挑戰成功率:" HorizontalAlignment="Left" Margin="423,72,0,0" VerticalAlignment="Top" Width="74"/>
        <Label x:Name="labelChallengeSuccessVal" Content="70%" HorizontalAlignment="Left" Margin="493,72,0,0" VerticalAlignment="Top" Width="40"/>
        <Button x:Name="buttonChallengeDetail" Content="詳細" HorizontalAlignment="Left" Margin="540,72,0,0" VerticalAlignment="Top" Width="40" Height="20" Click="buttonChallengeDetail_Click" ToolTip="詳細的挑戰記錄"/>
        <ComboBox x:Name="comboBoxTrainMemoryTimeVal" HorizontalAlignment="Left" Margin="501,282,0,0" VerticalAlignment="Top" Width="58" SelectedIndex="1" RenderTransformOrigin="-0.109,0.628"/>
        <Label x:Name="labelRemainingTime" Content="剩餘記憶時間:" HorizontalAlignment="Left" Margin="423,129,0,0" VerticalAlignment="Top" Width="152" Height="30" Background="#FF9DE05D" Padding="5,9,5,5" Grid.ColumnSpan="2"/>
        <Label x:Name="labelRemainingTimeVal" Content="00:00:00" HorizontalAlignment="Left" Margin="515,133,0,0" VerticalAlignment="Top" Width="59" Height="24" Grid.ColumnSpan="2"/>
        <Button x:Name="buttonStartChallenge" Content="開始挑戰" HorizontalAlignment="Left" Margin="481,192,0,0" VerticalAlignment="Top" Width="78" Height="34" Click="buttonStartChallenge_Click" Grid.ColumnSpan="2"/>
        <RadioButton x:Name="radioButtonDefaultMemoryTime" Content="默認" HorizontalAlignment="Left" Margin="481,263,0,0" VerticalAlignment="Top" IsChecked="True"/>
        <RadioButton x:Name="radioButtonSelfDefMemoryTime" Content="" HorizontalAlignment="Left" Margin="481,287,0,0" VerticalAlignment="Top"/>
        <GroupBox x:Name="groupBoxTrain" Header="訓練區" HorizontalAlignment="Left" Margin="415,239,0,0" VerticalAlignment="Top" Height="150" Width="172" Panel.ZIndex="-1">
            <Button x:Name="buttonMoreTrain" Content="V" Margin="144,111,-0.8,3" RenderTransformOrigin="0.5,0.5" ToolTip="展開訓練2區" Padding="0" Click="buttonMoreTrain_Click" FontWeight="Bold" FontSize="9.333" HorizontalAlignment="Left" Width="16">
                <Button.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform Y="-0.4"/>
                    </TransformGroup>
                </Button.RenderTransform>
            </Button>
        </GroupBox>
        <GroupBox x:Name="groupBoxChallenge" Header="挑戰區" HorizontalAlignment="Left" Margin="414,107,0,0" VerticalAlignment="Top" Height="126" Width="172" Panel.ZIndex="-1" Grid.ColumnSpan="2">
            <Button x:Name="buttonFinishMemoryChallenge" Content="完成記憶" HorizontalAlignment="Left" Margin="0,73,0,0" VerticalAlignment="Top" Width="53" ToolTip="提前完成記憶,開始答題" Click="buttonFinishMemoryChallege_Click" IsEnabled="False"/>
        </GroupBox>
        <Button x:Name="buttonFinishMemory" Content="完成記憶" HorizontalAlignment="Left" Margin="422,353,0,0" VerticalAlignment="Top" Width="54" IsEnabled="False" Click="buttonFinishMemory_Click"/>
        <CheckBox x:Name="checkBoxNumber" Content="0-9" HorizontalAlignment="Left" Margin="75,420,0,0" VerticalAlignment="Top" ToolTip="要記憶的字符串包含數字" IsChecked="True"/>
        <CheckBox x:Name="checkBoxLowercaseAlphabet" Content="a-z" HorizontalAlignment="Left" Margin="127,420,0,0" VerticalAlignment="Top" ToolTip="要記憶的字符串包含小寫字母"/>
        <CheckBox x:Name="checkBoxUppercaseAlphabet" Content="A-Z" HorizontalAlignment="Left" Margin="179,420,0,0" VerticalAlignment="Top" ToolTip="要記憶的字符串包含大寫字母"/>
        <TextBlock x:Name="textBlockMemoryString" HorizontalAlignment="Left" Margin="16,441,0,0" TextWrapping="Wrap" Text="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" VerticalAlignment="Top" Height="33" Width="508" Background="#FF9DE05D"/>
        <TextBox x:Name="textBoxMemoryString" HorizontalAlignment="Left" Height="38" Margin="16,483,0,0" TextWrapping="Wrap" Text="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" VerticalAlignment="Top" Width="509" Background="#FFD5F9D4"/>
        <Button x:Name="buttonCheck" Content="驗證" HorizontalAlignment="Left" Margin="529,483,0,0" VerticalAlignment="Top" Width="53" Height="36" Click="buttonCheck_Click" ToolTip="驗證輸入與要記憶的字符串是否一致" IsDefault="True" Grid.ColumnSpan="2"/>
        <Button x:Name="buttonHideMemoryString" Content="隱藏" HorizontalAlignment="Left" Margin="529,441,0,0" VerticalAlignment="Top" Width="53" Height="33" Click="buttonHideMemoryString_Click" ToolTip="隱藏要記憶的字符串" Grid.ColumnSpan="2"/>
        <ComboBox x:Name="comboBoxMemoryCharCountVal" HorizontalAlignment="Left" Margin="328,415,0,0" VerticalAlignment="Top" Width="53" SelectedIndex="2" RenderTransformOrigin="-0.109,0.628"/>
        <Label x:Name="labelMemoryCharCount" Content="記憶字符個數:" HorizontalAlignment="Left" Margin="246,419,0,0" VerticalAlignment="Top" Width="82" Height="19" Padding="0"/>
        <Button x:Name="buttonCreateMemoeyString" Content="生成記憶字符串" HorizontalAlignment="Left" Margin="401,415,0,0" VerticalAlignment="Top" Width="99" ToolTip="生成要記憶的字符串" Click="buttonCreateMemoeyString_Click" Height="20"/>
        <Label x:Name="labelMemoryCharType" Content="字符類型:" HorizontalAlignment="Left" Margin="16,419,0,0" VerticalAlignment="Top" Width="54" Height="16" Padding="0"/>
        <GroupBox x:Name="groupBox1" Header="訓練2區" HorizontalAlignment="Left" Margin="5,398,0,0" VerticalAlignment="Top" Height="132" Width="582" Panel.ZIndex="-1" Grid.ColumnSpan="2"/>
    </Grid>
</Window>

        MainWindow.xaml.cs文件

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;

/*****************************************************
 * 
根據當前分值進行計算段位
等級 分值
青銅 0-7
白銀 8-15
黃金 16-30
鉑金 31-60
鑽石 61-80
大師 81-90
王者 91-100

往低級挑戰 成功不加分  失敗-1分 
級內挑戰  成功+1分  失敗-1分
越級挑戰 成功+1+級差分數 失敗-1分
 *
 *****************************************************/

namespace MemoryAlliance
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        int mErrIndex;//紅框Button索引
        int mNowIndex;//當前點擊索引1~mValCnt, mNowIndex==mValCnt時 訓練/挑戰完成
        int mValCnt;//數值個數
        int mTopVal;//數值上限
        int mHideValTimeOut;//定時器超時計數
        int mRemainTime;//剩餘時間 單位秒
        int mSpanTime;//完成用時 單位秒
        int mGameMode;//遊戲模式:1:挑戰模式 2:訓練模式
        int mProcess;//遊戲進度, 0結束  1進行中
        int mIsSaveConfigure;//是否保存配置數據,爲0時不保存--數據非法
        const int mHideTrain2Height = 424;//隱藏訓練2區時 的高度
        const int mShowTrain2Height = 569;//顯示訓練2區時 的高度
        string mConfFile = "MemoryAlliance.conf";
        ConfigureInfo mConfigure;//配置        
        ButtonManager mBtnManager;//按鈕統一管理者
        List<int> mIndexList;//數值位置索引集合
        List<char> mCharList;//訓練區2的記憶字符串集合
        DispatcherTimer mHideValTimer;//隱藏數值的定時器
        DispatcherTimer mRemainTimeTimer;//剩餘時間的定時器

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void init()
        {            
            mProcess = 0;
            mGameMode = 0;
            mErrIndex = 0;
            mNowIndex = 0;
            mValCnt = 5;
            mRemainTime = 5;
            mTopVal = 100;
            mHideValTimeOut = mValCnt;
            mIsSaveConfigure = 1;
            mBtnManager = new ButtonManager();
            initBtnManager();

            //初始挑戰練難度選擇列表框
            comboBoxChallengeLevel.Items.Add("青銅");//3-9
            comboBoxChallengeLevel.Items.Add("白銀");//8-15
            comboBoxChallengeLevel.Items.Add("黃金");//16-30
            comboBoxChallengeLevel.Items.Add("鉑金");//31-60
            comboBoxChallengeLevel.Items.Add("鑽石");//61-80
            comboBoxChallengeLevel.Items.Add("大師");//81-90
            comboBoxChallengeLevel.Items.Add("王者");//91-100

            //初始化記憶時間選擇列表框
            comboBoxTrainMemoryTimeVal.Items.Add(3);
            comboBoxTrainMemoryTimeVal.Items.Add(5);
            comboBoxTrainMemoryTimeVal.Items.Add(10);
            comboBoxTrainMemoryTimeVal.Items.Add(20);
            comboBoxTrainMemoryTimeVal.Items.Add(30);
            comboBoxTrainMemoryTimeVal.Items.Add(40);
            comboBoxTrainMemoryTimeVal.Items.Add(50);
            comboBoxTrainMemoryTimeVal.Items.Add(60);
            comboBoxTrainMemoryTimeVal.Items.Add(90);
            comboBoxTrainMemoryTimeVal.Items.Add(120);
            comboBoxTrainMemoryTimeVal.Items.Add(200);
            comboBoxTrainMemoryTimeVal.Items.Add(300);

            //初始化訓練難度選擇列表框
            for (int i = 3; i <= mTopVal; i++)
            {
                comboBoxTrainLevel.Items.Add(i);
                comboBoxMemoryCharCountVal.Items.Add(i);
            }

            //初始化記憶時間定時器
            mHideValTimer = new DispatcherTimer();            
            mHideValTimer.Tick += hideValTimer_Tick;

            //初始化Button的點擊響應事件
            initBtnClickEvent();

            //初始化剩餘時間定時器
            mRemainTimeTimer = new DispatcherTimer();
            mRemainTimeTimer.Tick += remainTimeTimer_Tick;
            mRemainTimeTimer.Interval = TimeSpan.FromMilliseconds(1 * 1000);

            //初始化記憶字符串集合
            string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            mCharList = new List<char>();
            mCharList.AddRange(allChar);

        }

        /// <summary>
        /// 計算挑戰成功率
        /// </summary>
        /// <param name="rank">0-7, other:all rank</param>
        /// <returns>挑戰成功率,結果乘以100倍</returns>
        private int getSuccessRate(int rank)
        {
            int rate = 0;
            int totalSuccess = 0;
            int totalFailed = 0;
            if (rank == 0)
            {
                totalSuccess = mConfigure.challengeLevel0Success;
                totalFailed = mConfigure.challengeLevel0Failed;
            }
            else if (rank == 1)
            {
                totalSuccess = mConfigure.challengeLevel1Success;
                totalFailed = mConfigure.challengeLevel1Failed;
            }
            else if (rank == 2)
            {
                totalSuccess = mConfigure.challengeLevel2Success;
                totalFailed = mConfigure.challengeLevel2Failed;
            }
            else if (rank == 3)
            {
                totalSuccess = mConfigure.challengeLevel3Success;
                totalFailed = mConfigure.challengeLevel3Failed;
            }
            else if (rank == 4)
            {
                totalSuccess = mConfigure.challengeLevel4Success;
                totalFailed = mConfigure.challengeLevel4Failed;
            }
            else if (rank == 5)
            {
                totalSuccess = mConfigure.challengeLevel5Success;
                totalFailed = mConfigure.challengeLevel5Failed;
            }
            else if (rank == 6)
            {
                totalSuccess = mConfigure.challengeLevel6Success;
                totalFailed = mConfigure.challengeLevel6Failed;
            }
            else//計算總的
            {
                totalSuccess = mConfigure.getTotalSuccess();
                totalFailed = mConfigure.getTotalFailed();                
            }

            if (totalSuccess <= 0)
            {
                rate = 0;
            }
            else
            {
                double total = totalSuccess + totalFailed;
                rate = (int)((totalSuccess / total) * 100);
            }

            return rate;
        }

        /// <summary>
        /// 設置總挑戰成功率
        /// </summary>
        private void setTotalChallengeSuccessRate()
        {
            int rate = getSuccessRate(8);
            if (rate > 0)
            {
                labelChallengeSuccessVal.Content = rate + "%";
            }
            else
            {
                labelChallengeSuccessVal.Content = "0%";
            }
        }

        /// <summary>
        /// 加載配置
        /// </summary>
        private int loadConfigure()
        {
            int ret = Common.readConfigure(mConfFile, out mConfigure);

            if (ret != -1)
            {
                int level = 0;

                //顯示當前段位
                labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);

                //根據當前水平設置挑戰難度
                //comboBoxChallengeLevel.SelectedIndex = level;

                //顯示最高段位
                labelTopLevelVal.Content = getLevelString(mConfigure.topLevel, out level);

                //設置挑戰成功率           
                setTotalChallengeSuccessRate();

                //設置記憶時間
                if (0 >= mConfigure.memoryTime)//默認
                {
                    radioButtonDefaultMemoryTime.IsChecked = true;
                }
                else
                {
                    radioButtonSelfDefMemoryTime.IsChecked = true;
                    comboBoxTrainMemoryTimeVal.SelectedValue = mConfigure.memoryTime;
                }

                //設置訓練難度
                if (mConfigure.trainLevel < 3)
                {
                    mConfigure.trainLevel = 5;
                }
                comboBoxTrainLevel.SelectedValue = mConfigure.trainLevel;

                //設置字符類型 charType=0-7
                if ((mConfigure.charType & 1) == 1)//選擇了數字
                {
                    checkBoxNumber.IsChecked = true;
                }
                else
                {
                    checkBoxNumber.IsChecked = false;
                }

                if ((mConfigure.charType & 2) == 2)//選擇了小寫字母
                {
                    checkBoxLowercaseAlphabet.IsChecked = true;
                }
                else
                {
                    checkBoxLowercaseAlphabet.IsChecked = false;
                }

                if ((mConfigure.charType & 4) == 4)//選擇了大寫字母
                {
                    checkBoxUppercaseAlphabet.IsChecked = true;
                }
                else
                {
                    checkBoxUppercaseAlphabet.IsChecked = false;
                }

                //設置字符個數
                if (mConfigure.charCnt < 3)
                {
                    mConfigure.charCnt = 5;
                }

                comboBoxMemoryCharCountVal.SelectedValue = mConfigure.charCnt;
            }

            return ret;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <returns></returns>
        private bool saveConfigure()
        {
            //保存當前挑戰難度
            mConfigure.challengeLevel =  comboBoxChallengeLevel.SelectedIndex;
                        
            //保存記憶時間
            if ((bool)radioButtonDefaultMemoryTime.IsChecked)//默認
            {
                mConfigure.memoryTime = 0;
            }
            else
            {
                if (null != comboBoxTrainMemoryTimeVal.SelectedValue)
                {
                    mConfigure.memoryTime = (int)comboBoxTrainMemoryTimeVal.SelectedValue;
                }
                mConfigure.memoryTime = 0;
            }

            //保存訓練難度
            mConfigure.trainLevel = (int)comboBoxTrainLevel.SelectedValue;

            //保存字符類型 charType=0-7   
            mConfigure.charType = 0;
            if ((bool)checkBoxNumber.IsChecked)//選擇了數字
            {
                mConfigure.charType = 1;
            }

            if ((bool)checkBoxLowercaseAlphabet.IsChecked)//選擇了小寫字母
            {
                mConfigure.charType += 2;
            }

            if ((bool)checkBoxUppercaseAlphabet.IsChecked)//選擇了大寫字母
            {
                mConfigure.charType += 4;
            }

            //保存字符個數
            if (mConfigure.charCnt < 3)
            {
                mConfigure.charCnt = 5;
            }
            mConfigure.charCnt = (int)comboBoxMemoryCharCountVal.SelectedValue;

            Common.saveConfigure(mConfFile, mConfigure);

            return true;
        }


        #region  //在左上角的系統菜單添加 "關於"記憶聯盟""  需要調用系統API接口

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);//獲取系統菜單
        [DllImport("user32.dll")]
        private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);//添加菜單項

        //一些系統定義的常量值 (詳情請參考Windows API)
        public const int WM_SYSCOMMAND = 0x0112;
        public const int MF_SEPARATOR = 0x800;
        public const int MF_BYPOSITION = 0x400;

        //自定義消息一般在0x0400之後
        public const int MENU_ABOUT = 0x0470;

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        //處理各種窗口消息(詳情請參考Windows API)
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //分流處理各種消息
            switch (msg)
            {
                case WM_SYSCOMMAND://系統命令
                    int id = wParam.ToInt32() & 0xFFFF;
                    switch(id)
                    {
                        case MENU_ABOUT: //關於
                            handled = true;//表示已經處理,不用系統處理了
                            string text = "【簡介】 這是一個用來訓練記憶力的小遊戲。"
                                    + "\n\n【遊戲玩法】"
                                    + "\n  挑戰區:在挑戰時間內按順序將‘數字’點開。完成挑戰,段位提升;否則降低段位。"
                                    + "\n  訓練區:按照順序將‘數字’點開。沒有時間限制,不影響段位。"
                                    + "\n  訓練2區:隨機生成一串字符,記憶後隱藏它,然後將它默寫出來。"
                                    + "\n\n【段位等級說明】  (以下由低到高進行排序)"
                                    + "\n  新手、青銅、白銀、黃金、鉑金、鑽石、大師、王者"
                                    + "\n  除新手外每級細分爲3小級,如青銅級細分爲:青銅-3,青銅-2,青銅-1;其它等級類似。3小級中1級比2級要高。";
                            MessageBox.Show(text, "關於\"記憶聯盟\"");
                            break;
                    }
                    break;

            }
            handled = false;
            return IntPtr.Zero;
        }
      
        #endregion


        /// <summary>
        /// 窗體加載完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //添加 關於"記憶聯盟"
            WindowInteropHelper helper = new WindowInteropHelper((Window)sender);
            IntPtr sysMenuHandle = GetSystemMenu(helper.Handle, false);
            InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);//添加分割線
            InsertMenu(sysMenuHandle, 6, MF_BYPOSITION, MENU_ABOUT, "關於\"記憶聯盟\"");//添加一項菜單項

            init();

            //加載配置
            int ret = loadConfigure();
            if (-1 == ret)
            {
                MessageBox.Show("數據已被損壞!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                mIsSaveConfigure = 0;
                this.Close();//關閉窗體
            }

            //隱藏訓練2區
            Height = mHideTrain2Height;

            //設置挑戰說明:
            labelChallengeLevelComment.ToolTip = "1. 成功挑戰本段位或高段位難度,段位提升,難度越高提升越多;"
                                               + "\n2. 成功挑戰低於當前段位難度的不提升段位;"
                                               + "\n3. 任何難度挑戰失敗會降低段位;";
            //設置挑戰難度
            comboBoxChallengeLevel.SelectedIndex = mConfigure.challengeLevel;
        }

        /// <summary>
        /// mHideValTimer定時器執行內容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hideValTimer_Tick(object sender, EventArgs e)
        {
            //關閉定時器
            mHideValTimer.Stop();

            //隱藏所有值
            hideBtnVal();                    

            //完成記憶按鈕虛化
            buttonFinishMemory.IsEnabled = false;
            buttonFinishMemoryChallenge.IsEnabled = false;
            
            //開始挑戰倒計時
            if (1 == mGameMode)
            {
                labelRemainingTime.Content = "剩餘挑戰時間:";
                initRemainTime(2 * mValCnt);
                mRemainTimeTimer.Start();
            }
            else if (2 == mGameMode)//訓練模式 用來統計完成用時
            {
                mSpanTime = 0;
                mRemainTimeTimer.Start();
            }

            //遊戲進行中
            mProcess = 1;
        }

        /// <summary>
        /// 剩餘時間定時器 每秒一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void remainTimeTimer_Tick(object sender, EventArgs e)
        {
            mSpanTime++;
            if (1 == mGameMode)
            {
                if (mSpanTime == mRemainTime)
                {
                    mRemainTimeTimer.Stop();
                    //更新剩餘時間
                    updateRemainTime(mRemainTime, mSpanTime);

                    //挑戰時間到
                    buttonStartChallenge_Click(buttonStartChallenge, null);
                    if (labelRemainingTime.Content.Equals("剩餘挑戰時間:"))
                    {
                        MessageBox.Show("用時超時,挑戰失敗! 已經完成"+ mNowIndex + "/" + mValCnt +"。", "挑戰失敗");
                    }

                }
                else
                {
                    //更新剩餘時間
                    updateRemainTime(mRemainTime, mSpanTime);
                }

                
            }           
        }

        /// <summary>
        /// 更新剩餘時間
        /// </summary>
        /// <param name="totalTime">總時間</param>
        /// <param name="spanTime">用時</param>
        private void updateRemainTime(int totalTime, int spanTime)
        {
            int time = totalTime - spanTime;
            int hour = time / 3600;
            int min = (time % 3600) / 60;
            int sec = (time % 60);
            labelRemainingTimeVal.Content = string.Format("{0:D2}", hour) + string.Format(":{0:D2}", min) + string.Format(":{0:D2}", sec);

        }

        /// <summary>
        /// 隱藏數值
        /// </summary>
        private void hideBtnVal()
        {
            if (null != mIndexList)
            {
                for (int index = 0; index < mIndexList.Count; index++)
                {
                    mBtnManager.setBtnVal(mIndexList[index], "");
                    mBtnManager.setBtnBackground(mIndexList[index], Brushes.Black);
                }
            }            
        }

        /// <summary>
        /// 初始化Button管理者
        /// </summary>
        private void initBtnManager()
        {
            mBtnManager.mDefaultBackground = button12.Background;
            mBtnManager.mDefaultBorderBrush = button12.BorderBrush;
            mBtnManager.mDefaultThickness = button12.BorderThickness;
            mBtnManager.addBtn(button1);
            mBtnManager.addBtn(button2);
            mBtnManager.addBtn(button3);
            mBtnManager.addBtn(button4);
            mBtnManager.addBtn(button5);
            mBtnManager.addBtn(button6);
            mBtnManager.addBtn(button7);
            mBtnManager.addBtn(button8);
            mBtnManager.addBtn(button9);
            mBtnManager.addBtn(button10);

            mBtnManager.addBtn(button11);
            mBtnManager.addBtn(button12);
            mBtnManager.addBtn(button13);
            mBtnManager.addBtn(button14);
            mBtnManager.addBtn(button15);
            mBtnManager.addBtn(button16);
            mBtnManager.addBtn(button17);
            mBtnManager.addBtn(button18);
            mBtnManager.addBtn(button19);
            mBtnManager.addBtn(button20);

            mBtnManager.addBtn(button21);
            mBtnManager.addBtn(button22);
            mBtnManager.addBtn(button23);
            mBtnManager.addBtn(button24);
            mBtnManager.addBtn(button25);
            mBtnManager.addBtn(button26);
            mBtnManager.addBtn(button27);
            mBtnManager.addBtn(button28);
            mBtnManager.addBtn(button29);
            mBtnManager.addBtn(button30);

            mBtnManager.addBtn(button31);
            mBtnManager.addBtn(button32);
            mBtnManager.addBtn(button33);
            mBtnManager.addBtn(button34);
            mBtnManager.addBtn(button35);
            mBtnManager.addBtn(button36);
            mBtnManager.addBtn(button37);
            mBtnManager.addBtn(button38);
            mBtnManager.addBtn(button39);
            mBtnManager.addBtn(button40);

            mBtnManager.addBtn(button41);
            mBtnManager.addBtn(button42);
            mBtnManager.addBtn(button43);
            mBtnManager.addBtn(button44);
            mBtnManager.addBtn(button45);
            mBtnManager.addBtn(button46);
            mBtnManager.addBtn(button47);
            mBtnManager.addBtn(button48);
            mBtnManager.addBtn(button49);
            mBtnManager.addBtn(button50);

            mBtnManager.addBtn(button51);
            mBtnManager.addBtn(button52);
            mBtnManager.addBtn(button53);
            mBtnManager.addBtn(button54);
            mBtnManager.addBtn(button55);
            mBtnManager.addBtn(button56);
            mBtnManager.addBtn(button57);
            mBtnManager.addBtn(button58);
            mBtnManager.addBtn(button59);
            mBtnManager.addBtn(button60);

            mBtnManager.addBtn(button61);
            mBtnManager.addBtn(button62);
            mBtnManager.addBtn(button63);
            mBtnManager.addBtn(button64);
            mBtnManager.addBtn(button65);
            mBtnManager.addBtn(button66);
            mBtnManager.addBtn(button67);
            mBtnManager.addBtn(button68);
            mBtnManager.addBtn(button69);
            mBtnManager.addBtn(button70);

            mBtnManager.addBtn(button71);
            mBtnManager.addBtn(button72);
            mBtnManager.addBtn(button73);
            mBtnManager.addBtn(button74);
            mBtnManager.addBtn(button75);
            mBtnManager.addBtn(button76);
            mBtnManager.addBtn(button77);
            mBtnManager.addBtn(button78);
            mBtnManager.addBtn(button79);
            mBtnManager.addBtn(button80);

            mBtnManager.addBtn(button81);
            mBtnManager.addBtn(button82);
            mBtnManager.addBtn(button83);
            mBtnManager.addBtn(button84);
            mBtnManager.addBtn(button85);
            mBtnManager.addBtn(button86);
            mBtnManager.addBtn(button87);
            mBtnManager.addBtn(button88);
            mBtnManager.addBtn(button89);
            mBtnManager.addBtn(button90);

            mBtnManager.addBtn(button91);
            mBtnManager.addBtn(button92);
            mBtnManager.addBtn(button93);
            mBtnManager.addBtn(button94);
            mBtnManager.addBtn(button95);
            mBtnManager.addBtn(button96);
            mBtnManager.addBtn(button97);
            mBtnManager.addBtn(button98);
            mBtnManager.addBtn(button99);
            mBtnManager.addBtn(button100);
        }

        /// <summary>
        /// 初始化Button的點擊事件
        /// </summary>
        private void initBtnClickEvent()
        {
            button1.Click += new RoutedEventHandler(button_Click);
            button2.Click += new RoutedEventHandler(button_Click);
            button3.Click += new RoutedEventHandler(button_Click);
            button4.Click += new RoutedEventHandler(button_Click);
            button5.Click += new RoutedEventHandler(button_Click);
            button6.Click += new RoutedEventHandler(button_Click);
            button7.Click += new RoutedEventHandler(button_Click);
            button8.Click += new RoutedEventHandler(button_Click);
            button9.Click += new RoutedEventHandler(button_Click);
            button10.Click += new RoutedEventHandler(button_Click); 
                                  
            button11.Click += new RoutedEventHandler(button_Click);
            button12.Click += new RoutedEventHandler(button_Click);
            button13.Click += new RoutedEventHandler(button_Click);
            button14.Click += new RoutedEventHandler(button_Click);
            button15.Click += new RoutedEventHandler(button_Click);
            button16.Click += new RoutedEventHandler(button_Click);
            button17.Click += new RoutedEventHandler(button_Click);
            button18.Click += new RoutedEventHandler(button_Click);
            button19.Click += new RoutedEventHandler(button_Click);
            button20.Click += new RoutedEventHandler(button_Click);
            button21.Click += new RoutedEventHandler(button_Click);

            button22.Click += new RoutedEventHandler(button_Click);
            button23.Click += new RoutedEventHandler(button_Click);
            button24.Click += new RoutedEventHandler(button_Click);
            button25.Click += new RoutedEventHandler(button_Click);
            button26.Click += new RoutedEventHandler(button_Click);
            button27.Click += new RoutedEventHandler(button_Click);
            button28.Click += new RoutedEventHandler(button_Click);
            button29.Click += new RoutedEventHandler(button_Click);
            button30.Click += new RoutedEventHandler(button_Click);
            button31.Click += new RoutedEventHandler(button_Click);

            button32.Click += new RoutedEventHandler(button_Click);
            button33.Click += new RoutedEventHandler(button_Click);
            button34.Click += new RoutedEventHandler(button_Click);
            button35.Click += new RoutedEventHandler(button_Click);
            button36.Click += new RoutedEventHandler(button_Click);
            button37.Click += new RoutedEventHandler(button_Click);
            button38.Click += new RoutedEventHandler(button_Click);
            button39.Click += new RoutedEventHandler(button_Click);
            button40.Click += new RoutedEventHandler(button_Click);

            button41.Click += new RoutedEventHandler(button_Click);
            button42.Click += new RoutedEventHandler(button_Click);
            button43.Click += new RoutedEventHandler(button_Click);
            button44.Click += new RoutedEventHandler(button_Click);
            button45.Click += new RoutedEventHandler(button_Click);
            button46.Click += new RoutedEventHandler(button_Click);
            button47.Click += new RoutedEventHandler(button_Click);
            button48.Click += new RoutedEventHandler(button_Click);
            button49.Click += new RoutedEventHandler(button_Click);
            button50.Click += new RoutedEventHandler(button_Click);

            button51.Click += new RoutedEventHandler(button_Click);
            button52.Click += new RoutedEventHandler(button_Click);
            button53.Click += new RoutedEventHandler(button_Click);
            button54.Click += new RoutedEventHandler(button_Click);
            button55.Click += new RoutedEventHandler(button_Click);
            button56.Click += new RoutedEventHandler(button_Click);
            button57.Click += new RoutedEventHandler(button_Click);
            button58.Click += new RoutedEventHandler(button_Click);
            button59.Click += new RoutedEventHandler(button_Click);
            button60.Click += new RoutedEventHandler(button_Click);

            button61.Click += new RoutedEventHandler(button_Click);
            button62.Click += new RoutedEventHandler(button_Click);
            button63.Click += new RoutedEventHandler(button_Click);
            button64.Click += new RoutedEventHandler(button_Click);
            button65.Click += new RoutedEventHandler(button_Click);
            button66.Click += new RoutedEventHandler(button_Click);
            button67.Click += new RoutedEventHandler(button_Click);
            button68.Click += new RoutedEventHandler(button_Click);
            button69.Click += new RoutedEventHandler(button_Click);
            button70.Click += new RoutedEventHandler(button_Click);

            button71.Click += new RoutedEventHandler(button_Click);
            button72.Click += new RoutedEventHandler(button_Click);
            button73.Click += new RoutedEventHandler(button_Click);
            button74.Click += new RoutedEventHandler(button_Click);
            button75.Click += new RoutedEventHandler(button_Click);
            button76.Click += new RoutedEventHandler(button_Click);
            button77.Click += new RoutedEventHandler(button_Click);
            button78.Click += new RoutedEventHandler(button_Click);
            button79.Click += new RoutedEventHandler(button_Click);
            button80.Click += new RoutedEventHandler(button_Click);

            button81.Click += new RoutedEventHandler(button_Click);
            button82.Click += new RoutedEventHandler(button_Click);
            button83.Click += new RoutedEventHandler(button_Click);
            button84.Click += new RoutedEventHandler(button_Click);
            button85.Click += new RoutedEventHandler(button_Click);
            button86.Click += new RoutedEventHandler(button_Click);
            button87.Click += new RoutedEventHandler(button_Click);
            button88.Click += new RoutedEventHandler(button_Click);
            button89.Click += new RoutedEventHandler(button_Click);
            button90.Click += new RoutedEventHandler(button_Click);

            button91.Click += new RoutedEventHandler(button_Click);
            button92.Click += new RoutedEventHandler(button_Click);
            button93.Click += new RoutedEventHandler(button_Click);
            button94.Click += new RoutedEventHandler(button_Click);
            button95.Click += new RoutedEventHandler(button_Click);
            button96.Click += new RoutedEventHandler(button_Click);
            button97.Click += new RoutedEventHandler(button_Click);
            button98.Click += new RoutedEventHandler(button_Click);
            button99.Click += new RoutedEventHandler(button_Click);
            button100.Click += new RoutedEventHandler(button_Click);
        }
        
        /// <summary>
        /// Button點擊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (mProcess != 1)//未開始遊戲,不響應
            {
                return;
            }

            Button btn = (Button)sender;
            btn.Background = mBtnManager.mDefaultBackground;

            string val = btn.Name.Remove(0, 6);//"button" len is 6
            int clickIndex = Convert.ToInt32(val);

            if (mIndexList != null)
            {
                int i = mIndexList.IndexOf(clickIndex);
                if (i >= 0 && mBtnManager.get(mIndexList[i]).Content.Equals(""))
                {
                    mBtnManager.setBtnVal(mIndexList[i], i + 1);//恢復原值
                    mBtnManager.get(mIndexList[i]).BorderBrush = new SolidColorBrush(Colors.Green);
                    if (clickIndex == mIndexList[mNowIndex])//按順序點對了
                    {
                        mNowIndex++;
                        if (mNowIndex == mValCnt)//完成
                        {
                            closeAllTimer();
                            buttonStartTrain.Content = "開始訓練";
                            buttonStartChallenge.Content = "開始挑戰";
                            buttonStartTrain.IsEnabled = true;
                            buttonStartChallenge.IsEnabled = true;
                            buttonFinishMemory.IsEnabled = false;
                            comboBoxChallengeLevel.IsEnabled = true;
                            buttonFinishMemoryChallenge.IsEnabled = false;                                                       

                            if (1 == mGameMode)
                            {
                                challengeSuccess(mSpanTime);
                                MessageBox.Show("恭喜您挑戰成功!用時" + mSpanTime + "秒。", "挑戰成功");
                            }
                            else{
                                MessageBox.Show("恭喜您完成訓練!用時" + mSpanTime + "秒。", "訓練完成");
                            }
                            mProcess = 0;                            
                        }
                    }
                    else//點錯了
                    {
                        closeAllTimer();
                        mErrIndex = mIndexList[i];
                        mBtnManager.clearAllBtnVal();

                        mBtnManager.get(mIndexList[i]).BorderBrush = new SolidColorBrush(Colors.Red);
                        for (i=0;i<mValCnt;i++)//恢復所有原值
                        {
                            mBtnManager.setBtnVal(mIndexList[i], i + 1);
                        }

                        buttonStartTrain.IsEnabled = true;
                        buttonStartChallenge.IsEnabled = true;
                        buttonFinishMemory.IsEnabled = false;
                        buttonFinishMemoryChallenge.IsEnabled = false;

                        buttonStartTrain.Content = "開始訓練";
                        buttonStartChallenge.Content = "開始挑戰";
                        comboBoxChallengeLevel.IsEnabled = true;                        

                        if (1 == mGameMode)
                        {
                            challengeFailed();                           
                            MessageBox.Show("啊哦,記憶錯誤! 已完成"+ mNowIndex+"/"+mValCnt +"。", "挑戰失敗");
                        }
                        else
                        {
                            MessageBox.Show("啊哦,記憶錯誤! 已完成" + mNowIndex + "/" + mValCnt + "。", "訓練失敗");
                        }

                        mProcess = 0;                       
                    }
                    //點到沒有值的Button 不響應
                }

                
            }                     
        }

        
        /// <summary>
        /// 挑戰成功
        /// </summary>
        /// <param name="spanTime">完成用時</param>
        private void challengeSuccess(int spanTime)
        {
            int level = 0;
            int challengeLevel = 0;
            getLevelString(mConfigure.curLevel, out level);
            challengeLevel = comboBoxChallengeLevel.SelectedIndex;//挑戰等級

            int sucess = 0;
            int failed = 0;
            int minTime = 0;
            mConfigure.getRankInfo(level, out sucess, out failed, out minTime);

            //更新最短同用時    
            if (0 != minTime && spanTime > minTime)
            {
                spanTime = -1;
            }

            //挑戰成功數+1
            sucess++;
            mConfigure.setRankInfo(level, sucess, -1, spanTime);

            //加1分 低級不加分,同級加1分, 越級1+級差 
            if (challengeLevel >= level)
            {
                //加分
                mConfigure.curLevel += 1 + challengeLevel - level;
                                
                //更新最高段位
                if (mConfigure.curLevel > mConfigure.topLevel)
                {
                    mConfigure.topLevel = mConfigure.curLevel;
                    labelTopLevelVal.Content = getLevelString(mConfigure.topLevel, out level);
                    //晉級時間 20191228
                    string date = DateTime.Now.ToLocalTime().ToString("yyyyMMdd");
                    mConfigure.topLevelTime = Convert.ToInt32(date);
                }

                //更新當前段位
                labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);


                //根據當前段位設置挑戰難度
                comboBoxChallengeLevel.SelectedIndex = level;
            }

            //更新挑戰成功率
            setTotalChallengeSuccessRate();

            //保存
            saveConfigure();
        }

        /// <summary>
        /// 挑戰失敗
        /// </summary>
        private void challengeFailed()
        {
            int level = 0;
            int challengeLevel = 0;
            getLevelString(mConfigure.curLevel, out level);
            challengeLevel = comboBoxChallengeLevel.SelectedIndex;//挑戰等級

            int sucess = 0;
            int failed = 0;
            int minTime = 0;
            mConfigure.getRankInfo(level, out sucess, out failed, out minTime);

            //挑戰失敗數+1
            failed++;
            mConfigure.setRankInfo(challengeLevel, -1, failed, -1);

            //扣一分
            mConfigure.curLevel--;
            if (mConfigure.curLevel < 0)
            {
                mConfigure.curLevel = 0;
            }

            //更新當前段位
            labelCurrentLevelVal.Content = getLevelString(mConfigure.curLevel, out level);

            //根據當前段位設置挑戰難度
            comboBoxChallengeLevel.SelectedIndex = level;

            //更新挑戰成功率
            setTotalChallengeSuccessRate();

            //保存
            saveConfigure();
        }

        /// <summary>
        /// 清除Button的紅框和綠框
        /// </summary>
        private void clearButtonRegAndGreenBorder()
        {
            //清除上一次紅框
            mBtnManager.setDefaultStyle(mErrIndex);

            if (null != mIndexList)
            {
                //清除上一次綠框
                for (int i = 0; i < mNowIndex && i< mIndexList.Count; i++)
                {
                    mBtnManager.setDefaultStyle(mIndexList[i]);
                }
            }
        }

        /// <summary>
        /// 開始訓練
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            mGameMode = 2;
            string text = buttonStartTrain.Content.ToString();
            closeAllTimer();

            mProcess = 0;
            if (text.Equals("開始訓練"))
            {                
                buttonFinishMemory.IsEnabled = true;
                buttonStartChallenge.IsEnabled = false;
                buttonStartTrain.Content = "結束訓練";

                //清除紅綠邊框
                clearButtonRegAndGreenBorder();

                mNowIndex = 0;

                //訓練難度
                mValCnt = (int)comboBoxTrainLevel.SelectedValue;

                //獲取唯一索引集合
                List<int> indexList = Common.genUniqueRandomVal(1, mTopVal, mValCnt);
                if (indexList == null)
                {
                    return;
                }

                if (null != mIndexList)
                {
                    mIndexList.Clear();
                }

                mBtnManager.clearAllBtnVal();
                mIndexList = indexList;

                //顯示值
                for (int index = 0, i = 1; index < mIndexList.Count; index++)
                {
                    mBtnManager.setBtnVal(mIndexList[index], i);
                    i++;
                }

                //記憶時間
                mHideValTimeOut = mValCnt + (mValCnt / 7) * 4;//默認
                if ((bool)radioButtonSelfDefMemoryTime.IsChecked)
                {
                    mHideValTimeOut = (int)comboBoxTrainMemoryTimeVal.SelectedValue;
                }
                
                //mHideValTimeOut秒後自動隱藏數值
                mHideValTimer.Interval = TimeSpan.FromMilliseconds(mHideValTimeOut * 1000);
                mHideValTimer.Start();

                mErrIndex = mIndexList[0];

            }
            else //結束訓練
            {
                closeAllTimer();
                buttonStartTrain.Content = "開始訓練";
                buttonFinishMemory.IsEnabled = false;
                buttonStartChallenge.IsEnabled = true;

                mErrIndex = mIndexList[mNowIndex];
                mBtnManager.get(mIndexList[mNowIndex]).BorderBrush = new SolidColorBrush(Colors.Red);
                
                for (int i = 0; i < mValCnt; i++)//恢復原值
                {
                    mBtnManager.get(mIndexList[i]).Background = mBtnManager.mDefaultBackground;
                    mBtnManager.setBtnVal(mIndexList[i], i + 1);
                }
            }

            
        }

        /// <summary>
        /// 關閉相關的所有定時器
        /// </summary>
        private void closeAllTimer()
        {
            if (mRemainTimeTimer.IsEnabled)
            {
                mRemainTimeTimer.Stop();
            }
            if (mHideValTimer.IsEnabled)
            {
                mHideValTimer.Stop();
            }
        }

        /// <summary>
        /// 窗體關閉的時候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //正在挑戰或正在訓練的彈出確認框
            bool isChallenging = buttonStartChallenge.Content.Equals("結束挑戰");
            if (isChallenging || buttonStartTrain.Content.Equals("結束訓練"))
            {
                string msg = "正在挑戰中,此操作會影響段位,";
                if (!isChallenging)
                {
                    msg = "正在訓練中,";
                }

                var result = MessageBox.Show(msg+"確認退出?", "退出", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    //關閉定時器
                    closeAllTimer();

                    //正在挑戰退出,扣1分
                    if (buttonStartChallenge.Content.Equals("結束挑戰"))//挑戰有超時結束,需要實時判斷, 不然可能被扣2分。
                    {
                        mConfigure.curLevel--;
                        if (mConfigure.curLevel < 0)
                        {
                            mConfigure.curLevel = 0;
                        }
                    }

                    Environment.Exit(0);
                }
                else if (result == MessageBoxResult.No)
                {
                    e.Cancel = true;
                }
            }

            //保存配置以及數據
            if (mIsSaveConfigure != 0)
            {
                saveConfigure();
            }
            
        }

        /// <summary>
        /// 初始化剩餘時間
        /// </summary>
        private void initRemainTime(int remainTime)
        {
            mSpanTime = 0;
            
            if (remainTime <= 0)
            {
                remainTime = 2 * mValCnt;
            }
            mRemainTime = remainTime;

            if (1 == mGameMode)//挑戰模式
            {
                updateRemainTime(mRemainTime, mSpanTime);
            }
        }

        /// <summary>
        /// 開始挑戰
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonStartChallenge_Click(object sender, RoutedEventArgs e)
        {
            mGameMode = 1;

            string text = buttonStartChallenge.Content.ToString();
            closeAllTimer();

            mProcess = 0;
            if (text.Equals("開始挑戰"))
            {                
                buttonStartChallenge.Content = "結束挑戰";
                buttonStartTrain.IsEnabled = false;
                comboBoxChallengeLevel.IsEnabled = false;
                buttonFinishMemoryChallenge.IsEnabled = true;

                //清除紅綠邊框
                clearButtonRegAndGreenBorder();

                mNowIndex = 0;

                //獲取當前段位
                int curLevel = 0;
                getLevelString(mConfigure.curLevel, out curLevel);

                //挑戰難度
                int challengeLevel = comboBoxChallengeLevel.SelectedIndex;
                Random random = new Random();
                if (0 == challengeLevel)//青銅 3-7
                {
                    
                    if(curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min+2);
                    }
                    else
                    {
                        mValCnt = random.Next(3, 7);
                    }

                }
                else if (1 == challengeLevel)//白銀 8-15
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min + 2);
                    }
                    else
                    {
                        mValCnt = random.Next(8, 15);
                    }
                }
                else if (2 == challengeLevel)//黃金 16-30
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min + 2);
                    }                    
                    else
                    {
                        mValCnt = random.Next(16, 30);
                    }
                }
                else if (3 == challengeLevel)//鉑金 30-60
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min + 2);
                    }
                    else
                    {
                        mValCnt = random.Next(30, 60);
                    }
                }
                else if (4 == challengeLevel)//鑽石 61-80
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min + 2);
                    }
                    else
                    {
                        mValCnt = random.Next(61, 80);
                    }
                }
                else if (5 == challengeLevel)//大師 81-90
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }
                        mValCnt = random.Next(min, min + 2);
                    }
                    else
                    {
                        mValCnt = random.Next(81, 90);
                    }
                }
                else//王者 91-100
                {
                    if (curLevel == challengeLevel)//同級
                    {
                        int min = mConfigure.curLevel - 1;
                        if (min < 3)
                        {
                            min = 3;
                        }
                        else if (min > 98)
                        {
                            min = 98;
                        }

                        mValCnt = random.Next(min, min + 2);
                    }
                    else
                    {
                        mValCnt = random.Next(91, 100);
                    }
                }

                //獲取唯一索引集合
                List<int> indexList = Common.genUniqueRandomVal(1, mTopVal, mValCnt);
                if (indexList == null)
                {
                    return;
                }

                if (null != mIndexList)
                {
                    mIndexList.Clear();
                }

                mBtnManager.clearAllBtnVal();
                mIndexList = indexList;

                //顯示值
                for (int index = 0, i = 1; index < mIndexList.Count; index++)
                {
                    mBtnManager.setBtnVal(mIndexList[index], i);
                    i++;
                }

                mErrIndex = mIndexList[0];

                //開始記憶倒計時
                labelRemainingTime.Content = "剩餘記憶時間:";
                mHideValTimeOut = mValCnt + (mValCnt / 7) * 4;//默認               
                initRemainTime(mHideValTimeOut);

                //mHideValTimeOut秒後自動隱藏數值
                mHideValTimer.Interval = TimeSpan.FromMilliseconds(mHideValTimeOut * 1000);
                mHideValTimer.Start();
                mRemainTimeTimer.Start();
                
            }
            else //結束挑戰
            {
                closeAllTimer();
                buttonStartChallenge.Content = "開始挑戰";
                buttonStartTrain.IsEnabled = true;
                comboBoxChallengeLevel.IsEnabled = true;
                buttonFinishMemoryChallenge.IsEnabled = false;

                mErrIndex = mIndexList[mNowIndex];
                mBtnManager.get(mIndexList[mNowIndex]).BorderBrush = new SolidColorBrush(Colors.Red);
                for (int i = 0; i < mValCnt; i++)//恢復原值
                {
                    mBtnManager.get(mIndexList[i]).Background = mBtnManager.mDefaultBackground;
                    mBtnManager.setBtnVal(mIndexList[i], i + 1);
                }

                //主動結束挑戰,扣1分
                challengeFailed();
            }

            
        }

        /// <summary>
        /// 完成記憶--開始訓練
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonFinishMemory_Click(object sender, RoutedEventArgs e)
        {
            hideBtnVal();
            //關閉定時器
            mHideValTimer.Stop();
            //隱藏Button
            buttonFinishMemory.IsEnabled = false;
            //計時
            mRemainTimeTimer.Start();

            //遊戲進行中
            mProcess = 1;
        }

        /// <summary>
        /// 完成記憶--開始挑戰
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonFinishMemoryChallege_Click(object sender, RoutedEventArgs e)
        {
            //關閉定時器
            closeAllTimer();

            hideBtnVal();            

            //隱藏Button
            buttonFinishMemoryChallenge.IsEnabled = false;
                        
            mProcess = 1;

            //開啓挑戰倒計時
            labelRemainingTime.Content = "剩餘挑戰時間:";
            initRemainTime(2*mValCnt);
            mRemainTimeTimer.Start();
            
        }

        //更多按鈕的旋轉角度
        static int gAngle = 0;
        /// <summary>
        /// 更多訓練
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMoreTrain_Click(object sender, RoutedEventArgs e)
        {
            RotateTransform rotateTranform = new RotateTransform();
            
            if (0 == gAngle)
            {
                gAngle = 180;
                rotateTranform.Angle = 180;
                buttonMoreTrain.RenderTransform = rotateTranform;
                buttonMoreTrain.ToolTip = "隱藏訓練2區";
                Height = mShowTrain2Height;
            }
            else
            {
                gAngle = 0;
                rotateTranform.Angle = 0;
                buttonMoreTrain.RenderTransform = rotateTranform;
                buttonMoreTrain.ToolTip = "展開訓練2區";
                Height = mHideTrain2Height;
            }
            
            
        }

        /// <summary>
        /// 隱藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonHideMemoryString_Click(object sender, RoutedEventArgs e)
        {
            string text = (string)buttonHideMemoryString.Content;
            if (text.Equals("隱藏"))
            {
                textBlockMemoryString.Background = new SolidColorBrush(Colors.Black);
                buttonHideMemoryString.Content = "顯示";
                buttonHideMemoryString .ToolTip = "顯示要記憶的字符串";
            }
            else
            {
                textBlockMemoryString.Background = new SolidColorBrush(Color.FromRgb(0x9D, 0xE0,0x5D));
                buttonHideMemoryString.Content = "隱藏";
                buttonHideMemoryString.ToolTip = "隱藏要記憶的字符串";
            }
        }

        /// <summary>
        /// 生成要記憶的字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCreateMemoeyString_Click(object sender, RoutedEventArgs e)
        {
            int minVal = 0;
            int topVal = 0;//10個數字+26小寫字母+26大寫字母
            int index;
            int charType = 0;
            if ((bool)checkBoxNumber.IsChecked)
            {
                charType = 1;
                topVal = 9;
            }

            if ((bool)checkBoxLowercaseAlphabet.IsChecked)
            {
                if (1 != charType)//沒有選數字
                {
                    minVal = 10;
                }
                charType += 2;
                topVal = 35;
                
            }

            if ((bool)checkBoxUppercaseAlphabet.IsChecked)
            {
                if (1 == charType)//選了數字和大寫字母
                {
                    minVal = 35;
                    topVal = 71;
                }
                else if (2 == charType)//選了小寫字母和大寫字母
                {
                    minVal = 10;
                    topVal = 61;
                }                
                else if (3 == charType)//三種都選了
                {
                    minVal = 0;
                    topVal = 61;
                }
                else //只選了大寫字母
                {
                    minVal = 35;
                    topVal = 61;
                }
                charType += 4;
            }

            if (0 == charType)
            {
                MessageBox.Show("請至少選擇一種字符類型", "提示");
                return;
            }
                        
            int charCnt = (int)comboBoxMemoryCharCountVal.SelectedValue;
            string memoryString = "";

            Random random = new Random();
            for (; charCnt > 0; charCnt--)
            {
                index = random.Next(minVal, topVal);
                memoryString += mCharList[index];
            }
           
            textBlockMemoryString.Text = memoryString;

            //清空答題區
            textBoxMemoryString.Text = "";
        }

        /// <summary>
        /// 驗證
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCheck_Click(object sender, RoutedEventArgs e)
        {
            string str1 = textBlockMemoryString.Text;
            string str2 = textBoxMemoryString.Text;
            int len1 = str1.Length;
            int len2 = str2.Length;

            if (str2.Trim().Equals(""))
            {
                return;
            }

            if (str1.Equals(str2))
            {
                //顯示記憶字符串
                textBlockMemoryString.Background = new SolidColorBrush(Color.FromRgb(0x9D, 0xE0, 0x5D));
                buttonHideMemoryString.Content = "隱藏";
                buttonHideMemoryString.ToolTip = "隱藏要記憶的字符串";
                MessageBox.Show("恭喜您! 輸入正確!", "驗證正確");
            }
            else
            {
                //找到第一個不同的
                int i = 0;
                for (; i < str1.Length && i < str2.Length; i++)
                {
                    if (str1[i] != str2[i])
                    {
                        break;
                    }
                }

                if (buttonHideMemoryString.Content.Equals("隱藏"))
                {
                    if (i != str1.Length)
                    {
                        //將第一個不一致的字符標紅
                        textBlockMemoryString.Text = "";
                        textBlockMemoryString.Inlines.Add(new Run(str1.Substring(0, i)));
                        textBlockMemoryString.Inlines.Add(new Run(str1[i] + "") { Foreground = Brushes.Red });
                        textBlockMemoryString.Inlines.Add(new Run(str1.Substring(i + 1, str1.Length - 1 - i)));

                        MessageBox.Show("抱歉! 輸入有誤! 已經答對" + i + "/" + str1.Length + "。", "驗證錯誤");
                    }
                    else //輸入對了,但是後面有多餘字符
                    {
                        MessageBox.Show("抱歉! 輸入有誤! 已經答對" + i + "/" + str1.Length + "。但輸入有多餘", "驗證錯誤");
                    }
                }
                else//沒有對幾個的提示
                {
                    MessageBox.Show("抱歉! 輸入有誤","驗證錯誤");
                }
            }
        }
        /*
            個等級分三小等級
            等級 分值
            新手 0
            青銅 1-7
            白銀 8-15
            黃金 16-30
            鉑金 31-60
            鑽石 61-80
            大師 81-90
            王者 91-100
        */
        /// <summary>
        /// 根據分值得到段位
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        private string getLevelString(int score, out int level)
        {
            int rank = 0;//小等級
            string levelStr = "";
            if (score <= 0)
            {
                levelStr = "新手";
                level = 0;
            }
            else if (score <= 7)//青銅 1-7  1-2==>3  3-4==>2  5-7==>1
            {
                //rank = 4 - (score + 1) / 2; if(rank ==0) rank=1;

                if (score <= 2)
                {
                    rank = 3;
                }
                else if (score <= 5)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }


                levelStr = "青銅-" + rank;
                level = 0;
            }
            else if (score <= 15)//白銀 8-15
            {
                if (score <= 9)
                {
                    rank = 3;
                }
                else if (score <= 12)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }

                levelStr = "白銀-" + rank;
                level = 1;
            }
            else if (score <= 30)//黃金 16-30
            {
                if (score <= 20)
                {
                    rank = 3;
                }
                else if (score <= 25)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }
                levelStr = "黃金-" + rank;
                level = 2;
            }
            else if (score <= 60)//鑽石 31-60
            {
                if (score <= 40)
                {
                    rank = 3;
                }
                else if (score <= 50)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }

                levelStr = "鑽石-" + rank;
                level = 3;
            }
            else if (score <= 80)//鉑金 61-80
            {
                if (score <= 66)
                {
                    rank = 3;
                }
                else if (score <= 73)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }

                levelStr = "鉑金-" + rank;
                level = 4;
            }
            else if (score <= 90)//大師 81-90
            {
                if (score <= 83)
                {
                    rank = 3;
                }
                else if (score <= 86)
                {
                    rank = 2;
                }
                else
                {
                    rank = 1;
                }

                levelStr = "大師-" + rank;
                level = 5;
            }
            else//王者 91-100
            {
                levelStr = "王者-";
                if (score <= 93)
                {
                    rank = 3;
                }
                else if (score <= 96)
                {
                    rank = 2;
                }
                else if (score <= 100)
                {
                    rank = 1;
                }
                else
                {
                    levelStr = "至尊-";
                    rank = score - 100;
                }

                levelStr += rank;
                level = 6;
            }

            return levelStr;
        }

        /// <summary>
        /// 詳細 歷史挑戰
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonChallengeDetail_Click(object sender, RoutedEventArgs e)
        {
            int mLevel = 0;
            int topLevelTime = 20191227;
            int totalSuccess = mConfigure.getTotalSuccess(); 
            int totalFailed = mConfigure.getTotalFailed();
            int level0SucessRate = getSuccessRate(0);
            int level1SucessRate = getSuccessRate(1);
            int level2SucessRate = getSuccessRate(2);
            int level3SucessRate = getSuccessRate(3);
            int level4SucessRate = getSuccessRate(4);
            int level5SucessRate = getSuccessRate(5);
            int level6SucessRate = getSuccessRate(6);

            if (0 != mConfigure.topLevelTime)
            {
                topLevelTime = mConfigure.topLevelTime;
            }

            //保存配置
            saveConfigure();

            string text = "當前段位: " + getLevelString(mConfigure.curLevel, out mLevel) + "    當前積分: " + mConfigure.curLevel
                        + "\n最高段位: " + getLevelString(mConfigure.topLevel, out mLevel) + "    晉級時間: " + topLevelTime
                        + "\n挑戰總次數: " + (totalSuccess + totalFailed) + "  成功: " + totalSuccess + "  失敗: " + totalFailed + "  勝率: " + labelChallengeSuccessVal.Content
                        + "\n青銅級  成功: "+ mConfigure.challengeLevel0Success + "  失敗: "+ mConfigure.challengeLevel0Failed+ "  勝率: "+ level0SucessRate +"%  最短用時: "+ mConfigure.challengeLevel0MinTime+ "秒"
                        + "\n白銀級  成功: "+ mConfigure.challengeLevel1Success + "  失敗: "+ mConfigure.challengeLevel1Failed+ "  勝率: "+ level1SucessRate +"%  最短用時: "+ mConfigure.challengeLevel1MinTime+ "秒"
                        + "\n黃金級  成功: "+ mConfigure.challengeLevel2Success + "  失敗: "+ mConfigure.challengeLevel2Failed+ "  勝率: "+ level2SucessRate +"%  最短用時: "+ mConfigure.challengeLevel2MinTime+ "秒"
                        + "\n鉑金級  成功: "+ mConfigure.challengeLevel3Success + "  失敗: "+ mConfigure.challengeLevel3Failed+ "  勝率: "+ level3SucessRate +"%  最短用時: "+ mConfigure.challengeLevel3MinTime+ "秒"
                        + "\n鑽石級  成功: "+ mConfigure.challengeLevel4Success + "  失敗: "+ mConfigure.challengeLevel4Failed+ "  勝率: "+ level4SucessRate +"%  最短用時: "+ mConfigure.challengeLevel4MinTime+ "秒"
                        + "\n大師級  成功: "+ mConfigure.challengeLevel5Success + "  失敗: "+ mConfigure.challengeLevel5Failed+ "  勝率: "+ level5SucessRate +"%  最短用時: "+ mConfigure.challengeLevel5MinTime+ "秒"
                        + "\n王者級  成功: "+ mConfigure.challengeLevel6Success + "  失敗: "+ mConfigure.challengeLevel6Failed+ "  勝率: "+ level6SucessRate +"%  最短用時: "+ mConfigure.challengeLevel6MinTime+ "秒";

            MessageBox.Show(text, "歷史戰績");
        }

    }                     
}
                       

 Common.cs文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace MemoryAlliance
{
    //標識爲可序列化
    [StructLayout(LayoutKind.Sequential), Serializable]
    public struct ConfigureInfo
    {
        public int trainLevel;//訓練難度 3-100        
        public int challengeLevel;//挑戰難度 0-6
        public int charType;//組合1 | 2 | 4
        public int charCnt;//記憶字符個數
        public int memoryTime;//記憶時間非負數 0是默認  其它爲實際秒數
        public int curLevel;//當前段位
        public int topLevel;//最高段位
        public int topLevelTime;//晉級時間 20191227

        public int challengeLevel0Success;//Level 0 挑戰成功數
        public int challengeLevel0Failed;//Level 0 挑戰失敗數
        public int challengeLevel0MinTime;//Level 0 最短用時

        public int challengeLevel1Success;//Level 1 挑戰成功數
        public int challengeLevel1Failed;//Level 1 挑戰失敗數
        public int challengeLevel1MinTime;//Level 1 最短用時

        public int challengeLevel2Success;//Level 2 挑戰成功數
        public int challengeLevel2Failed;//Level 2 挑戰失敗數
        public int challengeLevel2MinTime;//Level 2 最短用時

        public int challengeLevel3Success;//Level 3 挑戰成功數
        public int challengeLevel3Failed;//Level 3 挑戰失敗數
        public int challengeLevel3MinTime;//Level 3 最短用時

        public int challengeLevel4Success;//Level 4 挑戰成功數
        public int challengeLevel4Failed;//Level 4 挑戰失敗數
        public int challengeLevel4MinTime;//Level 4 最短用時

        public int challengeLevel5Success;//Level 5 挑戰成功數
        public int challengeLevel5Failed;//Level 5 挑戰失敗數
        public int challengeLevel5MinTime;//Level 5 最短用時

        public int challengeLevel6Success;//Level 6 挑戰成功數
        public int challengeLevel6Failed;//Level 6 挑戰失敗數
        public int challengeLevel6MinTime;//Level 6 最短用時

        /// <summary>
        /// 挑戰成功總數
        /// </summary>
        /// <returns></returns>
        public int getTotalSuccess()
        {
            int totalSuccess = 0;

            totalSuccess = challengeLevel0Success + challengeLevel1Success
                         + challengeLevel2Success + challengeLevel3Success
                         + challengeLevel4Success + challengeLevel5Success
                         + challengeLevel6Success;

            return totalSuccess;
        }

        /// <summary>
        /// 挑戰失敗總數
        /// </summary>
        /// <returns></returns>
        public int getTotalFailed()
        {
            int totalFailed = 0;

            totalFailed = challengeLevel0Failed + challengeLevel1Failed
                         + challengeLevel2Failed + challengeLevel3Failed
                         + challengeLevel4Failed + challengeLevel5Failed
                         + challengeLevel6Failed;

            return totalFailed;
        }

        /// <summary>
        /// 設置等級挑戰信息
        /// </summary>        如果值爲負數就不設置
        /// <param name="rank">等級0-6</param>
        /// <param name="success">挑戰成功數</param>
        /// <param name="faied">挑戰失敗數</param>
        /// <param name="minTime">最短用時</param>
        public void setRankInfo(int rank, int success, int faied, int minTime)
        {
            if (rank == 0)
            {
                if (success >= 0)
                {
                    challengeLevel0Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel0Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel0MinTime = minTime;
                }
            }
            else if (rank == 1)
            {
                if (success >= 0)
                {
                    challengeLevel1Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel1Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel1MinTime = minTime;
                }
            }
            else if (rank == 2)
            {
                if (success >= 0)
                {
                    challengeLevel2Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel2Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel2MinTime = minTime;
                }
            }
            else if (rank == 3)
            {
                if (success >= 0)
                {
                    challengeLevel3Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel3Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel3MinTime = minTime;
                }
            }
            else if (rank == 4)
            {
                if (success >= 0)
                {
                    challengeLevel4Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel4Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel4MinTime = minTime;
                }
            }
            else if (rank == 5)
            {
                if (success >= 0)
                {
                    challengeLevel5Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel5Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel5MinTime = minTime;
                }
            }
            else
            {
                if (success >= 0)
                {
                    challengeLevel6Success = success;
                }

                if (faied >= 0)
                {
                    challengeLevel6Failed = faied;
                }

                if (minTime >= 0)
                {
                    challengeLevel6MinTime = minTime;
                }
            }
        }

        /// <summary>
        /// 獲取等級挑戰信息
        /// </summary>
        /// <param name="rank">等級0-6</param>
        /// <param name="success">挑戰成功數</param>
        /// <param name="faied">挑戰失敗數</param>
        /// <param name="minTime">最短用時</param>
        public void getRankInfo(int rank, out int success, out int faied, out int minTime)
        {
            if (rank == 0)
            {
                success = challengeLevel0Success;
                faied = challengeLevel0Failed;
                minTime = challengeLevel0MinTime;
            }
            else if (rank == 1)
            {
                success = challengeLevel1Success;
                faied = challengeLevel1Failed;
                minTime = challengeLevel1MinTime;
            }
            else if (rank == 2)
            {
                success = challengeLevel2Success;
                faied = challengeLevel2Failed;
                minTime = challengeLevel2MinTime;
            }
            else if (rank == 3)
            {
                success = challengeLevel1Success;
                faied = challengeLevel1Failed;
                minTime = challengeLevel1MinTime;
            }
            else if (rank == 4)
            {
                success = challengeLevel4Success;
                faied = challengeLevel4Failed;
                minTime = challengeLevel4MinTime;
            }
            else if (rank == 5)
            {
                success = challengeLevel5Success;
                faied = challengeLevel5Failed;
                minTime = challengeLevel5MinTime;
            }
            else
            {
                success = challengeLevel6Success;
                faied = challengeLevel6Failed;
                minTime = challengeLevel6MinTime;
            }

        }
    }
    class Common
    {
        static List<int> mBaseList = null;
        /// <summary>
        /// 生成指定範圍的n個不重複的隨機數
        /// </summary>
        /// <param name="minVal"></param>
        /// <param name="topVal"></param>
        /// <param name="cnt"></param>
        /// <returns></returns>
        public static List<int> genUniqueRandomVal(int minVal, int topVal, int cnt)
        {
            int index;

            if (minVal >= topVal)
            {
                return null;
            }

            if (null == mBaseList)
            {
                mBaseList = new List<int>();
            }

            mBaseList.Clear();
            for (int i = minVal; i <= topVal; i++)
            {
                mBaseList.Add(i);
            }

            List<int> uniqueValList = new List<int>();
            Random random = new Random();
            for (; cnt > 0;)
            {
                index = random.Next(minVal, topVal);

                uniqueValList.Add(mBaseList[index - 1]);
                mBaseList.RemoveAt(index - 1);
                cnt--;
                topVal--;
            }
            return uniqueValList;
        }

        /// <summary>
        /// ConfigureInfo反序列化
        /// </summary>
        /// <param name="rawdatas"></param>
        /// <returns></returns>
        public static ConfigureInfo confInfoDeserialize(byte[] rawdatas)
        {
            Type anytype = typeof(ConfigureInfo);
            int rawsize = Marshal.SizeOf(anytype);
            if (rawsize > rawdatas.Length)
            {
                return new ConfigureInfo();
            }
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.Copy(rawdatas, 0, buffer, rawsize);
            object retobj = Marshal.PtrToStructure(buffer, anytype);
            Marshal.FreeHGlobal(buffer);
            return (ConfigureInfo)retobj;
        }

        /// <summary>
        /// ConfigureInfo序列化
        /// </summary>
        /// <param name="obj">ConfigureInfo結構體</param>
        /// <returns></returns>
        public static byte[] confInfoSerialize(object obj)
        {
            int rawsize = Marshal.SizeOf(obj);
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.StructureToPtr(obj, buffer, false);
            byte[] rawdatas = new byte[rawsize];
            Marshal.Copy(buffer, rawdatas, 0, rawsize);
            Marshal.FreeHGlobal(buffer);
            return rawdatas;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="confFileName">配置文件名</param>
        /// <param name="conf">配置</param>
        /// <returns></returns>
        public static bool saveConfigure(string confFileName, object conf)
        {
            try
            {
                FileStream fs = File.OpenWrite(confFileName);
                byte[] data = confInfoSerialize(conf);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(data);

                //計算校驗和
                int dateSize = Marshal.SizeOf(conf);
                ushort checkSum = calcCheckSum(data, dateSize);
                bw.Write(checkSum);

                bw.Flush();
                bw.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }


        /// <summary>
        /// 讀取配置
        /// </summary>
        /// <param name="confFileName">配置文件名</param>
        /// <param name="conf">配置</param>
        /// <returns></returns>
        public static int readConfigure(string confFileName, out ConfigureInfo conf)
        {
            try
            {
                FileStream fs = File.OpenRead(confFileName);
                BinaryReader br = new BinaryReader(fs);
                int size = Marshal.SizeOf(typeof(ConfigureInfo));

                byte[] data = br.ReadBytes(size);

                //讀取校驗和
                ushort checkSum1 = br.ReadUInt16();
                ushort checkSum2 = calcCheckSum(data, size);

                br.Close();
                fs.Close();

                //判斷校驗和,如果不一樣,表示數據被惡意修改
                if (checkSum1 != checkSum2)
                {
                    conf = new ConfigureInfo();
                    return -1;
                }
                else
                {
                    conf = confInfoDeserialize(data);
                }
            }
            catch
            {
                conf = new ConfigureInfo();
                return 0;
            }

            return 1;
        }

        /// <summary>
        /// 計算校驗和
        /// </summary>
        /// <param name="data">指定的數據</param>
        /// <param name="size">數據長度</param>
        /// <returns></returns>
        private static ushort calcCheckSum(byte[] data, int size)
        {
            byte checkSumA = 0;
            byte checkSumB = 0;
            ushort checkSum = 0;

            for (int i = 0; i < size; i++)
            {
                checkSumA += data[i];
                checkSumB ^= data[i];
            }
            
            checkSum = (ushort)((Convert.ToUInt16(checkSumA) << 8) + Convert.ToUInt16(checkSumB));
            return checkSum;
        }

    }
}

ButtonManager.cs文件

 

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MemoryAlliance
{
    /// <summary>
    /// Button的佈局類
    /// </summary>
    class ButtonManager
    {
        public Brush mDefaultBackground;//Button默認的背景樣式
        public Brush mDefaultBorderBrush;//Button默認的邊框樣式
        public Thickness mDefaultThickness;//Button默認的邊框粗細        
        private List<Button> mBtnList;

        public ButtonManager()
        {
            mBtnList = new List<Button>();
        }

        /// <summary>
        /// 清空Button集合
        /// </summary>
        public void clearList()
        {
            mBtnList.Clear();
        }

        /// <summary>
        /// 添加Button
        /// </summary>
        /// <param name="btn"></param>
        public void addBtn(Button btn)
        {
            if (mBtnList != null && btn != null)
            {
                mBtnList.Add(btn);
            }            
        }

        /// <summary>
        /// 獲取指定Button對象
        /// </summary>
        /// <param name="index">[1-100]</param>
        /// <returns></returns>
        public Button get(int index)
        {
            if (index >= 1 && index <= mBtnList.Count)
            {
                return mBtnList[index-1];
            }
            return null;
        }

        /// <summary>
        /// 設置指定Button數值
        /// </summary>
        /// <param name="btnIndex">[1,100]</param>
        /// <param name="val">[1,100]</param>
        /// <returns></returns>
        public void setBtnVal(int btnIndex, int val)
        {
            if (btnIndex > 0 && btnIndex <= mBtnList.Count)
            {
                mBtnList[btnIndex-1].Content = val;
            }
        }

        /// <summary>
        /// 設置指定Button數值
        /// </summary>
        /// <param name="btnIndex">[1,100]</param>
        /// <param name="val">[1,100]</param>
        /// <returns></returns>
        public void setBtnVal(int btnIndex, string val)
        {
            if (btnIndex > 0 && btnIndex <= mBtnList.Count)
            {
                mBtnList[btnIndex - 1].Content = val;
            }
        }

        /// <summary>
        /// 設置指定Button背景色
        /// </summary>
        /// <param name="btnIndex"></param>
        /// <param name="brush"></param>
        public void setBtnBackground(int btnIndex, Brush brush)
        {
            if (btnIndex > 0 && btnIndex <= mBtnList.Count)
            {
                mBtnList[btnIndex - 1].Background = brush;
            }
        }

        /// <summary>
        /// 清空所有Button數值
        /// </summary>
        public void clearAllBtnVal()
        {
            if (mBtnList != null)
            {
                foreach (var item in mBtnList)
                {
                    item.Content = "";
                    item.Background = mDefaultBackground;
                }
            }
        }

        /// <summary>
        /// 將Butto設置爲默認樣式
        /// </summary>
        /// <param name="btnIndex"></param>
        /// <returns></returns>
        public bool setDefaultStyle(int btnIndex)
        {
            if (null == mBtnList)
            {
                return false;
            }

            if (btnIndex > 0 && btnIndex <= mBtnList.Count)
            {
                mBtnList[btnIndex - 1].Background = mDefaultBackground;
                mBtnList[btnIndex - 1].BorderBrush = mDefaultBorderBrush;
                mBtnList[btnIndex - 1].BorderThickness = mDefaultThickness;
            }

            return false;
        }
    }
}

五、總結

     5.1 修改系統菜單,需要調用系統API。

           聲明/引用 系統接口           

[DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);//獲取系統菜單
        [DllImport("user32.dll")]
        private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);//添加菜單項

        //一些系統定義的常量值 (詳情請參考Windows API)
        public const int WM_SYSCOMMAND = 0x0112;
        public const int MF_SEPARATOR = 0x800;
        public const int MF_BYPOSITION = 0x400;

        //自定義消息一般在0x0400之後
        public const int MENU_ABOUT = 0x0470;

           在窗體加載完成函數進行添加菜單項,在添加新的菜單項是需要設置一個"消息id",後面根據這個"消息id"來識別這個菜單項。

//添加 關於"記憶聯盟"
            WindowInteropHelper helper = new WindowInteropHelper((Window)sender);
            IntPtr sysMenuHandle = GetSystemMenu(helper.Handle, false);
            InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);//添加分割線
            InsertMenu(sysMenuHandle, 6, MF_BYPOSITION, MENU_ABOUT, "關於\"記憶聯盟\"");//添加一項菜單項

          新添加的菜單項的動作響應    (攔截各種窗口消息,是我們需要的消息時才進行響應)      

 protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        //處理各種窗口消息(詳情請參考Windows API)
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //分流處理各種消息
            switch (msg)
            {
                case WM_SYSCOMMAND://系統命令
                    int id = wParam.ToInt32() & 0xFFFF;
                    switch(id)
                    {
                        case MENU_ABOUT: //關於
                            handled = true;//表示已經處理,不用系統處理了
                            string text = "【簡介】 這是一個用來訓練記憶力的小遊戲。"
                                    + "\n\n【遊戲玩法】"
                                    + "\n  挑戰區:在挑戰時間內按順序將‘數字’點開。完成挑戰,段位提升;否則降低段位。"
                                    + "\n  訓練區:按照順序將‘數字’點開。沒有時間限制,不影響段位。"
                                    + "\n  訓練2區:隨機生成一串字符,記憶後隱藏它,然後將它默寫出來。"
                                    + "\n\n【段位等級說明】  (以下由低到高進行排序)"
                                    + "\n  新手、青銅、白銀、黃金、鉑金、鑽石、大師、王者"
                                    + "\n  除新手外每級細分爲3小級,如青銅級細分爲:青銅-3,青銅-2,青銅-1;其它等級類似。3小級中1級比2級要高。";
                            MessageBox.Show(text, "關於\"記憶聯盟\"");
                            break;
                    }
                    break;

            }
            handled = false;
            return IntPtr.Zero;
        }

             效果:

             

     更多接口:

[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool RemoveMenu(IntPtr hMenu, uint nPosition, uint nFlags);//移除
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool SetMenuItemBitmaps(IntPtr hMenu, Int32 nPosition, Int32 nFlags, Bitmap pBmpUnchecked, Bitmap pBmpchecked);//設置圖標
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern bool AppendMenu(IntPtr hMenu,Int32 nFlags, Int32 IDNewItem, string lpNewItem);//追加菜單項

      5.2 設置textBlock中某一部分字符的顏色           

myTextBlock.Inlines.Add(new Run("abc"));//添加"abc"字符串,默認黑色
myTextBlock.Inlines.Add(new Run("d") { Foreground = Brushes.Red });//添加紅色的d
myTextBlock.Inlines.Add(new Run("e") { Foreground = Brushes.Green });//添加綠色的e
myTextBlock.Inlines.Add(new Run("f") { Foreground = Brushes.Blue });//添加藍色的f
myTextBlock.Inlines.Add(new Run("ghi"));////添加"ghi"字符串,默認黑色

           字符效果:

           

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章