回看当年写的数独游戏重温WPF

毕业以来一直从事Android开发,很多在学校期间自学的东西都快忘了。今天拉肚子没上班,借此机会整理一下当时做的数独游戏用到的WPF相关知识点。会过,就不能让它忘了!

重温WPF

下载完整源代码

窗口布局文件xaml

  1. 根节点为<Window>表示当前窗口
    • WindowStyle设置为None表示不要标题栏
    • AllowsTransparency设置为True表示允许透明
    • WindowStartupLocation设置为CenterScreen表示窗口打开时在屏幕中间
    • WindowState设置为Maximized表示最大化打开
    • ResizeMode设置为NoResize表示不允许改变尺寸
  2. <Grid>网格布局

    <Grid Name="gd_main_menu">
    <Grid.RowDefinitions>
    <RowDefinition Height="2*" />
    <RowDefinition Height="6*" />
    <RowDefinition Height="3*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="3*" />
    <ColumnDefinition Width="8*" />
    <ColumnDefinition Width="4*" />
    </Grid.ColumnDefinitions>
    <Image Grid.Row="0" Grid.Column="0"/>
    <Image Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="5"/>
    </Grid>

    • <Gird.RowDefinitions>表示定义该<Gird>的行信息,其中包含几个<RowDefinition>表示有几行;Height属性表示该行高度,2*表示占比例为2(即(2/(2+6+3)))。
    • <Grid.ColumnDefinitions>与上述类似,可类比理解。
    • 位于<Gird>中的控件可通过Grid.RowGrid.Column两个属性来设置控件所在的单元格,通过Grid.RowSpanGrid.ColumnSpan来设置控件占用的单元格数
  3. <Image>图片控件
    • Name为该控件起个名字(其他控件通用)
    • MouseDown设置鼠标单击时触发的方法(其他控件通用)
    • Source设置该图片控件显示的图片路径(调试相对路径的当前路径为工程根目录,运行时为exe所在目录)
    • MouseEnter设置鼠标悬浮在该控件上时触发的方法(其他控件通用)
    • MouseLeave设置鼠标从该控件上移开时触发的方法(其他控件通用)
    • VerticalAlignment垂直对齐可设置为Top顶部、Bottom底部、Center中间、Stretch填充
    • HorizontalAlignment水平对齐设置,可与垂直类比
    • Visibility设置是否可见
  4. 控件设置发光效果
    • 在创建该工程的时候会成成一个全局配置文件App.xaml,首先在该文件中创建一个样式<Style>

      <Style x:Key="OuterStyle" TargetType="{x:Type Image}">
      <Setter Property="Effect">
      <Setter.Value>
      <DropShadowEffect ShadowDepth="0" Color="#FFFFFF" Opacity="1" BlurRadius="50"/>
      </Setter.Value>
      </Setter>
      </Style>
    • 然后在控件中添加属性Style="{StaticResource OuterStyle}"即可实现该效果
    • 如果是平时不发光,鼠标悬浮时发光,则只需在<Setter>外添加标签即可,此处作为新的样式给出代码

      <Style x:Key="OuterGlowStyle" TargetType="{x:Type Image}">
      <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="Effect">
      <Setter.Value>
      <DropShadowEffect ShadowDepth="0" Color="#FFFFFF" Opacity="1" BlurRadius="50"/>
      </Setter.Value>
      </Setter>
      </Trigger>
      </Style.Triggers>
      </Style>

      1. x:Key相当于控件中的Name,用于唯一标识该样式
      2. TargetType说明该样式用于什么控件
      3. <Trigger>标识一个触发器,<Trigger Property="IsMouseOver" Value="True">标识当IsMouseOver属性为True时触发该触发器
      4. <DropShadowEffect>为影子效果,即发光

窗口逻辑文件,与xaml文件同名且后缀多了个.cs

  1. 设置窗口背景图片

    ImageBrush imagebrush = new ImageBrush();//创建一个画刷
    imagebrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/s.jpg"));//指定图片路径
    imagebrush.Stretch = Stretch.Fill;//设置填充效果
    this.Background = imagebrush;//讲画刷设置到背景
  2. 设置窗口中某个空间为淡出效果

    /// <summary>
    /// 淡出DependencyObject控件
    /// </summary>
    /// <param name="window">控件所在窗口</param>
    /// <param name="control">控件(控件的Name属性值)</param>
    internal static void showSlowly(Window window, FrameworkElement control)
    {
    Storyboard sb = new Storyboard();//创建故事板
    window.Resources.Add(Guid.NewGuid().ToString(), sb);//将故事板添加到window中
    DoubleAnimation da = new DoubleAnimation();//创建动画
    Storyboard.SetTarget(da, control);//将动画添加到控件
    Storyboard.SetTargetProperty(da, new PropertyPath("Opacity", new object[] { }));//设置动画属性
    da.From = 0;
    da.To = 1;
    sb.Duration = new Duration(TimeSpan.FromSeconds(1));
    sb.Children.Add(da);//将动画添加到故事板
    sb.Begin();//开始故事板
    }
  3. 打开新的窗口

    new WindowName().Show();//打开新窗口
    this.Close();//关闭当前窗口
  4. 为Image设置鼠标指向时切换显示图片
    首先为该<Image>设置属性MouseEnter="method",然后在cs文件中写逻辑

    private void ibtn_sudo_enter(object sender, MouseEventArgs e)
    {
    Image img = (Image)sender;//强制转型
    img.Source = new BitmapImage(new Uri("Resources/btn_enter.png", UriKind.Relative));//设置相对路径
    }
  5. 动态向<Grid>添加控件

    Label lbl = new Label();//定义一个Label用于设置颜色
    SolidColorBrush scb = new SolidColorBrush();//新建画刷
    scb.Color = Color.FromRgb(59,184,209);//设置颜色
    lbl.Background = scb;//将画刷添加到Label
    //将Label添加到grid相应单元格(i,j)
    Grid.SetRow(lbl, i);
    Grid.SetColumn(lbl, j);
    gd.Children.Add(lbl);//gd为Grid的Name
  6. 动态向控件添加点击等事件(参数均为方法名)

    img.MouseDown += new MouseButtonEventHandler(str_Click);//添加按钮监听
    img.MouseEnter += new MouseEventHandler(str_Enter);//添加enter监听
    img.MouseLeave += new MouseEventHandler(str_Leave);//添加leave监听
  7. 动态获取窗口上的控件

    Image img = gd.FindName("name") as Image;//获取对应位置的图片,gd为控件所在控件的Name,name为控件的Name
  8. 动态为控件设置模糊效果

    /// <summary>
    /// 设置模糊效果
    /// </summary>
    /// <param name="control">控件Name</param>
    public static void setBlurEffect(FrameworkElement control)
    {
    //创建模糊BlurEffect对象
    BlurEffect newBlurEffect = new BlurEffect();
    //设定模糊效果值Radius
    //为control添加Blur效果
    newBlurEffect.Radius = 10;
    control.Effect = newBlurEffect;
    }
  9. 动态设置控件可视性

    gd_sudo_choices.Visibility = Visibility.Visible;
  10. 动态设置控件阴影/发光效果

    FrameworkElement control = sender as FrameworkElement;//获取控件
    
    //创建并设置Effect
    DropShadowEffect dse = new DropShadowEffect();
    dse.ShadowDepth = 0;
    dse.Color = Color.FromRgb(0, 0, 0);
    dse.Opacity = 1;
    dse.BlurRadius = 50;
    
    control.Effect = dse;//设置控件Effect
    control.Effect = null;//取消控件Effect
  11. 定时器

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMilliseconds(1000);//每多少毫秒触发一次
    timer.Tick += new EventHandler(changeTime);//设置触发方法
    timer.Start();//开始定时器
  12. 获取指定控件的父控件

    Grid gd_sudo_choice = VisualTreeHelper.GetParent(control) as Grid;
发布了69 篇原创文章 · 获赞 55 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xmh19936688/article/details/51648460