299 lines
8.8 KiB
C#
299 lines
8.8 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Forms.IHM.Controls
|
|
{
|
|
[DefaultEvent("Click")]
|
|
public class HmiMenuTile : UserControl
|
|
{
|
|
private readonly TableLayoutPanel _layout;
|
|
private readonly PictureBox _picIcone;
|
|
private readonly Label _lblTitulo;
|
|
private readonly Label _lblSubtitulo;
|
|
|
|
private Color _fillColor = HmiTheme.SurfaceRaised;
|
|
private Color _borderColor = HmiTheme.Border;
|
|
private Color _accentColor = HmiTheme.Success;
|
|
private int _cornerRadius = 13;
|
|
private bool _pressed;
|
|
|
|
public HmiMenuTile()
|
|
{
|
|
SetStyle(
|
|
ControlStyles.AllPaintingInWmPaint |
|
|
ControlStyles.UserPaint |
|
|
ControlStyles.OptimizedDoubleBuffer |
|
|
ControlStyles.ResizeRedraw |
|
|
ControlStyles.SupportsTransparentBackColor,
|
|
true);
|
|
|
|
DoubleBuffered = true;
|
|
BackColor = Color.Transparent;
|
|
Cursor = Cursors.Hand;
|
|
MinimumSize = new Size(150, 110);
|
|
Margin = new Padding(6);
|
|
|
|
_layout = new TableLayoutPanel
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
BackColor = Color.Transparent,
|
|
ColumnCount = 1,
|
|
RowCount = 3,
|
|
Margin = Padding.Empty,
|
|
Padding = new Padding(8, 7, 8, 7)
|
|
};
|
|
_layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
|
|
_layout.RowStyles.Add(new RowStyle(SizeType.Absolute, 27F));
|
|
_layout.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
|
|
|
|
_picIcone = new PictureBox
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
BackColor = Color.Transparent,
|
|
SizeMode = PictureBoxSizeMode.Zoom,
|
|
Margin = new Padding(48, 4, 48, 3),
|
|
Cursor = Cursors.Hand,
|
|
TabStop = false
|
|
};
|
|
|
|
_lblTitulo = new Label
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
AutoSize = false,
|
|
BackColor = Color.Transparent,
|
|
ForeColor = HmiTheme.Text,
|
|
Font = new Font("Segoe UI", 13F, FontStyle.Bold),
|
|
Text = "TÍTULO",
|
|
TextAlign = ContentAlignment.MiddleCenter,
|
|
Margin = Padding.Empty,
|
|
Cursor = Cursors.Hand,
|
|
AutoEllipsis = true
|
|
};
|
|
|
|
_lblSubtitulo = new Label
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
AutoSize = false,
|
|
BackColor = Color.Transparent,
|
|
ForeColor = HmiTheme.TextMuted,
|
|
Font = new Font("Segoe UI", 8.25F, FontStyle.Regular),
|
|
Text = string.Empty,
|
|
TextAlign = ContentAlignment.TopCenter,
|
|
Margin = Padding.Empty,
|
|
Cursor = Cursors.Hand,
|
|
AutoEllipsis = true
|
|
};
|
|
|
|
_layout.Controls.Add(_picIcone, 0, 0);
|
|
_layout.Controls.Add(_lblTitulo, 0, 1);
|
|
_layout.Controls.Add(_lblSubtitulo, 0, 2);
|
|
Controls.Add(_layout);
|
|
|
|
WireChild(_layout);
|
|
WireChild(_picIcone);
|
|
WireChild(_lblTitulo);
|
|
WireChild(_lblSubtitulo);
|
|
|
|
UpdateRoundedRegion();
|
|
}
|
|
|
|
[Category("Conteúdo")]
|
|
public Image Icone
|
|
{
|
|
get { return _picIcone.Image; }
|
|
set { _picIcone.Image = value; Invalidate(); }
|
|
}
|
|
|
|
[Category("Conteúdo")]
|
|
[DefaultValue("TÍTULO")]
|
|
public string Titulo
|
|
{
|
|
get { return _lblTitulo.Text; }
|
|
set { _lblTitulo.Text = value ?? string.Empty; }
|
|
}
|
|
|
|
[Category("Conteúdo")]
|
|
[DefaultValue("")]
|
|
public string Subtitulo
|
|
{
|
|
get { return _lblSubtitulo.Text; }
|
|
set { _lblSubtitulo.Text = value ?? string.Empty; }
|
|
}
|
|
|
|
[Category("Aparência")]
|
|
public Color FillColor
|
|
{
|
|
get { return _fillColor; }
|
|
set { _fillColor = value; Invalidate(); }
|
|
}
|
|
|
|
[Category("Aparência")]
|
|
public Color BorderColor
|
|
{
|
|
get { return _borderColor; }
|
|
set { _borderColor = value; Invalidate(); }
|
|
}
|
|
|
|
[Category("Aparência")]
|
|
public Color AccentColor
|
|
{
|
|
get { return _accentColor; }
|
|
set { _accentColor = value; Invalidate(); }
|
|
}
|
|
|
|
[Category("Aparência")]
|
|
[DefaultValue(13)]
|
|
public int CornerRadius
|
|
{
|
|
get { return _cornerRadius; }
|
|
set
|
|
{
|
|
_cornerRadius = Math.Max(0, value);
|
|
UpdateRoundedRegion();
|
|
Invalidate();
|
|
}
|
|
}
|
|
|
|
public void SetStatus(string text, Color color)
|
|
{
|
|
Subtitulo = text;
|
|
_lblSubtitulo.ForeColor = color;
|
|
}
|
|
|
|
protected override void OnEnabledChanged(EventArgs e)
|
|
{
|
|
base.OnEnabledChanged(e);
|
|
Cursor = Enabled ? Cursors.Hand : Cursors.Default;
|
|
_lblTitulo.ForeColor = Enabled ? HmiTheme.Text : HmiTheme.Disabled;
|
|
_lblSubtitulo.ForeColor = Enabled ? HmiTheme.TextMuted : HmiTheme.Disabled;
|
|
Invalidate();
|
|
}
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
{
|
|
base.OnResize(e);
|
|
UpdateRoundedRegion();
|
|
}
|
|
|
|
protected override void OnMouseDown(MouseEventArgs e)
|
|
{
|
|
base.OnMouseDown(e);
|
|
if (!Enabled || e.Button != MouseButtons.Left)
|
|
return;
|
|
|
|
_pressed = true;
|
|
Invalidate();
|
|
}
|
|
|
|
protected override void OnMouseUp(MouseEventArgs e)
|
|
{
|
|
base.OnMouseUp(e);
|
|
if (!_pressed)
|
|
return;
|
|
|
|
_pressed = false;
|
|
Invalidate();
|
|
}
|
|
|
|
protected override void OnMouseLeave(EventArgs e)
|
|
{
|
|
base.OnMouseLeave(e);
|
|
if (!_pressed)
|
|
return;
|
|
|
|
_pressed = false;
|
|
Invalidate();
|
|
}
|
|
|
|
protected override void OnPaintBackground(PaintEventArgs e)
|
|
{
|
|
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
|
|
|
Color currentFill = _pressed
|
|
? HmiTheme.SurfacePressed
|
|
: _fillColor;
|
|
|
|
if (!Enabled)
|
|
currentFill = Color.FromArgb(22, 25, 28);
|
|
|
|
var bounds = new Rectangle(0, 0, Math.Max(1, Width - 1), Math.Max(1, Height - 1));
|
|
using (var path = HmiRoundedPanel.CreateRoundedPath(bounds, _cornerRadius))
|
|
using (var brush = new SolidBrush(currentFill))
|
|
{
|
|
e.Graphics.FillPath(brush, path);
|
|
}
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
|
|
|
Color border = Enabled ? _accentColor : HmiTheme.Disabled;
|
|
var bounds = new Rectangle(0, 0, Math.Max(1, Width - 1), Math.Max(1, Height - 1));
|
|
|
|
using (var path = HmiRoundedPanel.CreateRoundedPath(bounds, _cornerRadius))
|
|
using (var pen = new Pen(border, _pressed ? 2F : 1.25F))
|
|
{
|
|
e.Graphics.DrawPath(pen, path);
|
|
}
|
|
|
|
using (var pen = new Pen(Color.FromArgb(120, border), 3F))
|
|
{
|
|
pen.StartCap = LineCap.Round;
|
|
pen.EndCap = LineCap.Round;
|
|
e.Graphics.DrawLine(pen, 18, 8, Math.Max(18, Width - 18), 8);
|
|
}
|
|
|
|
base.OnPaint(e);
|
|
}
|
|
|
|
private void WireChild(Control child)
|
|
{
|
|
child.Click += Child_Click;
|
|
child.MouseDown += Child_MouseDown;
|
|
child.MouseUp += Child_MouseUp;
|
|
child.MouseLeave += Child_MouseLeave;
|
|
}
|
|
|
|
private void Child_Click(object sender, EventArgs e)
|
|
{
|
|
if (Enabled)
|
|
OnClick(e);
|
|
}
|
|
|
|
private void Child_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
OnMouseDown(e);
|
|
}
|
|
|
|
private void Child_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
OnMouseUp(e);
|
|
}
|
|
|
|
private void Child_MouseLeave(object sender, EventArgs e)
|
|
{
|
|
OnMouseLeave(e);
|
|
}
|
|
|
|
private void UpdateRoundedRegion()
|
|
{
|
|
if (Width <= 0 || Height <= 0)
|
|
return;
|
|
|
|
using (var path = HmiRoundedPanel.CreateRoundedPath(
|
|
new Rectangle(0, 0, Width, Height),
|
|
_cornerRadius))
|
|
{
|
|
if (Region != null)
|
|
Region.Dispose();
|
|
|
|
Region = new Region(path);
|
|
}
|
|
}
|
|
}
|
|
}
|