SmartButton
A push button with a crisp, bevelled Windows XP look in blue, olive or silver – or a gradient in colours of your own – that can show an icon with its full alpha channel and can open a drop-down menu of commands.
public partial class SmartButton : System.Windows.Forms.Control, IButtonControl
Inherits from / implements: System.Windows.Forms.Control, IButtonControl
Remarks
Use it wherever a Button would go: it raises
Control.Click, takes a DialogResult, and implements
IButtonControl, so it can be a form's Form.AcceptButton or
Form.CancelButton. Space clicks it while it has the focus, and an ampersand in
Text gives it an Alt+letter shortcut.
Two looks
- XP bevel (
IsRenderingThemeon, the default). The button is drawn in the Windows XP style chosen byXPTheme, with an amber edge under the pointer, a darker face while pressed, and a blue (in the olive scheme, green) edge while it is the form's default button. SetDefaultThemetofalsebefore choosingXPTheme: whileDefaultThemeis on, the style follows the Windows colour scheme and a value set inXPThemeis ignored. - Gradient (
IsRenderingThemeoff). The face is a gradient fromStartColortoEndColoratLinearGradientRenderingAngle, inside a one-pixelBorderColoredge. It lightens under the pointer and reverses while pressed (or usesPushedStartColorandPushedEndColor).
A disabled button is always drawn with the grey XP bevel, with grey text and a greyed image.
Text and image
Text is centred, in ForeColor, and trimmed with an ellipsis when it
does not fit; UseCustomTextStringFormat and TextStringFormat lay it out
your way. Icon shows an icon with its alpha channel intact (soft shadows and edges);
Image shows a bitmap, with TransparentColor made see-through. Either sits
at the left of the text, or in the middle when there is no text.
Menu button
With ButtonStyle set to ButtonStyles.MenuButton, the button shows a
separator and a down arrow at its right, and a click opens a menu under it instead of raising
Control.Click. The menu is the button's Control.ContextMenuStrip when one is
set; otherwise it is a ContextMenu built from DropDownMenuItems, drawn with
ContextMenuProperties when its items are BinaryMenuItem
objects and UseCustomDefinedPropertiesForRenderingContextMenu is on. Handle
OnAboutToTheShowContextMenuOnMenuButton to update or cancel the menu as it opens.
The button has a size of 10 by 10 pixels when created; give it a Control.Size.
Examples
A "Save" button in the default blue style that is the form's accept button, and an "Export" menu button beside it.
using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.ButtonControls;
using Binarymission.WinForms.Controls.ButtonControls.SmartButtonControls.Models;
public class InvoiceForm : Form
{
public InvoiceForm()
{
Text = "Invoice INV-20431";
SmartButton save = new SmartButton
{
Text = "&Save",
Location = new Point(16, 16),
Size = new Size(96, 30),
Icon = new Icon("save.ico", 16, 16)
};
save.Click += (sender, e) => SaveInvoice();
SmartButton export = new SmartButton
{
Text = "Export",
Location = new Point(124, 16),
Size = new Size(110, 30),
ButtonStyle = ButtonStyles.MenuButton,
DropDownMenuItems = new MenuItem[]
{
new MenuItem("As PDF...", (s, e) => ExportInvoice("pdf")),
new MenuItem("As Excel workbook...", (s, e) => ExportInvoice("xlsx")),
new MenuItem("As CSV...", (s, e) => ExportInvoice("csv"))
}
};
Controls.Add(save);
Controls.Add(export);
AcceptButton = save;
}
private void SaveInvoice() { /* ... */ }
private void ExportInvoice(string format) { /* ... */ }
}The same button drawn as a gradient in the application's own colours.
SmartButton approve = new SmartButton
{
Text = "Approve",
Size = new Size(110, 32),
IsRenderingTheme = false,
StartColor = Color.FromArgb(46, 160, 67),
EndColor = Color.FromArgb(28, 110, 44),
LinearGradientRenderingAngle = 90f,
BorderColor = Color.FromArgb(20, 80, 32),
ForeColor = Color.White
};See also
Members at a glance
| Constructors | |
|---|---|
SmartButton | Initialises a new button in the blue XP style, with black text, ready to be given a size and text. |
| Properties | |
BorderColor | Gets or sets the colour of the one-pixel edge round the button, when it is drawn as a gradient. |
ButtonStyle | Gets or sets whether the button is a plain push button or a menu button that opens a drop-down menu. |
ContextMenuProperties | Gets or sets the colours and rendering style of the drop-down menu, used while
UseCustomDefinedPropertiesForRenderingContextMenu is on. |
DefaultTheme | Gets or sets whether the XP style follows the Windows colour scheme rather than XPTheme. |
DialogResult | Gets or sets the result a click gives the form the button is on. |
DrawFocusRectangle | Gets or sets whether a dotted focus rectangle is drawn inside the button while it has the keyboard focus. |
DrawMenuButtonSeparator | Gets or sets whether a menu button draws a separator line between its text and its arrow. |
DropDownArrowMarkColor | Gets or sets the colour of the down arrow at the right of a menu button. |
DropDownMenuItems | Gets or sets the commands a menu button lists in its drop-down menu. |
DropDownMenuItems | Gets or sets the commands a menu button lists in its drop-down menu. |
DropDownMenuRendererTheme | Gets or sets the built-in theme for the drop-down menu that a menu button builds from
DropDownMenuItems. |
EndColor | Gets or sets the colour the face's gradient ends with, when the button is drawn as a gradient. |
ForeColor | Gets or sets the colour of the button's text. |
Icon | Gets or sets an icon to show on the button, drawn with its alpha channel so soft edges and shadows look right. |
Image | Gets or sets a bitmap to show on the button. |
IsRenderingTheme | Gets or sets whether the button is drawn with the Windows XP bevel or with your own gradient. |
LinearGradientRenderingAngle | Gets or sets the direction of the face's gradient, when the button is drawn as a gradient. |
MenuButtonSeparatorColor | Gets or sets the colour of the separator line between a menu button's text and its arrow. |
MenuButtonSeparatorLineHeight | Gets or sets the height of the separator line between a menu button's text and its drop-down arrow. |
MenuStripColorTable | Gets or sets your own colours for the drop-down menu that a menu button builds from
DropDownMenuItems. |
PushedEndColor | Gets or sets the colour the face's gradient ends with while the button is pressed or its menu is open. |
PushedStartColor | Gets or sets the colour the face's gradient starts from while the button is pressed or its menu is open. |
ShowDropDownArrow | Gets or sets whether a menu button shows its down arrow. |
StartColor | Gets or sets the colour the face's gradient starts from, when the button is drawn as a gradient. |
Text | Gets or sets the caption shown on the button. |
TextStringFormat | Gets or sets the alignment, trimming and wrapping used for the caption when
UseCustomTextStringFormat is on. |
TransparentColor | Gets or sets the colour in Image that is made see-through, so the button's face shows
through it. |
UseCustomDefinedPropertiesForRenderingContextMenu | Gets or sets whether the drop-down menu is drawn with the colours in ContextMenuProperties. |
UseCustomTextStringFormat | Gets or sets whether the caption is laid out with TextStringFormat rather than centred. |
UseUserDefinedColorForArrowMark | Gets or sets whether a menu button's arrow keeps the colour you give it rather than following
XPTheme. |
UseUserDefinedColorForMenuButtonSeparator | Gets or sets whether a menu button's separator line keeps the colour you give it rather than following
XPTheme. |
XPTheme | Gets or sets which Windows XP colour scheme the bevelled button is drawn in. |
| Methods | |
Dispose | Releases the drawing resources the button holds. |
NotifyDefault | Tells the button whether it is the form's default button, so it can draw itself as one. |
OnClick | Handles a click: a menu button raises OnAboutToTheShowContextMenuOnMenuButton and opens
its menu; any other button applies its DialogResult and raises the
Control.Click event. |
OnDpiChangedAfterParent | Repaints the button when it moves to a display with a different scale factor. |
OnEnter | Raises the Control.Enter event and repaints the button. |
OnHandleCreated | Raises the Control.HandleCreated event, sets the button's outline for the display, and
applies the theme and separator colour. |
OnKeyDown | Raises the Control.KeyDown event, showing the button pressed while Enter or Space is held down. |
OnKeyUp | Raises the Control.KeyUp event after releasing the pressed look, and clicks the button when the key is Space. |
OnLeave | Raises the Control.Leave event and repaints the button. |
OnMouseDown | Raises the Control.MouseDown event, and on a left-button press shows the button pressed and gives it the focus. |
OnMouseEnter | Raises the Control.MouseEnter event and shows the button's hover look. |
OnMouseLeave | Raises the Control.MouseLeave event and returns the button to its normal look. |
OnMouseMove | Raises the Control.MouseMove event and shows the button pressed while the left button is held down over it. |
OnMouseUp | Raises the Control.MouseUp event and releases the pressed look of a plain button; a menu button stays pressed while its menu is open. |
OnPaint | Draws the button – its bevel or gradient, image, text, focus rectangle and, on a menu button, the
separator and arrow – then raises the Control.Paint event. |
OnParentChanged | Raises the Control.ParentChanged event after matching the button's background to its
parent. |
OnSizeChanged | Raises the Control.SizeChanged event after fitting the outline, the image position and
MenuButtonSeparatorLineHeight to the new size. |
OnSystemColorsChanged | Raises the Control.SystemColorsChanged event and, while DefaultTheme is on,
picks up a change of Windows colour scheme. |
OnTextChanged | Raises the Control.TextChanged event after re-laying out the caption and image. |
PerformClick | Clicks the button from code, as if the user had clicked it. |
ProcessMnemonic | Clicks the button when its Alt+letter shortcut is pressed. |
WndProc | Processes Windows messages, and returns a menu button to its unpressed look when its menu closes. |
| Events | |
OnAboutToTheShowContextMenuOnMenuButton | Occurs when a menu button has been clicked, just before its drop-down menu opens. |
Constructors
SmartButton
Initialises a new button in the blue XP style, with black text, ready to be given a size and text.
Properties
BorderColor
Gets or sets the colour of the one-pixel edge round the button, when it is drawn as a gradient.
- Value
- The default is
Color.Black.
Remarks
Used only while IsRenderingTheme is false. The edge is one
logical pixel wide.
ButtonStyle
Gets or sets whether the button is a plain push button or a menu button that opens a drop-down menu.
- Value
ButtonStyles.NormalorButtonStyles.MenuButton. The default isButtonStyles.Normal.- Default
ButtonStyles.Normal
Remarks
A menu button draws a separator and a down arrow at its right and, when clicked, raises
OnAboutToTheShowContextMenuOnMenuButton and opens its menu – the
Control.ContextMenuStrip if one is set, otherwise one built from
DropDownMenuItems. It does not raise Control.Click.
Setting it to ButtonStyles.MenuButton turns ShowDropDownArrow back
on, so set that afterwards to hide the arrow.
DefaultTheme
Gets or sets whether the XP style follows the Windows colour scheme rather than XPTheme.
- Value
trueto follow Windows; the default istrue.
Remarks
While this is on, a value set in XPTheme is ignored. The Windows XP Olive Green and
Silver schemes give XPThemes.OliveGreen and XPThemes.Silver; every other
scheme, including those of current Windows versions, gives XPThemes.Blue.
To choose the style yourself, set this to false first and then set
XPTheme.
Example
saveButton.DefaultTheme = false;
saveButton.XPTheme = XPThemes.Silver;DialogResult
Gets or sets the result a click gives the form the button is on.
- Value
- One of the
DialogResultvalues. The default isNone, which leaves the form alone.
Remarks
When it is not None, a click sets the form's
DialogResult, which closes a form shown with Form.ShowDialog. A value
outside the enumeration is ignored. Not applied by a ButtonStyles.MenuButton.
Example
okButton.DialogResult = DialogResult.OK;
cancelButton.DialogResult = DialogResult.Cancel;
CancelButton = cancelButton;
if (new CustomerDetailsForm().ShowDialog() == DialogResult.OK)
{
RefreshCustomerList();
}DrawFocusRectangle
Gets or sets whether a dotted focus rectangle is drawn inside the button while it has the keyboard focus.
- Value
trueto draw it; the default isfalse.- Default
false
Remarks
Turn it on for forms people move through with the Tab key. Not drawn on a disabled button.
DropDownArrowMarkColor
Gets or sets the colour of the down arrow at the right of a menu button.
- Value
- The arrow colour. The default is
Color.RoyalBlue, the blue XP scheme's colour.
Remarks
A disabled button draws the arrow in the system grey text colour. To have the arrow follow
XPTheme instead, set UseUserDefinedColorForArrowMark to
false.
See also
EndColor
Gets or sets the colour the face's gradient ends with, when the button is drawn as a gradient.
- Value
- The default is
Color.White.
Remarks
Used only while IsRenderingTheme is false.
See also
ForeColor
Gets or sets the colour of the button's text.
- Value
- The text colour. The default is
SystemColors.ControlText. - Default
ControlText"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint
Remarks
A disabled button draws its text in the system grey text colour.
Icon
Gets or sets an icon to show on the button, drawn with its alpha channel so soft edges and shadows look right.
- Value
- The icon, or
nullfor none. The default isnull. - Default
null), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint
Remarks
The icon is drawn at its own size, 7 logical pixels from the left edge and centred vertically,
with the text to its right – or in the middle of the button when Text is empty. On a
disabled button it is drawn greyed.
A button shows an icon or an Image, not both: setting an icon clears
Image.
Example
printButton.Icon = new Icon(Properties.Resources.PrinterIcon, 24, 24);
printButton.Text = "Print";Image
Gets or sets a bitmap to show on the button.
- Value
- The image, or
nullfor none. The default isnull. - Default
null), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint
Exceptions
System.InvalidCastException- The image is not a
Bitmap, such as aMetafile.
Remarks
The image must be a Bitmap. Pixels of TransparentColor are made
see-through, and the button keeps its own copy. The pixels are also made see-through in the bitmap you
pass, so pass a copy if you use that bitmap elsewhere.
The image is placed as Icon is, and greyed on a disabled button. A button shows an
image or an Icon, not both: setting an image clears Icon. For images with
soft, semi-transparent edges, use Icon.
IsRenderingTheme
Gets or sets whether the button is drawn with the Windows XP bevel or with your own gradient.
- Value
truefor the XP bevel inXPTheme;falsefor a gradient fromStartColortoEndColorwith aBorderColoredge. The default istrue.
Remarks
A disabled button is drawn with the grey XP bevel whatever this is set to.
LinearGradientRenderingAngle
Gets or sets the direction of the face's gradient, when the button is drawn as a gradient.
- Value
- The angle in degrees, measured clockwise from the left-to-right direction: 0 runs left to right, 90 top to bottom. The default is 90.
Remarks
Used only while IsRenderingTheme is false.
PushedEndColor
Gets or sets the colour the face's gradient ends with while the button is pressed or its menu is open.
- Value
- The default is
Color.Empty, which usesStartColor– so a pressed button shows its own gradient reversed.
Remarks
Used only while IsRenderingTheme is false.
See also
PushedStartColor
Gets or sets the colour the face's gradient starts from while the button is pressed or its menu is open.
- Value
- The default is
Color.Empty, which usesEndColor– so a pressed button shows its own gradient reversed.
Remarks
Used only while IsRenderingTheme is false.
See also
ShowDropDownArrow
Gets or sets whether a menu button shows its down arrow.
- Value
trueto show the arrow; the default istrue.- Default
true), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint
Remarks
A value set while ButtonStyle is not ButtonStyles.MenuButton is
ignored, and setting ButtonStyle to ButtonStyles.MenuButton turns the arrow
back on – so set this after ButtonStyle.
StartColor
Gets or sets the colour the face's gradient starts from, when the button is drawn as a gradient.
- Value
- The default is
Color.Silver.
Remarks
Used only while IsRenderingTheme is false. The gradient runs in
the direction of LinearGradientRenderingAngle. Under the pointer the gradient is
lightened by about a fifth towards white.
See also
Text
Gets or sets the caption shown on the button.
- Value
- The caption. An ampersand before a letter (
"&Save") underlines it and makes Alt+letter click the button.
Remarks
The caption is centred in the space beside any image, in ForeColor, and is
trimmed with an ellipsis when it does not fit, unless UseCustomTextStringFormat says
otherwise.
TextStringFormat
Gets or sets the alignment, trimming and wrapping used for the caption when
UseCustomTextStringFormat is on.
- Value
- A
StringFormat, ornull. The default isnull.
Remarks
The button reads a copy at every repaint, so changes made to the object later are picked up. Ampersand shortcuts are always shown underlined, whatever the format says.
Example
nextButton.TextStringFormat = new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Center
};
nextButton.UseCustomTextStringFormat = true;TransparentColor
Gets or sets the colour in Image that is made see-through, so the button's face shows
through it.
- Value
- The colour to remove, such as the magenta background of a toolbar bitmap. The default is
Color.Empty, which removes nothing.
Remarks
Applies to the current image straight away and to any image set later. It has no effect on
Icon, which carries its own transparency.
Example
refreshButton.TransparentColor = Color.Magenta;
refreshButton.Image = Properties.Resources.Refresh16;UseCustomTextStringFormat
Gets or sets whether the caption is laid out with TextStringFormat rather than centred.
- Value
trueto useTextStringFormat; the default isfalse, which centres the caption both ways and trims it at a word with an ellipsis.
Remarks
Has no effect until TextStringFormat is set.
UseUserDefinedColorForArrowMark
Gets or sets whether a menu button's arrow keeps the colour you give it rather than following
XPTheme.
- Value
trueto keepDropDownArrowMarkColor;falseto colour the arrow to match the XP scheme (royal blue, olive or black). The default istrue.
Remarks
Setting it to false recolours the arrow straight away.
XPTheme
Gets or sets which Windows XP colour scheme the bevelled button is drawn in.
- Value
- One of the
XPThemesvalues. The default isXPThemes.Blue.
Remarks
Ignored while DefaultTheme is true (the default) – set that to
false first. Used only while IsRenderingTheme is on.
When UseUserDefinedColorForMenuButtonSeparator or
UseUserDefinedColorForArrowMark is off, the separator and arrow change to the scheme's
colour: royal blue, olive or black.
Methods
Dispose
Releases the drawing resources the button holds.
Parameters
disposingtruewhen called fromDispose();falsewhen called from a finaliser.
NotifyDefault
Tells the button whether it is the form's default button, so it can draw itself as one.
Parameters
valuetrueto draw the default button's accent – a blue (in the olive scheme, green) edge on the XP bevel;falseto draw it as an ordinary button.
Remarks
The form calls this itself as the focus moves and when the button is its
Form.AcceptButton; there is seldom a reason to call it from code.
OnClick
Handles a click: a menu button raises OnAboutToTheShowContextMenuOnMenuButton and opens
its menu; any other button applies its DialogResult and raises the
Control.Click event.
Parameters
e- The event data.
Remarks
A derived class that overrides this should call the base implementation, or the menu will not
open and Control.Click will not be raised.
OnDpiChangedAfterParent
Repaints the button when it moves to a display with a different scale factor.
Parameters
e- The event data.
OnEnter
Raises the Control.Enter event and repaints the button.
Parameters
e- The event data.
OnHandleCreated
Raises the Control.HandleCreated event, sets the button's outline for the display, and
applies the theme and separator colour.
Parameters
e- The event data.
OnKeyDown
Raises the Control.KeyDown event, showing the button pressed while Enter or Space is held down.
Parameters
ke- The key that was pressed.
OnKeyUp
Raises the Control.KeyUp event after releasing the pressed look, and clicks the button when the key is Space.
Parameters
ke- The key that was released.
OnLeave
Raises the Control.Leave event and repaints the button.
Parameters
e- The event data.
OnMouseDown
Raises the Control.MouseDown event, and on a left-button press shows the button pressed and gives it the focus.
Parameters
e- The pointer position and buttons.
OnMouseEnter
Raises the Control.MouseEnter event and shows the button's hover look.
Parameters
e- The event data.
OnMouseLeave
Raises the Control.MouseLeave event and returns the button to its normal look.
Parameters
e- The event data.
OnMouseMove
Raises the Control.MouseMove event and shows the button pressed while the left button is held down over it.
Parameters
e- The pointer position and buttons.
OnMouseUp
Raises the Control.MouseUp event and releases the pressed look of a plain button; a menu button stays pressed while its menu is open.
Parameters
e- The pointer position and buttons.
OnPaint
Draws the button – its bevel or gradient, image, text, focus rectangle and, on a menu button, the
separator and arrow – then raises the Control.Paint event.
Parameters
e- The graphics surface and clip rectangle to paint.
Remarks
A handler of Control.Paint draws on top of the finished button.
OnParentChanged
Raises the Control.ParentChanged event after matching the button's background to its
parent.
Parameters
e- The event data.
Remarks
The button's Control.BackColor is set to a fully transparent version of the
parent's background colour, so the corners outside the bevel show the parent. The button also calls
this on every resize, so Control.ParentChanged is raised then too. When the button is
removed from its parent nothing is done and the event is not raised.
OnSizeChanged
Raises the Control.SizeChanged event after fitting the outline, the image position and
MenuButtonSeparatorLineHeight to the new size.
Parameters
e- The event data.
OnSystemColorsChanged
Raises the Control.SystemColorsChanged event and, while DefaultTheme is on,
picks up a change of Windows colour scheme.
Parameters
e- The event data.
OnTextChanged
Raises the Control.TextChanged event after re-laying out the caption and image.
Parameters
e- The event data.
PerformClick
Clicks the button from code, as if the user had clicked it.
Remarks
Does nothing while the button is hidden or disabled. A ButtonStyles.MenuButton
opens its menu; any other button raises Control.Click and applies its
DialogResult.
ProcessMnemonic
Clicks the button when its Alt+letter shortcut is pressed.
Parameters
charCode- The character typed with Alt.
Returns
true when charCode is the letter after the ampersand in
Text and the button was clicked; otherwise the result of the base implementation.
WndProc
Processes Windows messages, and returns a menu button to its unpressed look when its menu closes.
Parameters
m- The message to process.