I want to create an borderless WinForm which has a custom header with the Default System Icons for:
- Minimization
- Maximization
- Closing
Is there any Way i can achieve this in C# or VB?
I want to create an borderless WinForm which has a custom header with the Default System Icons for:
Is there any Way i can achieve this in C# or VB?
For everyone how wants a fully customizable button in the windows 10 aero style.
Here it is:
First, a base class which fixes the error, that a button which is focused gets an outline when the form loses focus:
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsControls {
/// <summary>
/// Modified button which has no focus rectangles when the form which contains this button loses fucus while the button was focused.
/// </summary>
[ToolboxItem(typeof(NoFocusCueBotton))]
public class NoFocusCueBotton : Button {
protected override bool ShowFocusCues => false;
/// <summary>
/// Creates a new instance of a <see cref="NoFocusCueBotton"/>
/// </summary>
public NoFocusCueBotton() { }
public override void NotifyDefault(bool value) {
base.NotifyDefault(false);
}
}
}
Next the actual button:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsControls {
/// <summary>
/// Button which represents the default close, minimize or maximize buttons of the windows 10 aero theme.
/// </summary>
[ToolboxItem(true)]
public class WindowsDefaultTitleBarButton : NoFocusCueBotton {
/// <summary>
/// Represents the 3 possible types of the windows border buttons.
/// </summary>
public enum Type {
Close,
Maximize,
Minimize
}
private Pen activeIconColorPen;
private Brush activeIconColorBrush;
private Brush activeColorBrush;
/// <summary>
/// The type which defines the buttons behaviour.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(Type.Close)]
[Category("Appearance")]
[Description("The type which defines the buttons behaviour.")]
public Type ButtonType { get; set; }
/// <summary>
/// The background color of the button when the mouse is inside the buttons bounds.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(null)]
[Category("Appearance")]
[Description("The background color of the button when the mouse is inside the buttons bounds.")]
public Color HoverColor { get; set; }
/// <summary>
/// The background color of the button when the button is clicked.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(null)]
[Category("Appearance")]
[Description("The background color of the button when the button is clicked.")]
public Color ClickColor { get; set; }
/// <summary>
/// The default color of the icon.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(null)]
[Category("Appearance")]
[Description("The default color of the icon.")]
public Color IconColor { get; set; }
/// <summary>
/// The color of the icon when the mouse is inside the buttons bounds.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(null)]
[Category("Appearance")]
[Description("The color of the icon when the mouse is inside the buttons bounds.")]
public Color HoverIconColor { get; set; }
/// <summary>
/// The color of the icon when the button is clicked.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DefaultValue(null)]
[Category("Appearance")]
[Description("The color of the icon when the button is clicked.")]
public Color ClickIconColor { get; set; }
/// <summary>
/// Property which returns the active background color of the button depending on if the button is clicked or hovered.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public virtual Color ActiveColor {
get {
if (this.Clicked)
return this.ClickColor;
if (this.Hovered)
return this.HoverColor;
return BackColor;
}
}
/// <summary>
/// Property which returns the active color of the buttons icon depending on if the button is clicked or hovered.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public virtual Color ActiveIconColor {
get {
if (this.Clicked)
return this.ClickIconColor;
if (this.Hovered)
return this.HoverIconColor;
return IconColor;
}
}
/// <summary>
/// Property which indicates if the mouse is currently inside the bounds of the button.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DefaultValue(false)]
public bool Hovered { get; set; }
/// <summary>
/// Property which indicates if the left mouse button was pressed down inside the buttons bounds. Can be true before the click event is triggered.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DefaultValue(false)]
public bool Clicked { get; set; }
public WindowsDefaultTitleBarButton() { }
protected override void OnMouseEnter(EventArgs e) {
base.OnMouseEnter(e);
Hovered = true;
}
protected override void OnMouseLeave(EventArgs e) {
base.OnMouseLeave(e);
Hovered = false;
}
protected override void OnMouseDown(MouseEventArgs mevent) {
base.OnMouseDown(mevent);
Clicked = true;
}
protected override void OnMouseUp(MouseEventArgs mevent) {
base.OnMouseUp(mevent);
Clicked = false;
}
protected override void OnClick(EventArgs e) {
if (ButtonType == Type.Close)
this.FindForm()?.Close();
else if (ButtonType == Type.Maximize)
this.FindForm().WindowState = this.FindForm().WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
else
this.FindForm().WindowState = FormWindowState.Minimized;
base.OnClick(e);
}
protected override void OnPaint(PaintEventArgs pevent) {
System.Diagnostics.Trace.WriteLine(pevent.ClipRectangle.ToString());
activeColorBrush?.Dispose();
activeColorBrush = new SolidBrush(ActiveColor);
pevent.Graphics.FillRectangle(new SolidBrush(ActiveColor), pevent.ClipRectangle);
pevent.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
activeIconColorBrush?.Dispose();
activeIconColorPen?.Dispose();
activeIconColorBrush = new SolidBrush(ActiveIconColor);
activeIconColorPen = new Pen(activeIconColorBrush, 1.0f);
if (ButtonType == Type.Close)
drawCloseIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
else if (ButtonType == Type.Maximize)
drawMaximizeIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
else
drawMinimizeIcon(pevent, new Rectangle(0, 0, this.Width, this.Height));
}
protected virtual void drawCloseIcon(PaintEventArgs e, Rectangle drawRect) {
e.Graphics.DrawLine(
activeIconColorPen,
drawRect.X + drawRect.Width / 2 - 5,
drawRect.Y + drawRect.Height / 2 - 5,
drawRect.X + drawRect.Width / 2 + 5,
drawRect.Y + drawRect.Height / 2 + 5);
e.Graphics.DrawLine(
activeIconColorPen,
drawRect.X + drawRect.Width / 2 - 5,
drawRect.Y + drawRect.Height / 2 + 5,
drawRect.X + drawRect.Width / 2 + 5,
drawRect.Y + drawRect.Height / 2 - 5); ;
}
protected virtual void drawMaximizeIcon(PaintEventArgs e, Rectangle drawRect) {
if (this.FindForm().WindowState == FormWindowState.Normal) {
e.Graphics.DrawRectangle(
activeIconColorPen,
new Rectangle(
drawRect.X + drawRect.Width / 2 - 5,
drawRect.Y + drawRect.Height / 2 - 5,
10, 10));
} else if (this.FindForm().WindowState == FormWindowState.Maximized) {
e.Graphics.DrawRectangle(
activeIconColorPen,
new Rectangle(
drawRect.X + drawRect.Width / 2 - 3,
drawRect.Y + drawRect.Height / 2 - 5,
8, 8));
Rectangle rect = new Rectangle(
drawRect.X + drawRect.Width / 2 - 5,
drawRect.Y + drawRect.Height / 2 - 3,
8, 8);
e.Graphics.FillRectangle(activeIconColorBrush, rect);
e.Graphics.DrawRectangle(activeIconColorPen, rect);
}
}
protected virtual void drawMinimizeIcon(PaintEventArgs e, Rectangle drawRect) {
e.Graphics.DrawLine(
activeIconColorPen,
drawRect.X + drawRect.Width / 2 - 5,
drawRect.Y + drawRect.Height / 2,
drawRect.X + drawRect.Width / 2 + 5,
drawRect.Y + drawRect.Height / 2);
}
}
}
The six colors make the button fully costomizable regarding to the appearance.
Use the WindowsDefaultTitleBarButton.Type ButtonType
to chose which behavior and icon the button should have.
Example: Using the color theme of visual studio 2019 project selection window.
Values:
Form:
BackColor: #252526
Close Button:
Size: 46x30
BackColor: Transparent
HoverColor: #e81123
ClickColor: #f1707a
IconColor: #f1f1f1
HoverIconColor: #ffffff
ClickIconColor: #ffffff
MinMax Buttons:
Size: 46x30
BackColor: Transparent
HoverColor: #3f3f40
ClickColor: #007acc
IconColor: #f1f1f1
HoverIconColor: #ffffff
ClickIconColor: #ffffff
Not sure what your exact goal is, but gernerally, for 'custom'-designs using C# I would prefer WPF (Windows Presentation Foundation) instead of Windows Forms...
I guess it´s possible in Windows Forms too, maybe if you remove the borders as wanted and create 3 Buttons, using the common Windows-symbols as their background? But I´m not sure if it´s working ;)
EDIT :
using a controls 'paint'-event, you should be able to reach your goal:
private void button_Paint(object sender, PaintEventArgs e)
{
if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.Window.CloseButton.Normal))
{
VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.CloseButton.Normal);
Rectangle rectangle1 = new Rectangle(button.Location.X, button.Location.Y, button.Width, button.Height);
renderer.DrawBackground(e.Graphics, rectangle1);
}
}
this is checking if you are able to use the styles, and then draw the selected VisualStyleElement (eg. CloseButton, MinButton, etc.) to the button/control´s position.
See VisualStylesElement-CloseButton and Control.Paint-Event for more information.
works fine for me, hope it´s what you´ve been looking for...
As per my knowledge you can Use "Anchor property" + "Doc Property" of windows form by placing three buttons with images.
Or here i shown one example that is showing customized theme for windows form with controls.
This sample is in Vb.NET.Save as class in your project,build your project and then controls will be appear in toolbox area.Drag & Drop that control. Enjoy!
Try This T3 Vb.NET Themes Archives