WinForm中ComboBox的DrawItem事件的应用

WinForm中ComboBox的DrawItem事件的应用

2008年11月24日 2,208 Views

今天在CSDN的论坛看到一个问题,大致是这么个意思:
当鼠标滑过ComboBox的列表项时,怎么才能获取当前鼠标滑过列表项?

在网上找了找,没现成的,但是看到有人在VC里边用OnDrawItem来实现,就用了这个思路。
首先将将DrawMode属性设置为OwnerDrawFixed
然后绑定DrawItem事件,具体方法如下:

  1. private void moveComboBox1_DrawItem(object sender, DrawItemEventArgs e)
  2. {
  3.             //经测试,鼠标划过时,e.State 为 DrawItemState.Selected
  4.             if (e.State == DrawItemState.Selected)
  5.             {
  6.                 e.Graphics.FillRectangle(new SolidBrush(Color.Beige), new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
  7.                 //显示鼠标滑过项目的文本
  8.                 label1.Text = moveComboBox1.Items[e.Index].ToString();
  9.             }
  10.             else if (e.State == DrawItemState.None)
  11.             {
  12.                 e.Graphics.FillRectangle(new SolidBrush(Color.White), new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
  13.             }
  14.  
  15.             e.Graphics.DrawString(moveComboBox1.Items[e.Index].ToString(), moveComboBox1.Font, new SolidBrush(Color.Black), 2, e.Bounds.Y + 2);
  16. }

转载于:https://www.cnblogs.com/oyzh/archive/2009/07/02/1515356.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值