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運行,效果如下:





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM