要求:四张图片水平滚动,每隔5秒进行一次循环,点击按钮随机变更图片。

XAML前台代码:

 <Window x:Class="图片滚动.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Window_Loaded"
Title="MainWindow"
Height="350"
Width="525">
<Viewbox>
<Canvas Width="1241" Height="768" Name="ImageCanvas" Background="SkyBlue">
<Canvas x:Name="canvas_photo" Width="1242" Height="200">
<Image x:Name="image1" Width="200" Height="200"
Canvas.Left="0" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\4.jpg">
</Image>
<Image x:Name="image3" Width="200" Height="200"
Canvas.Left="420" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\6.jpg" Canvas.Top="0">
</Image>
<Image x:Name="image4" Width="200" Height="200"
Canvas.Left="620" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\7.jpg" Canvas.Top="0">
</Image>
<Image x:Name="image2" Width="200" Height="200" Canvas.Left="218" Source="F:\zcWPF入门\图片滚动\图片滚动\bin\Debug\Images\5.jpg" Canvas.Top="0"></Image>
</Canvas>
<Button Canvas.Left="303" Canvas.Top="474" Content="Button" Height="77"
Name="button1" Width="184" Click="button1_Click" />
</Canvas>
</Viewbox>
</Window>

C#后台代码:

 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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.IO; namespace 图片滚动
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private System.Windows.Threading.DispatcherTimer dTimer = new DispatcherTimer();
List<string> listPath = new List<string>();
//设定图片集合
List<Image> listImg = new List<Image>(); double position = 0.0; public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{ listImg.Add(image1);
listImg.Add(image2);
listImg.Add(image3);
listImg.Add(image4); DoubleAnimation animation = new DoubleAnimation();
//animation.From = 0;
//animation.To = 250;
animation.RepeatBehavior = RepeatBehavior.Forever;
animation.Duration = TimeSpan.FromSeconds();
image1.BeginAnimation(Canvas.LeftProperty, animation);
//image1.BeginAnimation(Canvas.TopProperty, animation); loadInfo(); //dTimer.Tick += new EventHandler(dTimer_Tick);
//dTimer.Interval = new TimeSpan(0, 0, 5);
//dTimer.Start(); //每30毫秒触发一次定时器
dTimer.Tick += new EventHandler(dTimer_Tick);
dTimer.Interval = TimeSpan.FromMilliseconds();
dTimer.Start(); } //遍历加载指定文件下的文件
public void loadInfo()
{
string floderPath = System.Environment.CurrentDirectory + "\\Images\\";
DirectoryInfo TheFolder = new DirectoryInfo(floderPath);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
listPath.Add(NextFile.FullName);
}
} private string Window_Loaded(int index)
{
return System.Environment.CurrentDirectory + "\\Images\\" + index.ToString() + ".jpg";
} //int i;
private void dTimer_Tick(object sender, EventArgs e)
{
//if(Mouse)
foreach (var item in listImg)
{
position = Canvas.GetLeft(item);
//图片进行两个位置的移动
position = position + ;
//判断如果越界了
if (position > this.canvas_photo.Width + )
{
//则回到起点图片零起点
Canvas.SetLeft(item, );
}
else
{
//没有越界则继续移动
Canvas.SetLeft(item, position);
}
}
//for (i = 0; i < listImg.Count; i++)
//{
// position = Canvas.GetLeft(listImg[i]);
// position = position + 2;
// if (position > this.canvas_photo.Width + 200)
// {
// Canvas.SetLeft(listImg[i], 0);
// }
// else
// {
// Canvas.SetLeft(listImg[i], position);
// }
//}
} //int imageIndex = 0;
private void button1_Click(object sender, RoutedEventArgs e)
{
//随机事件
Random ran = new Random();
int RandKey = ran.Next(, ) % listPath.Count;
listImg[(new Random().Next(, )) % listImg.Count].Source = new BitmapImage(new Uri(listPath[RandKey]));
}
}
}

【demo练习三】:图片水平滚动、点击按钮变更图片动画的更多相关文章

  1. [iOS] UICollectionView实现图片水平滚动

    最新更新: 简单封装了一下代码,参考新文章:UICollectionView实现图片水平滚动 先简单看一下效果: 新博客:http://wossoneri.github.io 准备数据 首先先加入一些 ...

  2. JQuery实现点击按钮切换图片(附源码)--JQuery基础

    JQuery实现切换图片相对比较简单,直接贴代码了哈,有注释噢!疑问请追加评论哈,不足之处还请大佬们指出! 1.案例代码: demo.html: <!DOCTYPE html><ht ...

  3. javascript总结36:DOM-点击按钮切换图片案例

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  4. 鼠标点击按钮控制图片移动或者键盘控制图片移动(解决问题:parseInt失效问题,用Number)

    在测试用鼠标点击按钮,使图片左右上下移动: 效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> ...

  5. JAVA Eclipse如何设置点击按钮切换图片

    右击图片文件夹,新建一个Android XML文件   设置文件的名称,注意这个新建的xml文件就是会被用作按钮的background属性的,所以名字不要太奇怪,设置Root Element为sele ...

  6. CSS3和js炫酷点击按钮3D翻转动画特效

    简要教程 flipside是一款使用CSS3和js制作的炫酷点击按钮无缝过渡到确认面板的过渡动画特效.该点击按钮特效在按钮不同方向的边部点击时,产生的过渡动画特效是不一样的. 在线预览   源码下载 ...

  7. JS特效——图片水平滚动

    具体源码如下: <!doctype html> <html lang="en"> <head> <meta http-equiv=&quo ...

  8. [iOS] WSHorizontalPickerView 图片水平滚动封装

    之前这篇文章传送门本来是记录自己练手的demo的,后来很多人来问我要代码.今天就抽时间封装了一下,没有考虑太多情况,等我有空再去仔细考虑吧. 代码在:Github 用法很简单,创建对象,设置数据源,记 ...

  9. Android_ViewPager_实现多个图片水平滚动

    1.示意图                       2.实现分析 (1).xml配置 <!-- 配置container和pager的clipChildren=false, 并且指定margi ...

随机推荐

  1. 自定义JavaScript字典类jsdictionary.js

    /* Dictionary类:本类实现了字典功能,所有方法.属性都模仿System..Collection.Generic.Dictionary类 构造函数: Dictionary() 属性: Com ...

  2. VUE 使用中踩过的坑

    vue如今可谓是一匹黑马,github star数已居第一位!前端开发对于vue的使用已经越来越多,它的优点就不做介绍了,本篇是我对vue使用过程中以及对一些社区朋友提问我的问题中做的一些总结,帮助大 ...

  3. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. linux多线程学习笔记六--一次性初始化和线程私有数据【转】

    转自:http://blog.csdn.net/kkxgx/article/details/7513278 版权声明:本文为博主原创文章,未经博主允许不得转载. 一,一次性初始化 以保证线程在调用资源 ...

  5. 【转】CentOS 6.0 系统 LAMP(Apache+MySQL+PHP)安装步骤

    一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...

  6. LeetCode OJ--Anagrams **

    https://oj.leetcode.com/problems/anagrams/ 在一个vector<string>中,找到所有经过顺序变换,可以变成一样的 string. 首先,对每 ...

  7. Notepad++ 自动补全,括号自动完成插件,主题和字体设置

    Notepad++ 自动补全成对符号http://rabbit52.com/2012/devel/notepad-autocomplete-brackets QuickText 和 Zen Codin ...

  8. luogu P2423 双塔

    题目描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“911”事件,Mr. F决定自己用水晶来搭建一座双塔.Mr. F有N块水晶,每块 ...

  9. asp.net 面试题(附答案)

    这次面试遇到的一些问题有很基础,也有的一些我没有听过.根据经验不同或应聘职位和公司的不同等,遇到的面试问题肯定也不一样.本人两年半asp.net开发经验,这是我年后应聘asp.net工程师遇到的问题, ...

  10. java程序容错

    程序最怕出错的方式就是直接闪退 编程应该以这种方式进行,保证结构不出错,数据可容错的方式 比如 fungetsonmfrominternet(){变量 a a=从网络返回数据 return a } 在 ...