最近用了一下gif動畫,覺得效果不錯,winform對gif動畫也有了不錯的支援,如Button控件,ToolStripButton控件,但TreeView控件卻不支援
嘗試了一下,基本效果已經做到,其中有幾點要注意的
1. 不能使用ImageList控件來包容gif圖像,這樣做會隻有一幀畫面一直顯示
2. 需要自己重繪節點,包括圖像,文本,分割線等一切,這一點比較費功夫
3. 需要重載OnDrawNode函數,但要注意,e.Bound是指TreeView的Bound,而不是指Node的Bound
4. Node.Bound要往左剪掉一個圖像寬度,才是圖像的繪制區域左邊
今天就不下載下傳了,直接拷貝代碼吧
代碼如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace TreeViewAnimateDemo
{
class ImageTreeView : TreeView
{
Image animateImage;
public ImageTreeView()
{
DrawMode = TreeViewDrawMode.OwnerDrawAll;
animateImage = Properties.Resources.Csearch_00; //指定一個Gif動畫
System.Drawing.ImageAnimator.Animate(animateImage, this.onFrameChanged);
}
void onFrameChanged(object sender, EventArgs e)
{
Invalidate();
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
//e.DrawDefault = true;
base.OnDrawNode(e);
e.Graphics.DrawImage(animateImage, new Rectangle(e.Node.Bounds.X - 16, e.Node.Bounds.Y, 16, 16));
using (Brush brush = new SolidBrush(this.ForeColor))
{
Font font = e.Node.NodeFont == null ? this.Font : e.Node.NodeFont;
e.Graphics.DrawString(e.Node.Text, font, brush, e.Node.Bounds.Location);
}
//e.DrawDefault = true;
}
}
}
運作前,先改寫一個能用的gif動畫哦
補充,這個隻是個Demo,需要自行改善
作者:superlight