重绘ComboBox[C#] .

重绘ComboBox[C#]

-------------------------------------------------------------------------------------

偶然一个机会,居然发现, override ComboBox的OnPaint不会被调用.所以导致不能像普通控件一样重绘ComboBox了.

-------------------------------------------------------------------------------------

我们可以重写窗口过程(WndProc),来处理消息.

建立一个类继承自ComboBox,

class CustomeComboBox : ComboBox

 

 

 

 

[c-sharp] view plain copy print ?
  1. //导入API函数   
  2.        [System.Runtime.InteropServices.DllImport("user32.dll ")]  
  3.        static extern IntPtr GetWindowDC(IntPtr hWnd);//返回hWnd参数所指定的窗口的设备环境。   
  4.   
  5.        [System.Runtime.InteropServices.DllImport("user32.dll ")]  
  6.        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //函数释放设备上下文环境(DC)   
  7.   
  8.        protected override void WndProc(ref   Message m)  
  9.        {  
  10.            base.WndProc(ref   m);  
  11.            //WM_PAINT = 0xf; 要求一个窗口重画自己,即Paint事件时   
  12.            //WM_CTLCOLOREDIT = 0x133;当一个编辑型控件将要被绘制时发送此消息给它的父窗口;   
  13.            //通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色   
  14.            //windows消息值表,可参考:http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html   
  15.            if (m.Msg == 0xf || m.Msg == 0x133)           
  16.            {  
  17.                IntPtr hDC = GetWindowDC(m.HWnd);  
  18.                if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回   
  19.                {  
  20.                    return;   
  21.                }  
  22.   
  23.                //建立Graphics对像   
  24.                Graphics g = Graphics.FromHdc(hDC);  
  25.                //画边框的    
  26.                ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), Color.Red, ButtonBorderStyle.Solid);  
  27.                //画坚线   
  28.                ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid);  
  29.                //g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height));   
  30.                //释放DC     
  31.                ReleaseDC(m.HWnd, hDC);         
  32.            }  
  33.        }   
  

注:本文参考了http://topic.csdn.net/u/20070728/01/7e5ebf43-fce8-4db1-a05c-d44c612d1391.html

 

 

转自:http://blog.csdn.net/sugar_tiger/article/details/4900641

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值