c#仿qq皮肤系列之combobox 控件实现

上传人:第*** 文档编号:31320637 上传时间:2018-02-06 格式:DOCX 页数:14 大小:36.56KB
返回 下载 相关 举报
c#仿qq皮肤系列之combobox 控件实现_第1页
第1页 / 共14页
c#仿qq皮肤系列之combobox 控件实现_第2页
第2页 / 共14页
c#仿qq皮肤系列之combobox 控件实现_第3页
第3页 / 共14页
c#仿qq皮肤系列之combobox 控件实现_第4页
第4页 / 共14页
c#仿qq皮肤系列之combobox 控件实现_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《c#仿qq皮肤系列之combobox 控件实现》由会员分享,可在线阅读,更多相关《c#仿qq皮肤系列之combobox 控件实现(14页珍藏版)》请在金锄头文库上搜索。

1、C#仿 QQ 皮肤系列之:ComboBox 控件实现 2011-01-19 来自:博客园 字体大小:【大 中 小】 摘要:这个控件是在系统控件 ComboBox 的基础之上来实现的 这个控件是在系统控件 ComboBox 的基础之上来实现的,我们先来看一下实现后的效果这是实现 后的效果,与普通的不同之处有,颜色有所变化,倒三角有所变化,再就是有一个鼠标跟随的效果。下面我来看看是怎么样实现的吧。我们第一步要打开我们的项目,然后右击项目,新添加一个 Component 组件。然后继承一下 Combox 类代码如下public class ComboBox : System.Windows.Form

2、s.ComboBox下面我们先定义几个变量/鼠标 Move 事件时图片private Image _mouseMoveImage = null;/鼠标 mouseDown 事件时图片private Image _mouseDownImage = null; /private Image _normalImage = null;下面我看看再在构造方法里设置一下些固定的属性代码 public ComboBox(): base()this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);/设置为手动绘制this.DrawMode = DrawM

3、ode.OwnerDrawFixed;/设置固定的 DropDownList 样式this.DropDownStyle = ComboBoxStyle.DropDownList;this.UpdateStyles();为了让控件里的项目大小一至方便绘制和添加效果我们要重写一下 OnCreateControl 方法来固定一下 Item 的大小代码 protected override void OnCreateControl()base.OnCreateControl();if (!DesignMode & this.Items.Count != 0)this.DropDownHeight =

4、this.Items.Count * 17;ResetBitmap();ResetBitmap()方法就是用来设置不同情况下的颜色的代码 public void ResetBitmap()this.NormalImage = Shared.NomalDrawButton;this.MouseDownImage = Shared.MouseDownDrawButton;this.MouseMoveImage = Shared.MouseMoveDrawButton;Shared 类的方法和属性请大家参考源代码里的 ,源代码的下载在 http:/ 这里都有提供。下面还是老规矩重写一下 WndPro

5、c 方法吧,代码 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, W

6、M_PAINT, 0, 0);OverrideControlBorder(gdc);m.Result = (IntPtr)1; / indicate msg has been processedWin32.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.Rele

7、aseDC(m.HWnd, hDC);gdc.Dispose();break;default:base.WndProc(ref m);break; 因为我们的图片颜色是手动绘制上去的所以我们应该在 OverrideDropDown 方法里进行绘制,也就是说当我们出现下拉列表的时候要绘制一下效果。代码 private void OverrideDropDown(Graphics g)if (DesignMode) return;Rectangle rect = new Rectangle(this.Width - DropDownButtonWidth, 0, DropDownButtonWid

8、th, this.Height);g.FillRectangle(new SolidBrush(Color.White), rect);if (this.Enabled)if (_mouseEnter)g.DrawImage(this.MouseMoveImage, new Rectangle(this.Width - 20, 3, 16, 16);elseg.DrawImage(this.NormalImage, new Rectangle(this.Width - 20, 3, 16, 16);elseg.DrawImage(Shared.NotEnableDrawButton, new

9、Rectangle(this.Width - 20, 3, 16, 16);根据鼠标的状态不同绘制出不同的效果,大家看一下 If 语句 就明白 了还有一个更重的方法就是 OnDrawItem 方法这是在加载时绘制一下初始化的效果代码 protected override void OnDrawItem(DrawItemEventArgs e)Graphics g = e.Graphics;/绘制区域Rectangle r = e.Bounds;Font fn = null;if (e.Index = 0)if (e.State = DrawItemState.None)/设置字体、字符串格式、

10、对齐方式fn = e.Font;string s = this.Itemse.Index.ToString ();StringFormat sf = new StringFormat();sf.Alignment = StringAlignment.Near;/根据不同的状态用不同的颜色表示if (e.State = (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);e.Graphics.DrawString(s, fn,

11、 new SolidBrush(Color.Black), r, sf);e.DrawFocusRectangle();elsee.Graphics.FillRectangle(new SolidBrush(Color.White), r);e.Graphics.DrawString(s, fn, new SolidBrush(Shared.FontColor), r, sf);e.DrawFocusRectangle(); elsefn = e.Font;StringFormat sf = new StringFormat();sf.Alignment = StringAlignment.N

12、ear;string s = this.Itemse.Index.ToString ();e.Graphics.FillRectangle(new SolidBrush(Shared.ControlBackColor), r);e.Graphics.DrawString(s, fn, new SolidBrush(Shared.FontColor), r, sf);有了这些我们现在要做的就是在 OnMouseEnter 事件 OnMouseLeave 事件里处理一下绘制的效果和参数就算是完工了我们一起来看看这两个事件的处理方法吧代码 protected override void OnMous

13、eEnter(EventArgs e)_mouseEnter = true;IntPtr hDC = IntPtr.Zero;Graphics gdc = null;hDC = Win32.GetWindowDC(this.Handle);gdc = Graphics.FromHdc(hDC);gdc.DrawImage(Shared.MouseMoveDrawButton, new Rectangle(this.Width - 20, 3, 16, 16);Win32.ReleaseDC(this.Handle, hDC);gdc.Dispose();base.OnMouseEnter(e)

14、;protected override void OnMouseLeave(EventArgs e) _mouseEnter = false;IntPtr hDC = IntPtr.Zero;Graphics gdc = null;hDC = Win32.GetWindowDC(this.Handle);gdc = Graphics.FromHdc(hDC);gdc.DrawImage(Shared.NomalDrawButton, new Rectangle(this.Width - 20, 3, 16, 16);Win32.ReleaseDC(this.Handle, hDC);base.

15、OnMouseLeave(e);到这里其实就可以完工了,其它的方法和事件都是一相辅助,等下可以看一下全部代码。具体的使用方法和系统的 Combox 控件没有任何的分别。直接绑定数据就 OK 了。为了方便以后的改动我把三个效果时产生的图片都定义成属性大家使用起来会方便一些代码 public Image MouseMoveImagegetreturn _mouseMoveImage;set_mouseMoveImage = value;public Image MouseDownImagegetreturn _mouseDownImage;set_mouseDownImage = value; public Image NormalImagegetreturn _normalImage;set_normalImage = value;整个类的代码如下代码 using System;using System.Collections.Generic;using System.Drawing;using System.Text;using System.Windows.Forms;using CRD.Common;namespace CRD.WinUI.Miscpublic class ComboBox :

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号