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

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

 

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

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

 

demo下载

 

 

效果图

r_%e6%9c%aa%e5%91%bd%e5%90%8d.JPG

  

 

 

ContractedBlock.gif ExpandedBlockStart.gif 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的实现

 

 

ContractedBlock.gif ExpandedBlockStart.gif 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);
        }
    }

 

 

 

 

转载于:https://www.cnblogs.com/fairy-tale/archive/2009/07/27/1532188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ListBox 控件默认情况下不支持横向滚动条,但可以通过在 ListBox 控件上添加一个水平滚动条(HScrollBar)来实现横向滚动条的效果。 具体实现步骤如下: 1. 将 ListBox 的 HorizontalScrollbar 属性设置为 true,使其支持水平滚动条。 2. 在 ListBox 控件的父容器添加一个 HScrollBar 控件,并设置其 Dock 属性为 Bottom,使其位于 ListBox 控件的下方。 3. 给 HScrollBar 控件的 ValueChanged 事件添加处理程序,在处理程序设置 ListBox 的 HorizontalExtent 和 HorizontalScrollbar 属性,以实现横向滚动条的滚动效果。 示例代码如下: ```csharp private void Form1_Load(object sender, EventArgs e) { // 设置 ListBox 控件支持水平滚动listBox1.HorizontalScrollbar = true; // 创建 HScrollBar 控件并设置其 Dock 属性为 Bottom HScrollBar hScrollBar1 = new HScrollBar(); hScrollBar1.Dock = DockStyle.Bottom; hScrollBar1.Minimum = 0; hScrollBar1.Maximum = 100; hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll); // 将 HScrollBar 控件添加到 ListBox 控件的父容器 this.Controls.Add(hScrollBar1); } private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) { // 设置 ListBox 的 HorizontalExtent 和 HorizontalScrollbar 属性,以实现横向滚动条的滚动效果 listBox1.HorizontalExtent = 200; listBox1.HorizontalScrollbar = true; } ``` 通过上述代码,可以实现ListBox 控件上显示横向滚动条的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值