C#仿QQ皮肤-ComboBox 控件实现
2010-09-30 22:44:29 来源:WEB开发网为了让控件里的项目大小一至方便绘制和添加效果我们要重写一下OnCreateControl方法来固定一下Item的大小
protected override void OnCreateControl()
{
base.OnCreateControl();
if (!DesignMode && this.Items.Count != 0)
{
this.DropDownHeight = this.Items.Count * 17;
}
ResetBitmap();
}
ResetBitmap()方法就是用来设置不同情况下的颜色的
public void ResetBitmap()
{
this.NormalImage = Shared.NomalDrawButton;
this.MouseDownImage = Shared.MouseDownDrawButton;
this.MouseMoveImage = Shared.MouseMoveDrawButton;
}
Shared类的方法和属性请大家参考源代码里的 ,源代码的下载在 http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html
这里都有提供。
下面还是老规矩重写一下WndProc方法吧,
protected override void WndProc(ref Message m)
{
IntPtr hDC = IntPtr.Zero;
Graphics gdc = null;
switch (m.Msg)
{
case 133:
hDC = Win32.GetWindowDC(m.HWnd);
gdc = Graphics.FromHdc(hDC);
Win32.SendMessage(this.Handle, WM_ERASEBKGND, hDC.ToInt32(), 0);
SendPrintClientMsg();
Win32.SendMessage(this.Handle, WM_PAINT, 0, 0);
OverrideControlBorder(gdc);
m.Result = (IntPtr)1; // indicate msg has been processed
Win32.ReleaseDC(m.HWnd, hDC);
gdc.Dispose();
break;
case WM_PAINT:
base.WndProc(ref m);
hDC = Win32.GetWindowDC(m.HWnd);
gdc = Graphics.FromHdc(hDC);
OverrideDropDown(gdc);
OverrideControlBorder(gdc);
Win32.ReleaseDC(m.HWnd, hDC);
gdc.Dispose();
break;
default:
base.WndProc(ref m);
break;
}
}
更多精彩
赞助商链接