LevelOS-Core/UI/ControlesCustom.cs

151 lines
5.1 KiB
C#

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace UI
{
public class RoundTextBox : UserControl
{
private TextBox _textBox = null!;
public int Radius { get; set; } = 4;
public Color BorderColor { get; set; } = Color.LightGray;
public Color FocusColor { get; set; } = Color.Blue;
private bool _focused;
public bool ReadOnly
{
get => _textBox.ReadOnly;
set => _textBox.ReadOnly = value;
}
public char PasswordChar
{
get => _textBox.PasswordChar;
set => _textBox.PasswordChar = value;
}
// ── Adiciona isso aqui ──
public int SelectionStart
{
get => _textBox.SelectionStart;
set => _textBox.SelectionStart = value;
}
public new event KeyEventHandler? KeyDown
{
add => _textBox.KeyDown += value;
remove => _textBox.KeyDown -= value;
}
public bool Multiline
{
get => _textBox.Multiline;
set
{
_textBox.Multiline = value;
_textBox.ScrollBars = value ? ScrollBars.Vertical : ScrollBars.None;
AjustarTextBox();
}
}
public override string Text
{
get => _textBox.Text;
set => _textBox.Text = value;
}
public new Color BackColor
{
get => base.BackColor;
set { base.BackColor = value; if (_textBox != null) _textBox.BackColor = value; }
}
//public RoundTextBox()
//{
// DoubleBuffered = true;
// base.BackColor = Color.White;
// _textBox = new TextBox
// {
// BorderStyle = BorderStyle.None,
// Font = new Font("Segoe UI", 9f),
// Location = new Point(6, 6),
// Width = Width - 12,
// Height = Height - 12,
// BackColor = Color.White
// };
// _textBox.GotFocus += (s, e) => { _focused = true; Invalidate(); };
// _textBox.LostFocus += (s, e) => { _focused = false; Invalidate(); };
// Controls.Add(_textBox);
// SizeChanged += (s, e) => AjustarTextBox();
//}
public RoundTextBox()
{
DoubleBuffered = true;
base.BackColor = Color.White;
_textBox = new TextBox
{
BorderStyle = BorderStyle.None,
Font = new Font("Segoe UI", 9f),
Location = new Point(6, 6),
Width = Width - 12,
Height = Height - 12,
BackColor = Color.White
};
_textBox.GotFocus += (s, e) => { _focused = true; Invalidate(); };
_textBox.LostFocus += (s, e) => { _focused = false; Invalidate(); };
_textBox.TextChanged += (s, e) => OnTextChanged(e); // ← adiciona essa
_textBox.Leave += (s, e) => OnLeave(e); // ← e essa
Controls.Add(_textBox);
SizeChanged += (s, e) => AjustarTextBox();
}
// Ajusta largura e altura do TextBox interno conforme Multiline
private void AjustarTextBox()
{
_textBox.Width = Width - 12;
_textBox.Height = _textBox.Multiline ? Height - 12 : _textBox.PreferredHeight;
// Recentraliza verticalmente quando não é multiline
if (!_textBox.Multiline)
_textBox.Location = new Point(6, (Height - _textBox.PreferredHeight) / 2);
else
_textBox.Location = new Point(6, 6);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using var path = GetPath(new Rectangle(0, 0, Width - 1, Height - 1), Radius);
using var brush = new SolidBrush(BackColor);
e.Graphics.FillPath(brush, path);
using var pen = new Pen(_focused ? FocusColor : BorderColor, _focused ? 1.5f : 1f);
e.Graphics.DrawPath(pen, path);
}
private static GraphicsPath GetPath(Rectangle r, int rad)
{
var path = new GraphicsPath();
int d = rad * 2;
path.AddArc(r.X, r.Y, d, d, 180, 90);
path.AddArc(r.Right - d, r.Y, d, d, 270, 90);
path.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90);
path.AddArc(r.X, r.Bottom - d, d, d, 90, 90);
path.CloseFigure();
return path;
}
}
public class RoundButton : Button
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(int nL, int nT, int nR, int nB, int nW, int nH);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 8, 8));
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = 0;
}
}
}