16,554
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
public class ExComboBox : ComboBox
{
private bool m_Unselectable = false;
DblPanel pnl = new DblPanel();
private const int WM_MOUSEWHEEL = 256;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONDBLCLK = 0x0203;
private const int VK_SHIFT = 0x10;
public ExComboBox()
{
pnl.Width = 17;
pnl.Height = this.Height - 2;
pnl.Left = this.Width - 18;
pnl.Top = 1;
this.Controls.Add(pnl);
pnl.BringToFront();
pnl.Visible = false;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (m_Unselectable == true)
e.Handled = true;
else
base.OnKeyPress(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (m_Unselectable == true)
{
if ((int)e.KeyData == 131139)
if (this.SelectedText != null)
Clipboard.SetText(this.SelectedText);
e.Handled = true;
}
else
base.OnKeyDown(e);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
pnl.Left = this.Width - 18;
}
public bool ReadOnly
{
get
{
return m_Unselectable;
}
set
{
m_Unselectable = value;
MakeUnselectable(m_Unselectable);
}
}
private void MakeUnselectable(bool Unselectable)
{
if (m_Unselectable == true && this.DropDownStyle != ComboBoxStyle.Simple)
{
pnl.Visible = true;
}
else
{
pnl.Visible = false;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (m_Unselectable == true)
{
if (this.DropDownStyle == ComboBoxStyle.DropDownList)
{
if (keyData != Keys.Tab)
return true;
}
else
{
if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.PageUp || keyData == Keys.PageDown)
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
}
protected override void WndProc(ref Message m)
{
if (this.m_Unselectable == true)
{
if (m.Msg == WM_MOUSEWHEEL || m.Msg == WM_LBUTTONDBLCLK)
return;
if (m.Msg == WM_LBUTTONDOWN)
{
this.Focus();
return;
}
}
base.WndProc(ref m);
}
protected override void OnDropDownStyleChanged(EventArgs e)
{
if (this.DropDownStyle == ComboBoxStyle.Simple)
pnl.Visible = false;
else
if (m_Unselectable == true)
pnl.Visible = true;
else
pnl.Visible = false;
base.OnDropDownStyleChanged(e);
}
protected class DblPanel : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
if (this.Visible == true)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics, e.ClipRectangle, System.Windows.Forms.VisualStyles.ComboBoxState.Disabled);
}
}
}
}