这是我的拆分按钮实现。它不绘制箭头,聚焦/不聚焦行为略有不同。
我的和原版都处理视觉风格,并且在 Aero 上看起来很棒。
基于http://wyday.com/splitbutton/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
using System.ComponentModel;
using System.Diagnostics;
// Original: http://blogs.msdn.com/jfoscoding/articles/491523.aspx
// Wyatt's fixes: http://wyday.com/splitbutton/
// Trimmed down and redone significantly from that version (Nick 5/6/08)
namespace DF
{
public class SplitButton : Button
{
private ContextMenuStrip m_SplitMenu = null;
private const int SplitSectionWidth = 14;
private static int BorderSize = SystemInformation.Border3DSize.Width * 2;
private bool mBlockClicks = false;
private Timer mTimer;
public SplitButton()
{
this.AutoSize = true;
mTimer = new Timer();
mTimer.Interval = 100;
mTimer.Tick += new EventHandler(mTimer_Tick);
}
private void mTimer_Tick(object sender, EventArgs e)
{
mBlockClicks = false;
mTimer.Stop();
}
#region Properties
[DefaultValue(null)]
public ContextMenuStrip SplitMenu
{
get
{
return m_SplitMenu;
}
set
{
if (m_SplitMenu != null)
m_SplitMenu.Closing -=
new ToolStripDropDownClosingEventHandler(m_SplitMenu_Closing);
m_SplitMenu = value;
if (m_SplitMenu != null)
m_SplitMenu.Closing +=
new ToolStripDropDownClosingEventHandler(m_SplitMenu_Closing);
}
}
private void m_SplitMenu_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
HideContextMenuStrip();
// block click events for 0.5 sec to prevent re-showing the menu
}
private PushButtonState _state;
private PushButtonState State
{
get
{
return _state;
}
set
{
if (!_state.Equals(value))
{
_state = value;
Invalidate();
}
}
}
#endregion Properties
protected override void OnEnabledChanged(EventArgs e)
{
if (Enabled)
State = PushButtonState.Normal;
else
State = PushButtonState.Disabled;
base.OnEnabledChanged(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
if (State.Equals(PushButtonState.Disabled))
return;
if (mBlockClicks)
return;
if (!State.Equals(PushButtonState.Pressed))
ShowContextMenuStrip();
else
HideContextMenuStrip();
}
protected override void OnMouseEnter(EventArgs e)
{
if (!State.Equals(PushButtonState.Pressed) && !State.Equals(PushButtonState.Disabled))
{
State = PushButtonState.Hot;
}
}
protected override void OnMouseLeave(EventArgs e)
{
if (!State.Equals(PushButtonState.Pressed) && !State.Equals(PushButtonState.Disabled))
{
if (Focused)
{
State = PushButtonState.Default;
}
else
{
State = PushButtonState.Normal;
}
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Graphics g = pevent.Graphics;
Rectangle bounds = this.ClientRectangle;
// draw the button background as according to the current state.
if (State != PushButtonState.Pressed && IsDefault && !Application.RenderWithVisualStyles)
{
Rectangle backgroundBounds = bounds;
backgroundBounds.Inflate(-1, -1);
ButtonRenderer.DrawButton(g, backgroundBounds, State);
// button renderer doesnt draw the black frame when themes are off =(
g.DrawRectangle(SystemPens.WindowFrame, 0, 0, bounds.Width - 1, bounds.Height - 1);
}
else
{
ButtonRenderer.DrawButton(g, bounds, State);
}
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.DrawString(Text, Font, SystemBrushes.ControlText, bounds, format);
}
private void ShowContextMenuStrip()
{
State = PushButtonState.Pressed;
if (m_SplitMenu != null)
{
m_SplitMenu.Show(this, new Point(0, Height), ToolStripDropDownDirection.BelowRight);
}
}
private void HideContextMenuStrip()
{
State = PushButtonState.Normal;
m_SplitMenu.Hide();
mBlockClicks = true;
mTimer.Start();
}
}
}