Wpf跑马灯——DrawingVisual运用

本文将建立一个wpf项目中运用DrawingVisual绘制跑马灯效果的简单实例,以下是详细步骤:


新建一个wpf项目,添加演示用图片,修改图片属性为"如果较新则复制"。


在MainWindow.xaml中,为系统自动创建的Grid容器命名,这样可以在后台操作的到它。

<Window x:Class="WpfPicRoll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="gd_main"></Grid>
</Window>

在后台代码中,添加窗体布局。

        public MainWindow()
{
InitializeComponent();
this.WindowLayout();
}

Image img_roll = new Image();

void WindowLayout()
{
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.Width = 300;
this.Height = 300;

img_roll.Margin = new Thickness(10, 10, 0, 0);
img_roll.HorizontalAlignment = HorizontalAlignment.Left;
img_roll.VerticalAlignment = VerticalAlignment.Top;
img_roll.Stretch = Stretch.Fill;
img_roll.Width = 200;
img_roll.Height = 200;
img_roll.Source = new BitmapImage(new Uri(@"Images\bg.jpg", UriKind.Relative));
this.gd_main.Children.Add(img_roll);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.InitPicRoll();
}

这里使用一个Image控件作为演示区域,背景图片可有可无。


完成初始化滚动事件InitPicRoll,

        List<BitmapImage> ls_images = new List<BitmapImage>(); //存放图片组
DispatcherTimer t_remain = new DispatcherTimer(); //切换
DispatcherTimer t_roll = new DispatcherTimer(); //滚动
int n_index = 0; //滚动索引
double n_height; //滚动高度
double n_width; //滚动宽度
double n_top; //滚动上边距

void InitPicRoll()
{
string[] img_files = Directory.GetFiles(string.Format("{0}/Images", AppDomain.CurrentDomain.SetupInformation.ApplicationBase), "*.png");
foreach (string img_path in img_files)
{
ls_images.Add(new BitmapImage(new Uri(img_path, UriKind.Absolute)));
}
n_height = img_roll.Height;
n_width = img_roll.Width;
t_remain.Interval = new TimeSpan(0, 0, 5);
t_remain.Tick += new EventHandler(t_remain_Tick);
t_roll.Interval = new TimeSpan(0, 0, 0, 0, 30);
t_roll.Tick += new EventHandler(t_roll_Tick);
t_remain.Start();
t_roll.Start();
}

wpf较winform中的用法有一些不同之处,例如:

获取程序启动路径:在winform中可以使用Application.StartupPath,在wpf中可以使用AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

获取一张图片素材:在winform中可以使用Image.FromFile(img_path)的方法获取一个Image文件,在wpf中Image将作为一个显示用控件出现,可以使用new BitmapImage(new Uri(img_path, UriKind.Absolute))方法,将图片素材文件保存为一个bitmap,这里的img_path素材路径需要指明UriKind是相对还是绝对路径;

使用时钟:在winform中可以new 一个Timer时钟,但是在wpf中却没有Timer,在wpf中可以new 一个DispatcherTimer,可以代替Timer完成我们需要的工作。


下面完成图片切换时钟t_remain和滚动控制时钟t_roll,代码如下:

void t_remain_Tick(object sender, EventArgs e)
{
n_index = ++n_index % ls_images.Count;
n_top = 0;
t_roll.Start();
}

void t_roll_Tick(object sender, EventArgs e)
{
n_top -= 5;
if (n_top <= -n_height)
{
t_roll.Stop();
}

DrawingVisual drawingVisual = new DrawingVisual();

DrawingContext drawingContext = drawingVisual.RenderOpen();
for (int i = 0; i < 2; i++)
{
drawingContext.DrawImage(ls_images[(n_index + i) % ls_images.Count()], new Rect(0, n_top + i * n_height, 200, 200));
}
drawingContext.Close();

RenderTargetBitmap composeImage = new RenderTargetBitmap(200, 200, 0, 0, PixelFormats.Pbgra32);
composeImage.Render(drawingVisual);

img_roll.Source = composeImage;
}


注意到我们在winform中可以使用Graphics画笔来Draw一个Image,在wpf中我们可以使用DrawingVisual来代替,DrawingVisual是一个绘图类,当你new 一个DrawingVisual对象时,该对象并没有绘图内容,需要使用DrawingContext在其中进行绘制,步骤为:

1.new DrawingVisual.

2.调用DrawingVisual的RenderOpen方法,new DrawingContext.

3.使用DrawingContext来完成绘制动作.

4.调用DrawingContext的Close方法并保存内容.

5.new RenderTargetBitmap用作呈现.

6.调用RenderTargetBitmap的Render方法呈现已经完成绘制动作的DrawingVisual.


重新生成项目,F5运行,效果如下:




转载于:https://www.cnblogs.com/xwlyun/archive/2012/03/06/2382245.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值