简单扩展winform中的ListBox,实现项闪烁、项变色,和代码拉动滚动条(新手村真是冷,斗胆放在这里,如果不合适,立马撤下)

紧紧为了实现功能而很简单的实现,有让大家眼睛受罪的地方请大家多多指教,只有大家的鞭策才能让我更好的进步,所以请不要珍惜您的烂菜叶、臭鸡蛋

 

附加代码:代码控制滚动条,网上关于控制滚动条的资料不多,我是直接从c->c#的,所以对API也不是很了解,高手请无视

关于刷新问题,请看这篇博文 GDI+学习(4) 双缓冲技术 

 

demo下载

 

 

效果图

  

 

 

Code
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
//关于这个方法的原型,第一个参数是句柄,第二个是要发送的消息类型,
        
//后面2个则是发送消息的附加参数1、2。其实你可以自由重载比如你也可以声明成这样
        
//public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam);
        
//public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string wParam2);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam);
        
//定义一个常量,这个是垂直滚动条消息值
        public static int WM_VSCROLL = 0x115;
        Timer timer;
        
private void button1_Click(object sender, EventArgs e)
        {
            timer 
= new Timer();
            timer.Interval 
= 100;
            
int i = 0;
            timer.Tick 
+= new EventHandler(delegate(object sender1, EventArgs e1) 
            {
                
//如果当前项不在可见区域内,下拉滚动条,+1是为了不让避免当前项可能发生的被覆盖问题
                if (this.exListBox1.Items.Count>i+1&&!exListBox1.ClientRectangle.Contains(exListBox1.GetItemRectangle(i+1)))
                    SendMessage(exListBox1.Handle, WM_VSCROLL, (IntPtr)(
3));
                exListBox1.Flash(i, 
100);
                
if (i < exListBox1.Items.Count - 1)
                    i
++;
                
else
                    timer.Dispose();
            });
            timer.Start();
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
//发送滚动消息,这里是往下
            SendMessage(exListBox1.Handle, WM_VSCROLL, (IntPtr)(3));
        }

        
private void button3_Click(object sender, EventArgs e)
        {
            
//发送滚动消息,这里是往上
            SendMessage(exListBox1.Handle, WM_VSCROLL, (IntPtr)(3));
        }
    }

 

 

下面是ListBox的实现

 

 

Code
Code
 
public class ExListBox:System.Windows.Forms.ListBox
    {
        
public ExListBox() 
        {
            
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        }

        
private Color[] flashColors ={ Color.Red, Color.White, Color.Black,Color.Gold };
        
/// <summary>
        
/// 闪动颜色数组
        
/// </summary>
        public Color[] FlashColors
        {
            
get { return flashColors; }
            
set { flashColors = value; }
        }
        
private int flashTimeSpan = 500;
        
/// <summary>
        
/// 闪动间隔
        
/// </summary>
        public int FlashTimeSpan
        {
            
get { return flashTimeSpan; }
            
set { flashTimeSpan = value; }
        }

        
private Color selectForeColor = Color.White;
        
/// <summary>
        
/// 选择项颜色
        
/// </summary>
        public Color SelectForeColor
        {
            
get { return selectForeColor; }
            
set { selectForeColor = value; }
        }

        
private Color selectBackColor = Color.CornflowerBlue;
        
/// <summary>
        
/// 选择项背景色
        
/// </summary>
        public Color SelectBackColor
        {
            
get { return selectBackColor; }
            
set { selectBackColor = value; }
        }
        
/// <summary>
        
/// 使指定项闪动
        
/// </summary>
        
/// <param name="index"></param>
        
/// <param name="timeSpan">时间长度,单位为毫秒</param>
        
/// <param name="color">颜色组,不同颜色切换</param>
        public void Flash(int index,int timeSpan,params Color[] color) 
        {
            
if (0 <= index && index <= this.Items.Count)
            {
                
if (timeSpan < 0)
                    timeSpan 
= flashTimeSpan;
                
if (color == null || color.Length < 2)
                    color 
= flashColors;
                
int i = 0;
                
object objItem = this.Items[index];
                ListBoxItem item;
                
if(objItem is ListBoxItem)
                    item 
= this.Items[index] as ListBoxItem;
                
else
                {
                    item 
= new ListBoxItem();
                    item.Text 
= this.Items[index].ToString();
                }
                System.Timers.Timer timer 
= new System.Timers.Timer(this.flashTimeSpan);
                timer.Elapsed 
+= new System.Timers.ElapsedEventHandler(delegate(object sender, System.Timers.ElapsedEventArgs e)
                {
                    
using (Graphics g = this.CreateGraphics())
                    {
                        DeleInvokeItemRectangle dele 
= new DeleInvokeItemRectangle(InvokeItemRectangle);
                        Rectangle rect 
= (Rectangle)this.Invoke(dele, index);
                        
if (rect.IsEmpty == false)
                        {
                            g.DrawString(item.Text, item.Font, 
new SolidBrush(color[i]), rect);
                            
if (i < color.Length - 1)
                                i
++;
                            
else
                            {
                                timer.Stop();
                                timer.Dispose();
                            }
                        }
                    }
                    
                });
                timer.Disposed 
+= new EventHandler(delegate(object sender, EventArgs e)
                {
                    
this.Invoke(new DeleRefresh(this.Refresh));
                    i 
= 0;
                });
                timer.Start();
            }
        }

        
delegate Rectangle DeleInvokeItemRectangle(int index);
        
public Rectangle InvokeItemRectangle(int index) 
        {
            
return this.GetItemRectangle(index);
        }
        
delegate void DeleRefresh();
        
//捕捉选定索引改变事件,用于绘制被选择项文本颜色
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            
if (this.SelectedIndex >= 0&&null!=this.Items[this.SelectedIndex]) 
            {
                Rectangle rect 
= this.GetItemRectangle(this.SelectedIndex);
                ListBoxItem item;
                
if (this.SelectedItem is ListBoxItem)
                    item 
= this.SelectedItem as ListBoxItem;
                
else
                {
                    item 
= new ListBoxItem();
                    item.Text 
= this.SelectedItem.ToString();
                }
                
using (Graphics g = this.CreateGraphics()) 
                {
                    g.DrawString(item.Text, item.Font, 
new SolidBrush(this.SelectForeColor), rect);
                }
            }
            
base.OnSelectedIndexChanged(e);
        }
        
//绘制项的颜色
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            
if (this.Items.Count < 1)
                
return;
            
try
            {
                
object objItem = this.Items[e.Index];
                ListBoxItem item;
                
if (objItem is ListBoxItem)
                    item 
= objItem as ListBoxItem;
                
//属性栏中直接添加的字符串数组,应该转换
                else
                {
                    item 
= new ListBoxItem();
                    item.Text 
= this.Items[e.Index].ToString();
                }
                
if (string.IsNullOrEmpty(item.Text))
                    
return;
                
if (item.Color.IsEmpty)
                    item.Color 
= this.ForeColor;
                
using (Graphics g = e.Graphics)
                {
                    
//如果该项被选择
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                        e.Graphics.FillRectangle(
new SolidBrush(this.SelectBackColor), e.Bounds);
                    
else
                        e.Graphics.FillRectangle(
new SolidBrush(this.BackColor), e.Bounds);
                    
//画出项文本
                    g.DrawString(item.Text, item.Font, new SolidBrush(item.Color), e.Bounds.X, e.Bounds.Y);
                    e.DrawFocusRectangle();
                }
            }
            
catch (IndexOutOfRangeException ee)
            {
                
throw ee;
            }
            
base.OnDrawItem(e);
        }
    }

 

 

 

 

posted @ 2009-07-27 16:23  番茄爱炒蛋  阅读(2364)  评论(3编辑  收藏  举报