侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

WPF实现简单的跑马灯效果

技术  /  管理员 发布于 7年前   436

最近项目上要用到跑马灯的效果,和网上不太相同的是,网上大部分都是连续的,而我们要求的是不连续的。

也就是是,界面上就展示4项(展示项数可变),如果有7项要展示的话,则不断的在4个空格里左跳,当然,衔接上效果不是很好看。

然后,需要支持点击以后进行移除掉不再显示的内容。

效果如下:

思路大致如下:

1、最外层用一个ViewBox,为了可以填充调用此控件的地方,这样可以方便自动拉伸

复制代码 代码如下:
<Viewbox x:Name="viewbox_main" Height="{Binding Path=ActualHeight}" Width="{Binding Path=ActualWidth}" MouseLeave="grid_main_MouseLeave" MouseMove="grid_main_MouseMove"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"/>

2、定义三个变量,一个是Count值,是为了设定要展示的UserControl的个数的,例如默认是4个,如效果图,当然,设置成5的话,就是5个了;一个List<Grid>是为了放入展示控件的列表,一个List<UserControl>是用来放所有要用于跑马灯里的控件的。

3、设置一个Canvas,放入到最外层的Viewbox中,用于跑马灯时候用(这也是常用的跑马灯控件Canvas)

//给Canvas设置一些属性 canvas_board.VerticalAlignment = VerticalAlignment.Stretch; canvas_board.HorizontalAlignment = HorizontalAlignment.Stretch;canvas_board.Width = this.viewbox_main.ActualWidth;canvas_board.Height = this.viewbox_main.ActualHeight;canvas_board.ClipToBounds = true;//用viewbox可以支持拉伸this.viewbox_main.Child = canvas_board;

4、将要循环的Grid放入到Canvas里,这里的Grid的个数,要比展示的个数大一个,也就是Count+1个值,因为滚动的时候,其实是在最外面有一个的,这样保证了循环的走动。至于两个控件之间的Margin这个就是要设置Grid的了,到时候控件是直接扔进Grid里的

//循环将Grid加入到要展示的列表里for (int i = 0; i < Uc_Count + 1; i++){ Grid grid = new Grid(); grid.Width = canvas_board.Width / Uc_Count - 10; grid.Height = canvas_board.Height - 10; grid.Margin = new Thickness(5); this.canvas_board.Children.Add(grid); grid.SetValue(Canvas.TopProperty, 0.0); grid.SetValue(Canvas.LeftProperty, i * (grid.Width + 10)); UcListForShow.Add(grid);}

5、给每个Grid增加一个动画效果,就是向左移动的效果

for (int i = 0; i < UcListForShow.Count; i++){ //设置滚动时候的效果 DoubleAnimationUsingKeyFrames daukf_uc = new DoubleAnimationUsingKeyFrames(); LinearDoubleKeyFrame k1_uc = new LinearDoubleKeyFrame(i * (UcListForShow[i].Width + 10), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))); LinearDoubleKeyFrame k2_uc = new LinearDoubleKeyFrame((i - 1) * (UcListForShow[i].Width + 10), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2.5))); daukf_uc.KeyFrames.Add(k1_uc); daukf_uc.KeyFrames.Add(k2_uc); storyboard_imgs.Children.Add(daukf_uc); Storyboard.SetTarget(daukf_uc, UcListForShow[i]); Storyboard.SetTargetProperty(daukf_uc, new PropertyPath("(Canvas.Left)"));}

6、滚动的时候,要计算UserControl到底是添加到了哪个Grid里面,也就是哪个控件作为了第一位。

我们设置一个索引值scroll_index,默认的时候,scroll_index=0,这是初始的状态,当滚动起来以后,scroll_index = scroll_index + 1 - Uc_Count;

然后,判断,循环的时候,是否是展示列表的末尾了,如果是的话,则要填充的控件是scroll_index %UcListSum.Count(滚动索引,对总数直接取余数),如果不是的话则是scroll_index++ % UcListSum.Count(滚动索引++,对总数直接取余数)

scroll_index = scroll_index + 1 - Uc_Count;for (int i = 0; i < UcListForShow.Count; i++){ UcListForShow[i].SetValue(Canvas.LeftProperty, i * (UcListForShow[i].Width + 10)); UserControl uc; if (i == UcListForShow.Count - 1) {  uc = UcListSum[scroll_index % UcListSum.Count]; } else {  uc = UcListSum[scroll_index++ % UcListSum.Count]; } if (uc.Parent != null) {  (uc.Parent as Grid).Children.Clear();//将Usercontrol从原来的里面移除掉,要不然会抛错,Usercontrol已属于另一个控件 } UcListForShow[i].Children.Clear(); UcListForShow[i].Children.Add(uc); //将隐藏按钮加入到Grid里 Button btn = new Button(); btn.Style = (dictionary["hidenStyle"] as Style);//从样式文件里读取到Button的样式 btn.Tag = UcListForShow[i].Children;//给Tag赋值,这样方便查找 btn.Click += Btn_Click;//注册隐藏事件 UcListForShow[i].Children.Add(btn);}

代码中,需要注意的是(uc.Parent as Grid).Children.Clear(),如果不移除的话,则会提示,已经属于另一个,所以,要从parent里面移除掉。

7、Button的隐藏事件,当Button点击以后,则要进行隐藏,其实也就是将总数里面,减除掉不再显示的那一项

private void Btn_Click(object sender, RoutedEventArgs e){ if ((sender as Button).Tag != null) {  UcListSum.Remove((((sender as Button).Tag as UIElementCollection)[0] as UserControl)); } if (UcListSum.Count == Uc_Count)//当列表数和要展示的数目相同的时候,就停止掉动画效果 {  storyboard_imgs.Completed -= Storyboard_imgs_Completed;  storyboard_imgs.Stop();  for (int i = 0; i < Uc_Count; i++)  {   UcListForShow[i].Children.Clear();   if (UcListSum[i].Parent != null)   {    (UcListSum[i].Parent as Grid).Children.Clear();   }   UcListForShow[i].Children.Add(UcListSum[i]);  }  return; }}

所有代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Animation;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace MarqueeUserControl{ /// <summary> /// MarqueeUC.xaml 的交互逻辑 /// </summary> public partial class MarqueeUC : UserControl {  ResourceDictionary dictionary;  public MarqueeUC()  {   InitializeComponent();   //读取样式文件   dictionary = new ResourceDictionary { Source = new Uri("/MarqueeUserControl;component/MarqueeUserControlDictionary.xaml", UriKind.Relative) };  }  #region 属性  private int _uc_Count = 0;  /// <summary>  /// 用来展示几个  /// </summary>  public int Uc_Count  {   get   {    return _uc_Count;   }   set   {    _uc_Count = value;   }  }  private List<Grid> _ucListForShow = new List<Grid>();  /// <summary>  /// 用来展示的控件列表  /// </summary>  private List<Grid> UcListForShow  {   get   {    return _ucListForShow;   }   set   {    _ucListForShow = value;   }  }  private List<UserControl> _ucListSum = new List<UserControl>();  /// <summary>  /// 要添加的控件的列表  /// </summary>  public List<UserControl> UcListSum  {   get   {    return _ucListSum;   }   set   {    _ucListSum = value;   }  }  #endregion  Canvas canvas_board = new Canvas();  Storyboard storyboard_imgs = new Storyboard();  int scroll_index = 0;//滚动索引  double scroll_width;//滚动宽度  void GridLayout()  {   if (Uc_Count == 0)//如果这个值没有赋值的话,则默认显示四个   {    Uc_Count = 4;   }   //给Canvas设置一些属性   canvas_board.VerticalAlignment = VerticalAlignment.Stretch;   canvas_board.HorizontalAlignment = HorizontalAlignment.Stretch;   canvas_board.Width = this.viewbox_main.ActualWidth;   canvas_board.Height = this.viewbox_main.ActualHeight;   canvas_board.ClipToBounds = true;   //用viewbox可以支持拉伸   this.viewbox_main.Child = canvas_board;   //循环将Grid加入到要展示的列表里   for (int i = 0; i < Uc_Count + 1; i++)   {    Grid grid = new Grid();    grid.Width = canvas_board.Width / Uc_Count - 10;    grid.Height = canvas_board.Height - 10;    grid.Margin = new Thickness(5);    this.canvas_board.Children.Add(grid);    grid.SetValue(Canvas.TopProperty, 0.0);    grid.SetValue(Canvas.LeftProperty, i * (grid.Width + 10));    UcListForShow.Add(grid);   }  }  void StoryLoad()  {   for (int i = 0; i < UcListForShow.Count; i++)   {//设置滚动时候的效果    DoubleAnimationUsingKeyFrames daukf_uc = new DoubleAnimationUsingKeyFrames();    LinearDoubleKeyFrame k1_uc = new LinearDoubleKeyFrame(i * (UcListForShow[i].Width + 10), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)));    LinearDoubleKeyFrame k2_uc = new LinearDoubleKeyFrame((i - 1) * (UcListForShow[i].Width + 10), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2.5)));    daukf_uc.KeyFrames.Add(k1_uc);    daukf_uc.KeyFrames.Add(k2_uc);    storyboard_imgs.Children.Add(daukf_uc);    Storyboard.SetTarget(daukf_uc, UcListForShow[i]);    Storyboard.SetTargetProperty(daukf_uc, new PropertyPath("(Canvas.Left)"));   }   storyboard_imgs.FillBehavior = FillBehavior.Stop;   storyboard_imgs.Completed += Storyboard_imgs_Completed;   storyboard_imgs.Begin();  }  private void Storyboard_imgs_Completed(object sender, EventArgs e)  {   scroll_index = scroll_index + 1 - Uc_Count;   for (int i = 0; i < UcListForShow.Count; i++)   {    UcListForShow[i].SetValue(Canvas.LeftProperty, i * (UcListForShow[i].Width + 10));    UserControl uc;    if (i == UcListForShow.Count - 1)    {     uc = UcListSum[scroll_index % UcListSum.Count];    }    else    {     uc = UcListSum[scroll_index++ % UcListSum.Count];    }    if (uc.Parent != null)    {     (uc.Parent as Grid).Children.Clear();//将Usercontrol从原来的里面移除掉,要不然会抛错,Usercontrol已属于另一个控件    }    UcListForShow[i].Children.Clear();    UcListForShow[i].Children.Add(uc);    //将隐藏按钮加入到Grid里    Button btn = new Button();    btn.Style = (dictionary["hidenStyle"] as Style);//从样式文件里读取到Button的样式    btn.Tag = UcListForShow[i].Children;//给Tag赋值,这样方便查找    btn.Click += Btn_Click;//注册隐藏事件    UcListForShow[i].Children.Add(btn);   }   storyboard_imgs.Begin();  }  private void Btn_Click(object sender, RoutedEventArgs e)  {   if ((sender as Button).Tag != null)   {    UcListSum.Remove((((sender as Button).Tag as UIElementCollection)[0] as UserControl));   }   if (UcListSum.Count == Uc_Count)//当列表数和要展示的数目相同的时候,就停止掉动画效果   {    storyboard_imgs.Completed -= Storyboard_imgs_Completed;    storyboard_imgs.Stop();    for (int i = 0; i < Uc_Count; i++)    {     UcListForShow[i].Children.Clear();     if (UcListSum[i].Parent != null)     {      (UcListSum[i].Parent as Grid).Children.Clear();     }     UcListForShow[i].Children.Add(UcListSum[i]);    }    return;   }  }  public void StartMar()  {   GridLayout();   scroll_width = this.canvas_board.Width;   for (int i = 0; i < UcListForShow.Count; i++)   {    UserControl uc;    if (i == UcListForShow.Count - 1)    {     uc = UcListSum[scroll_index % UcListSum.Count];    }    else    {     uc = UcListSum[scroll_index++ % UcListSum.Count];    }    if (uc.Parent != null)    {     (uc.Parent as Grid).Children.Clear();    }    UcListForShow[i].Children.Clear();    UcListForShow[i].Children.Add(uc);   }   StoryLoad();  }  private void grid_main_MouseLeave(object sender, MouseEventArgs e)  {   if (storyboard_imgs.GetCurrentState() == ClockState.Stopped)//如果是停止的状态,则直接返回,不再起作用   {    return;   }   if (storyboard_imgs.GetIsPaused() == true)//如果是暂停状态的话,则开始   {    storyboard_imgs.Begin();   }  }  private void grid_main_MouseMove(object sender, MouseEventArgs e)  {   if (storyboard_imgs.GetIsPaused() == false)   {    storyboard_imgs.Pause();   }  } }}

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:MarqueeUserControl"> <Style TargetType="Button" x:Key="hidenStyle">  <Setter Property="Background" Value="Transparent"/>  <Setter Property="HorizontalAlignment" Value="Center"/>  <Setter Property="VerticalAlignment" Value="Center"/>  <Setter Property="Width" Value="25"/>  <Setter Property="Height" Value="25"/>  <Setter Property="BorderBrush" Value="Transparent"/>  <Setter Property="BorderThickness" Value="0"/>  <Setter Property="Template"><!--把Image放到Template里作为Content显示,如果是单独给Content设置图片的话,则只有一个按钮显示图片,其他的不显示-->   <Setter.Value>    <ControlTemplate TargetType="{x:Type Button}">     <Border>      <Image Source="hiden.png"/>     </Border>    </ControlTemplate>   </Setter.Value>  </Setter> </Style></ResourceDictionary>

没有解决的问题

想给Button增加鼠标悬停的时候,显示,移除的时候隐藏,但是发现不好使,原因是当MouseOver上去的时候,虽然Visibility的值变了,但是只有到下一次的时候,Button的值才被附上,而此时,已经MouseLeave了,请哪位大神指导一下,看看这个显示和隐藏怎么做。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    转换文本为超连接和Email格式的代码
    下一条:
    基于nopCommerce的开发框架 附源码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    • 2025-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客