关闭

c#程序设计 猜猜看

109人阅读 评论(0) 收藏 举报
<Window
        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" mc:Ignorable="d" x:Class="猜猜看.MainWindow"
        Title="MainWindow" Height="440.972" Width="732.462">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition MaxHeight="60"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Text="猜猜看" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="BlueViolet" FontSize="20"/>
            <Grid Grid.Row="1" >
                <Grid.RowDefinitions>
                    <RowDefinition  MaxHeight="60"/>
                    <RowDefinition Height="240" />
                    <RowDefinition MaxHeight="60"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Button x:Name="PicGuessName" Content="猜名字" Margin="100,15" Click="PicGuessName_Click"/>
                    <Button Grid.Column="1" x:Name="NameGuessPIC" Content="猜图片" Margin="100,15" Click="NameGuessPIC_Click"/>
                </Grid>
                <Grid x:Name="Grid_NameGuessPIC" Visibility="Hidden" Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition MinHeight="160"/>
                        <RowDefinition MaxHeight="60"/>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Image x:Name="Img1" Margin="20,15,10,15" MouseDown="Img2_MouseDown"/>
                        <Image x:Name="Img2" Grid.Column="1" Margin="15,15" MouseDown="Img2_MouseDown"/>
                        <Image x:Name="Img3" Grid.Column="2" Margin="15,15" MouseDown="Img2_MouseDown"/>
                    </Grid>
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="TBlock_Name" Grid.Column="1" Width="150" Margin="2" HorizontalAlignment="Left"/>
                        <TextBlock Grid.Column="0" Width="50" Margin="2" HorizontalAlignment="Right" Text="姓名:" FontSize="15" TextAlignment="Right"/>
                    </Grid>

                </Grid>
                <Grid Grid.Row="1" x:Name="Grid_PicGuessName" Visibility="Visible">
                    <Grid.RowDefinitions>
                        <RowDefinition MinHeight="160"/>
                        <RowDefinition MaxHeight="60"/>
                    </Grid.RowDefinitions>
                    <Image Name="GuessImg" Margin="260,10"/>
                    <Grid Margin="0,175,0,5" Grid.RowSpan="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="TBlock_Guess1" Margin="5" Width="120" MouseDown="TBlock_Guess1_MouseDown"/>
                        <TextBlock x:Name="TBlock_Guess2" Grid.Column="1" Margin="5" Width="120" MouseDown="TBlock_Guess1_MouseDown"/>
                        <TextBlock x:Name="TBlock_Guess3"  Grid.Column="2" Margin="57,5,64,5" Width="120" MouseDown="TBlock_Guess1_MouseDown"/>
                    </Grid>
                </Grid>
            </Grid>
        </Grid>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 猜猜看
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
           
        }

        
        #region 按钮事件
        private void PicGuessName_Click(object sender, RoutedEventArgs e)
        {
            Grid_NameGuessPIC.Visibility = System.Windows.Visibility.Hidden;
            Grid_PicGuessName.Visibility = Visibility;
            bing_Name();
        }

        private void NameGuessPIC_Click(object sender, RoutedEventArgs e)
        {
            Grid_NameGuessPIC.Visibility = Visibility;
            Grid_PicGuessName.Visibility = System.Windows.Visibility.Hidden;
            bing_Pic();
        }
        #endregion


        // 加载图片信息
        public string[] LoadPic() {
             string Path =@"D:\old\Project\Old\项目\猜猜看\猜猜看\Image\";
             string[] imageName = Directory.GetFiles(Path);
             return imageName;
        }

        // 猜图片_绑定数据
        public void bing_Pic() {
              

            //  随机抽取照片
            Random random = new Random();
            int[] num={0,0,0};
            for (int i = 0; i < 3;) {
                int k = random.Next(8);
                if (k != 0 && !num.Contains(k-1))
                {
                    num[i] = k-1;
                    i++;
                }
            }


            //  获取照片路径
            string[] imageNam = LoadPic();


            // 绑定照片控件
            string name = imageNam[num[0]];
            Uri url = new Uri(name, UriKind.Absolute);
            BitmapImage bitmap = new BitmapImage(url);
            Img1.Source = bitmap;
            //     Img1.Source = new BitmapImage(new Uri(imageNam[num[0]], UriKind.Absolute));

            Img2.Source = new BitmapImage(new Uri(imageNam[num[1]]));
            Img3.Source = new BitmapImage(new Uri(imageNam[num[2]]));

            // 绑定文本控件

           int n = random.Next(3);
           Match match = Regex.Match(imageNam[num[n]],@"[0-9]{12}(\w{2,})");
           TBlock_Name.Text = match.Groups[1].Value;
        }


        // 猜名字_数据绑定
        public void bing_Name() {
            //  随机抽取照片
            Random random = new Random();
            int[] num = { 0, 0, 0 };
            for (int i = 0; i < 3;)
            {
                int k = random.Next(8);
                if (k!=0&&!num.Contains(k-1)) {
                    num[i] = k-1;
                    i++; 
                }
                
            }


            //  获取照片路径
            string[] imageNam = LoadPic();

           
            //  绑定文本控件
            string[] name = {"","",""};
            for (int i = 0; i < 3; i++) {

               
            }

            TBlock_Guess1.Text = name[0];
            TBlock_Guess2.Text = name[1];
            TBlock_Guess3.Text = name[2];


            // 绑定图片控件
             int m = random.Next(3);
             GuessImg.Source = new BitmapImage(new Uri(imageNam[num[m]]));
             
             
        }

        private void Img2_MouseDown(object sender, MouseButtonEventArgs e)
        {

            Image img = (Image)sender;
            Match match = Regex.Match(img.Source.ToString(), @".+([0-9]{12})([\w]{2,})\.jpg");
            if (TBlock_Name.Text == match.Groups[2].Value)
            {
                MessageBox.Show("猜对了");
            }
            else {
                MessageBox.Show("猜错了");
            }
            
        }

        private void TBlock_Guess1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock TB = (TextBlock)sender;
            
            Match match = Regex.Match( GuessImg.Source.ToString(), @".+([0-9]{12})([\w]{2,})\.jpg");

            if (TB.Text == match.Groups[2].Value) { MessageBox.Show("你猜对了"); } else { MessageBox.Show("猜错了"); }
        }
     }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1379次
    • 积分:99
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论