LevelOS-Core/CPM/Datetimepicker/LV-DATETIMEPICKER.cs

165 lines
5.0 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
namespace CPM.Datetimepicker
{
[ToolboxItem(true)]
public class LV_DATETIMEPICKER : DateTimePicker
{
// Aparência
private Color skinColor = Color.MediumSlateBlue;
private Color textColor = Color.White;
private Color borderColor = Color.PaleVioletRed;
private int borderSize = 0;
// Auxiliares
private bool droppedDown = false;
private Image calendarIcon;
private RectangleF iconButtonArea;
private const int calendarIconWidth = 34;
private const int arrowIconWidth = 17;
public LV_DATETIMEPICKER()
{
SetStyle(ControlStyles.UserPaint, true);
MinimumSize = new Size(0, 35);
Font = new Font(Font.Name, 9.5F);
calendarIcon =
LoadEmbeddedImage("Imagens.calendarWhite.png")
?? SystemIcons.Information.ToBitmap();
}
// 🔹 Propriedades públicas
public Color SkinColor
{
get => skinColor;
set
{
skinColor = value;
// Troca automática de ícone conforme o fundo
calendarIcon =
skinColor.GetBrightness() >= 0.6F
? LoadEmbeddedImage("Imagens.calendarDark.png")
: LoadEmbeddedImage("Imagens.calendarWhite.png")
?? SystemIcons.Information.ToBitmap();
Invalidate();
}
}
public Color TextColor
{
get => textColor;
set { textColor = value; Invalidate(); }
}
public Color BorderColor
{
get => borderColor;
set { borderColor = value; Invalidate(); }
}
public int BorderSize
{
get => borderSize;
set { borderSize = value; Invalidate(); }
}
// 🔹 Eventos
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
droppedDown = true;
Invalidate();
}
protected override void OnCloseUp(EventArgs e)
{
base.OnCloseUp(e);
droppedDown = false;
Invalidate();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
e.Handled = true;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
using var penBorder = new Pen(borderColor, borderSize) { Alignment = PenAlignment.Inset };
using var skinBrush = new SolidBrush(skinColor);
using var textBrush = new SolidBrush(textColor);
using var openBrush = new SolidBrush(Color.FromArgb(50, 64, 64, 64));
using var format = new StringFormat { LineAlignment = StringAlignment.Center };
RectangleF client = new RectangleF(0, 0, Width - 0.5F, Height - 0.5F);
RectangleF iconArea = new RectangleF(client.Width - calendarIconWidth, 0, calendarIconWidth, client.Height);
// Fundo
g.FillRectangle(skinBrush, client);
// Texto
g.DrawString(" " + Text, Font, textBrush, client, format);
// Ícone
if (calendarIcon != null)
{
g.DrawImage(calendarIcon,
Width - calendarIcon.Width - 9,
(Height - calendarIcon.Height) / 2);
}
// Destaque quando aberto
if (droppedDown)
g.FillRectangle(openBrush, iconArea);
// Borda
if (borderSize > 0)
g.DrawRectangle(penBorder, client.X, client.Y, client.Width, client.Height);
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
int iconWidth = GetIconButtonWidth();
iconButtonArea = new RectangleF(Width - iconWidth, 0, iconWidth, Height);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Cursor = iconButtonArea.Contains(e.Location) ? Cursors.Hand : Cursors.Default;
}
// 🔹 Métodos privados
private int GetIconButtonWidth()
{
int textWidth = TextRenderer.MeasureText(Text, Font).Width;
return textWidth <= Width - (calendarIconWidth + 20)
? calendarIconWidth
: arrowIconWidth;
}
private Image LoadEmbeddedImage(string relativePath)
{
var asm = Assembly.GetExecutingAssembly();
var resourceName = $"{asm.GetName().Name}.{relativePath}";
using var stream = asm.GetManifestResourceStream(resourceName);
return stream != null ? Image.FromStream(stream) : null;
}
}
}