FlatなComboBox

http://www.mitene.or.jp/~sugisita/index.html
PAPA'nさんのサイトにあったVB.NETC#に書き換えてみました。


public FlatComb()
{
// この呼び出しは、Windows.Forms フォーム デザイナで必要です。
InitializeComponent();

// TODO: InitializeComponent 呼び出しの後に初期化処理を追加します。
brBackground = new SolidBrush(base.BackColor);
brButton = new SolidBrush(this.m_ButtonColor);
brButtonText = new SolidBrush(this.m_ButtonForeColor);
brText = new SolidBrush(SystemColors.WindowText);
pnBorder = new Pen(this.m_BorderColor);

}

///


/// 使用されているリソースに後処理を実行します。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
brBackground.Dispose();
brButton.Dispose();
brButtonText.Dispose();
pnBorder.Dispose();
brText.Dispose();
fntArrow.Dispose();
base.Dispose( disposing );
}

#region コンポーネント デザイナで生成されたコード
///


/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///

private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

private const int WM_PAINT = 0x000F;
private Color m_BackColor = SystemColors.Window;
private Color m_BorderColor = SystemColors.ControlDark;
private Color m_ButtonColor = SystemColors.Control;
private Color m_ButtonForeColor = SystemColors.ControlText;
private SolidBrush brBackground;
private SolidBrush brButton;
private SolidBrush brButtonText;
private SolidBrush brText;
private Pen pnBorder;
private Font fntArrow = new Font("Marlett", 9);

protected override void WndProc(ref Message m) {
base.WndProc (ref m);
if (m.Msg == WM_PAINT) {
Graphics g = this.CreateGraphics();
RectangleF rctBackground = new RectangleF(this.ClientRectangle.Left,
this.ClientRectangle.Top,
this.ClientRectangle.Width - (SystemInformation.VerticalScrollBarWidth + SystemInformation.Border3DSize.Width + 1),
this.ClientRectangle.Height - 1);
RectangleF rctButton = new RectangleF(rctBackground.Left + rctBackground.Width,
rctBackground.Top,
SystemInformation.VerticalScrollBarWidth + SystemInformation.Border3DSize.Width,
rctBackground.Height);
//-- 背景の塗りつぶし
g.FillRectangle(brBackground, rctBackground);
g.FillRectangle(brButton, rctButton);

//-- 境界線の描画
g.DrawRectangle(pnBorder, Rectangle.Ceiling(rctBackground));
g.DrawRectangle(pnBorder, Rectangle.Ceiling(rctButton));

//-- DropDownStyleがDropDownListの場合、テキストを描画
if (this.DropDownStyle == ComboBoxStyle.DropDownList) {
StringFormat fmtText = new StringFormat();
RectangleF rctText = new RectangleF(rctBackground.Left + SystemInformation.Border3DSize.Width,
rctBackground.Top + SystemInformation.Border3DSize.Height,
rctBackground.Width - (SystemInformation.Border3DSize.Width * 2),
rctBackground.Height - (SystemInformation.Border3DSize.Height * 2));
fmtText.Alignment = StringAlignment.Near;
fmtText.LineAlignment = StringAlignment.Center;
if (this.Focused && this.DroppedDown == false) {
SolidBrush brHighlight = new SolidBrush(SystemColors.Highlight);
SolidBrush brHighlightText = new SolidBrush(SystemColors.HighlightText);
g.FillRectangle(brHighlight, rctText);
g.DrawString(this.Text, this.Font, brHighlightText, rctText, fmtText);
brHighlightText.Dispose();
brHighlight.Dispose();
}else{
g.DrawString(this.Text, this.Font, brText, rctText, fmtText);
}

}

//-- ボタン矢印の描画
StringFormat fmtArrow = new StringFormat();
fmtArrow.Alignment = StringAlignment.Center;
fmtArrow.LineAlignment = StringAlignment.Center;
g.DrawString("6", fntArrow, brButtonText, rctButton, fmtArrow);

g.Dispose();
}
}
//protected override void Finalize() {
// //-- 念のため、おそうじ
// brBackground.Dispose();
// brButton.Dispose();
// brButtonText.Dispose();
// pnBorder.Dispose();
// brText.Dispose();
// fntArrow.Dispose();
// base.Finalize();
//}
public override Color BackColor {
get {
return base.BackColor;
}
set {
brBackground.Dispose();
brBackground = new SolidBrush(value);
base.BackColor = value;
}
}
public override Color ForeColor {
get {
return base.ForeColor;
}
set {
brText.Dispose();
brText = new SolidBrush(value);
base.ForeColor = value;
}
}
[Browsable(true), Category("Appearance")]
public Color BorderColor {
get {
return m_BorderColor;
}
set {
m_BorderColor = value;
pnBorder.Dispose();
pnBorder = new Pen(m_BorderColor);
this.Invalidate();
}
}
[Browsable(true), Category("Appearance")]
public Color ButtonColor {
get {
return m_ButtonColor;
}
set {
m_ButtonColor = value;
brButton.Dispose();
brButton = new SolidBrush(m_ButtonColor);
this.Invalidate();
}
}
[Browsable(true), Category("Appearance")]
public Color ButtonForeColor {
get {
return m_ButtonForeColor;
}
set {
m_ButtonForeColor = value;
brButtonText.Dispose();
brButtonText = new SolidBrush(m_ButtonForeColor);
this.Invalidate();
}
}
public new ComboBoxStyle DropDownStyle {
get {
return base.DropDownStyle;
}
set {
if (value == ComboBoxStyle.Simple) {
MessageBox.Show(this,"FlatCombでは「Simple」に指定できません。","プロパティ指定",MessageBoxButtons.OK,MessageBoxIcon.Warning);
value = ComboBoxStyle.DropDown;
}
base.DropDownStyle = value;
}
}