232 lines
11 KiB
C#
232 lines
11 KiB
C#
using DevComponents.DotNetBar.Controls;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace LittleRabbit.UserComponent
|
||
{
|
||
/// <summary>
|
||
/// 自定义combobox控件
|
||
/// 增加边框,
|
||
/// </summary>
|
||
#region 需要注意的地方
|
||
//DrawMode=OwnerDrawVariale
|
||
//DropDownStyle=DropDownList
|
||
#endregion
|
||
class UcComboBoxBase : ComboBoxEx
|
||
{
|
||
#region 自定义属性
|
||
private Color _BorderColor = Color.FromArgb(87, 100, 108);
|
||
private Color _BoxCenterColor = Color.FromArgb(232, 233, 237);
|
||
private Color _BoxBackColor = Color.FromArgb(212, 213, 217);
|
||
private ButtonBorderStyle _BorderStyle = ButtonBorderStyle.Solid;
|
||
private int _ArrowWidth = 2;
|
||
private Color _ArrowColor = Color.FromArgb(87, 100, 108);
|
||
private bool _ArrowFlag = true;
|
||
private Color _FontColor = Color.FromArgb(0, 0, 0);
|
||
[Description("边框颜色"), Category("Set")]
|
||
public Color BorderColor
|
||
{
|
||
set { this._BorderColor = value; }
|
||
get { return _BorderColor; }
|
||
}
|
||
[Description("框外颜色"), Category("Set")]
|
||
public Color BoxBackColor
|
||
{
|
||
set { this._BoxBackColor = value; }
|
||
get { return this._BoxBackColor; }
|
||
}
|
||
[Description("框内颜色"), Category("Set")]
|
||
public Color BoxCenterColor
|
||
{
|
||
set { this._BoxCenterColor = value; this.BackColor = value; }
|
||
get { return this._BoxCenterColor; }
|
||
}
|
||
[Description("边框样式"), Category("Set")]
|
||
public ButtonBorderStyle BoxBorderStyle
|
||
{
|
||
set { this._BorderStyle = value; }
|
||
get { return this._BorderStyle; }
|
||
}
|
||
[Description("下拉箭头宽度"), Category("Set")]
|
||
public int ArrowWidth
|
||
{
|
||
set { this._ArrowWidth = value; }
|
||
get { return this._ArrowWidth; }
|
||
}
|
||
[Description("下拉箭头颜色"), Category("Set")]
|
||
public Color ArrowColor
|
||
{
|
||
set { this._ArrowColor = value; }
|
||
get { return this._ArrowColor; }
|
||
}
|
||
[Description("是否绘制下拉箭头"), Category("Set")]
|
||
public bool ArrowEnabled
|
||
{
|
||
set { this._ArrowFlag = value; }
|
||
get { return this._ArrowFlag; }
|
||
}
|
||
[Description("字体颜色"), Category("UserSet")]
|
||
public Color FontColor
|
||
{
|
||
set { this._FontColor = value; }
|
||
get { return this._FontColor; }
|
||
}
|
||
#endregion
|
||
public UcComboBoxBase()
|
||
{
|
||
this.UseCustomBackColor = true;
|
||
|
||
// 开启双缓冲等
|
||
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.EnableNotifyMessage | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
|
||
this.DrawMode = DrawMode.OwnerDrawFixed;
|
||
this.UpdateStyles();
|
||
}
|
||
|
||
//导入API函数
|
||
[System.Runtime.InteropServices.DllImport("user32.dll ")]
|
||
static extern IntPtr GetWindowDC(IntPtr hWnd); // 返回hWnd参数所指定的窗口的设备环境。
|
||
|
||
[System.Runtime.InteropServices.DllImport("user32.dll ")]
|
||
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); // 函数释放设备上下文环境(DC)
|
||
|
||
protected override void OnDrawItem(DrawItemEventArgs e)
|
||
{
|
||
//int ItemsCount = Items.Count;
|
||
//Rectangle rect = new Rectangle(0, 0, e.Bounds.Width, e.Bounds.Height * ItemsCount);
|
||
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||
|
||
//// 绘制下拉框背景
|
||
//e.Graphics.FillRectangle(new SolidBrush(_BoxCenterColor), rect);
|
||
//// 绘制下拉框边框
|
||
//e.Graphics.DrawRectangle(new Pen(new SolidBrush(_BorderColor)), rect);
|
||
//// 绘制选中文字背景
|
||
//for (int i = 0; i < Items.Count; i++)
|
||
//{
|
||
// if (SelectedItem != null)
|
||
// {
|
||
// if (SelectedItem.ToString() == Items[i].ToString())
|
||
// {
|
||
// Rectangle rectSel = new Rectangle(1, e.Bounds.Height * (SelectedIndex), e.Bounds.Width - 2, e.Bounds.Height - 2);
|
||
// e.Graphics.FillRectangle(new SolidBrush(Color.White), rectSel);
|
||
// }
|
||
// }
|
||
// // 绘制文字
|
||
// e.Graphics.DrawString(Items[i].ToString(), Font, new SolidBrush(_FontColor), new PointF(0, i * (e.Bounds.Height == 0 ? 16 : e.Bounds.Height)));
|
||
//}
|
||
|
||
if (e.Index >= 0)
|
||
{
|
||
e.Graphics.FillRectangle(new SolidBrush(_BoxCenterColor), e.Bounds);
|
||
if ((e.State & DrawItemState.Focus) == DrawItemState.Focus || (e.State & DrawItemState.Selected) == DrawItemState.Selected)
|
||
{
|
||
// 设置鼠标悬浮ComboBox的item的背景色
|
||
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
|
||
}
|
||
else if (((e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit))
|
||
{
|
||
// 默认背景
|
||
e.Graphics.FillRectangle(new SolidBrush(e.BackColor), e.Bounds);
|
||
}
|
||
// 绘制文字
|
||
e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(_FontColor), new PointF(e.Bounds.X, e.Bounds.Y));
|
||
}
|
||
}
|
||
|
||
|
||
protected override void WndProc(ref Message m)
|
||
{
|
||
try
|
||
{
|
||
// WM_PAINT = 0xf; 要求一个窗口重画自己,即Paint事件时
|
||
// WM_CTLCOLOREDIT = 0x133; 当一个编辑型控件将要被绘制时发送此消息给它的父窗口;
|
||
// 通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色
|
||
// windows消息值表,可参考:http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html
|
||
if (m.Msg == 0xf || m.Msg == 0x133)
|
||
{
|
||
IntPtr hDC = GetWindowDC(m.HWnd);
|
||
if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 建立Graphics对像
|
||
Graphics g = Graphics.FromHdc(hDC);
|
||
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
|
||
Rectangle rectCenter = new Rectangle(1, 1, Width - 3, Height - 2);
|
||
Color boxBackColor = Enabled ? _BoxBackColor : Color.FromArgb(255, 200, 200, 200);
|
||
Color boxCenterColor = Enabled ? _BoxCenterColor : Color.FromArgb(255, 200, 200, 200);
|
||
Color fontColor = Enabled ? _FontColor : Color.FromArgb(255, 127, 127, 127);
|
||
Color arrorColor = Enabled ? _ArrowColor : Color.FromArgb(255, 127, 127, 127);
|
||
// 填充框外颜色(预留,可扩展为带圆角矩形的阴影特效)
|
||
g.DrawRectangle(new Pen(_BoxBackColor), rect);
|
||
// 填充框内颜色
|
||
if (this.DropDownStyle == ComboBoxStyle.DropDownList)
|
||
{
|
||
g.FillRectangle(new SolidBrush(boxCenterColor), rectCenter);
|
||
}
|
||
// 填充下拉按钮
|
||
g.FillRectangle(new SolidBrush(boxCenterColor), Width - Height + 2, 1, Height - 3, Height - 2);
|
||
// 绘制边框
|
||
//ControlPaint.DrawBorder(g, rect, _BorderColor, _BorderStyle);
|
||
g.DrawPolygon(new Pen(new SolidBrush(_BorderColor), 1), new Point[] {
|
||
new Point(0, 0), new Point(Width, 0), new Point(Width, Height), new Point(0, Height), new Point(0, 1) });
|
||
// 绘制下拉按钮边框
|
||
//ControlPaint.DrawBorder(g, new Rectangle(Width - Height + 3, 0, Height - 3, Height), Color.Red, ButtonBorderStyle.Solid);
|
||
// 绘制下拉按钮坚线
|
||
g.DrawLine(new Pen(_BorderColor, 1), new PointF(this.Width - this.Height + 3, 0), new PointF(this.Width - this.Height + 3, this.Height));
|
||
string selValue = this.Text;
|
||
//string text = Enabled ? selValue : "";
|
||
string text = selValue;
|
||
// 绘制下拉按钮箭头
|
||
if (_ArrowFlag)
|
||
{
|
||
g.DrawLines(new Pen(arrorColor, _ArrowWidth), new PointF[] {
|
||
new PointF(this.Width - 6, this.Height / 2 - 2),
|
||
new PointF(this.Width - 10, this.Height / 2 + 2),
|
||
new PointF(this.Width - 14, this.Height / 2 - 2)
|
||
});
|
||
}
|
||
if (this.DropDownStyle == ComboBoxStyle.DropDownList)
|
||
{
|
||
// 绘制文本
|
||
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||
g.DrawString(text, Font, new SolidBrush(fontColor), 1, 3);
|
||
//// 绘制选中文本及背景 // 废弃
|
||
//if (this.SelectionLength > 0 && SelectionStart >= 0 && SelectionStart + SelectionLength <= text.Length)
|
||
//{
|
||
// var sizeLeft = g.MeasureString(text.Substring(0, SelectionStart), Font);
|
||
// var sizeSelect = g.MeasureString(text.Substring(SelectionStart, SelectionLength), Font);
|
||
// var left = sizeLeft.Width == 0 ? 3 : sizeLeft.Width - 1;
|
||
// var width = sizeSelect.Width - 3;
|
||
// Rectangle selectRect = new Rectangle((int)left, 3, (int)width, (int)sizeSelect.Height - 1);
|
||
// g.FillRectangle(new SolidBrush(Color.FromArgb(51, 153, 255)), selectRect);
|
||
// g.DrawString((text.Substring(SelectionStart, SelectionLength)), Font, new SolidBrush(Color.White), left - 2, 3);
|
||
//}
|
||
}
|
||
ReleaseDC(m.HWnd, hDC);
|
||
}
|
||
else
|
||
{
|
||
}
|
||
}
|
||
catch (Exception ex) { }
|
||
|
||
//if (m.Msg == 0x0014) // 禁掉清除背景消息
|
||
// return;
|
||
base.WndProc(ref m);
|
||
}
|
||
|
||
private void InitializeComponent()
|
||
{
|
||
this.SuspendLayout();
|
||
this.ResumeLayout(false);
|
||
}
|
||
}
|
||
}
|