📄 xcombobox.cs
字号:
namespace Imps.Client.Pc.Controls
{
using Imps.Client.Pc;
using Imps.Client.Pc.XControlStyle;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class XComboBox : ComboBox
{
private static int _dropDownWidth = 0;
private string _emptyTip = string.Empty;
private Color _emptyTipColor = Color.DarkGray;
private bool _isEmpty;
private Color _savedColor = Color.Black;
private static XComboBoxStyle _Style = new XComboBoxStyle();
public XComboBox()
{
this.set_DoubleBuffered(true);
base.SetStyle(0x20000, true);
MakesureComboDropDownWidth(this);
this.BackColor = base.Enabled ? Color.White : Color.FromKnownColor(KnownColor.ControlLight);
base.SystemColorsChanged += new EventHandler(this.XComboBox_SystemColorsChanged);
}
private static int CalcDropDownWidth(ComboBox cb)
{
InnerWin32.ComboBoxInfo structure = new InnerWin32.ComboBoxInfo();
structure.cbSize = Marshal.SizeOf(structure);
InnerWin32.GetComboBoxInfo(cb.Handle, ref structure);
return ((structure.rcButton.Right - structure.rcButton.Left) + 2);
}
private void ClearEmptyTip()
{
if (this._isEmpty)
{
base.SuspendLayout();
this._isEmpty = false;
base.Text = string.Empty;
base.ForeColor = this._savedColor;
base.ResumeLayout();
}
}
private void DrawBorder(Graphics g)
{
ControlPaint.DrawBorder(g, new Rectangle(0, 0, base.Width, base.Height), Style.BorderColor, ButtonBorderStyle.Solid);
}
private void DrawDropDown(Graphics g)
{
Image image = Style.DrawDownImages.Images[1];
if (base.DroppedDown)
{
image = Style.DrawDownImages.Images[2];
}
using (Brush brush = new SolidBrush(this.BackColor))
{
g.FillRectangle(brush, this.DropDownButtonRectangle);
}
DrawHelper.DrawImage(g, this.DropDownButtonRectangle, image, 2);
}
private static void MakesureComboDropDownWidth(ComboBox cb)
{
if (_dropDownWidth <= 0)
{
_dropDownWidth = CalcDropDownWidth(cb);
}
}
protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);
base.Invalidate();
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
this.BackColor = base.Enabled ? Color.White : Color.FromKnownColor(KnownColor.ControlLight);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
this.ClearEmptyTip();
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Style.StyleChanged += new EventHandler(this.Style_StyleChanged);
}
protected override void OnHandleDestroyed(EventArgs e)
{
Style.StyleChanged -= new EventHandler(this.Style_StyleChanged);
base.OnHandleDestroyed(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
if (this.Text.Length == 0)
{
this.SetEmptyTip();
}
}
private void SetEmptyTip()
{
if (!this._isEmpty && !string.IsNullOrEmpty(this.EmptyTextTip))
{
base.SuspendLayout();
this._isEmpty = true;
base.Text = this.EmptyTextTip;
this._savedColor = this.ForeColor;
base.ForeColor = this.EmptyTextTipColor;
base.ResumeLayout();
}
}
private void Style_StyleChanged(object sender, EventArgs e)
{
base.Invalidate();
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == 15)
{
using (Graphics g = Graphics.FromHwnd(base.Handle))
{
if (Style.UseTheme)
{
using (Pen pen = new Pen(this.BackColor, 2f))
{
g.DrawRectangle(pen, new Rectangle(2, 2, base.Width - 4, base.Height - 4));
}
this.DrawBorder(g);
this.DrawDropDown(g);
}
}
}
}
private void XComboBox_SystemColorsChanged(object sender, EventArgs e)
{
ComboBox cb = new ComboBox();
cb.Visible = true;
_dropDownWidth = CalcDropDownWidth(cb);
cb.Dispose();
}
private Rectangle DropDownButtonRectangle
{
get
{
return new Rectangle(base.Width - _dropDownWidth, 1, _dropDownWidth - 1, base.Height - 2);
}
}
public string EmptyTextTip
{
get
{
return this._emptyTip;
}
set
{
this._emptyTip = value;
}
}
public Color EmptyTextTipColor
{
get
{
return this._emptyTipColor;
}
set
{
this._emptyTipColor = value;
}
}
public static XComboBoxStyle Style
{
get
{
return _Style;
}
set
{
_Style = value;
}
}
public override string Text
{
get
{
if (!this._isEmpty)
{
return base.Text;
}
return string.Empty;
}
set
{
this.ClearEmptyTip();
base.Text = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -