Dreamine.UI.WinForms 1.0.1
Dreamine.UI.WinForms 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreaminePasswordBox.cs
이 파일의 문서화 페이지로 가기
1using System.Drawing;
2using System.Runtime.InteropServices;
4
6
15public class DreaminePasswordBox : UserControl
16{
25 private const int EM_SETCUEBANNER = 0x1501;
74 [DllImport("user32.dll", CharSet = CharSet.Unicode)]
75 private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);
76
85 private readonly TextBox _inner;
94 private bool _isFocused;
95
96 // ── Properties ────────────────────────────────────────
105 public string Password
106 {
107 get => _inner.Text;
108 set => _inner.Text = value;
109 }
110
119 private string _hint = string.Empty;
128 public string Hint
129 {
130 get => _hint;
131 set
132 {
133 _hint = value;
134 if (_inner.IsHandleCreated)
135 SendMessage(_inner.Handle, EM_SETCUEBANNER, (IntPtr)1, _hint);
136 }
137 }
138
147 public override Color ForeColor
148 {
149 get => base.ForeColor;
150 set { base.ForeColor = value; if (_inner != null) _inner.ForeColor = value; }
151 }
152
161 public new Font Font
162 {
163 get => base.Font;
164 set { base.Font = value; if (_inner != null) _inner.Font = value; }
165 }
166
175 public event EventHandler? PasswordChanged;
176
177 // ── Constructor ───────────────────────────────────────
187 {
188 // _inner must be created first — SetStyle and property setters below
189 // can trigger OnLayout / ForeColor / Font overrides before the field is set.
190 _inner = new TextBox
191 {
192 BorderStyle = BorderStyle.None,
193 BackColor = DreamineTheme.InputBackground,
195 Font = new Font("Segoe UI", 10f, FontStyle.Regular, GraphicsUnit.Point),
196 UseSystemPasswordChar = true,
197 };
198
199 _inner.GotFocus += (_, _) => { _isFocused = true; Invalidate(); };
200 _inner.LostFocus += (_, _) => { _isFocused = false; Invalidate(); };
201 _inner.TextChanged += (s, e) => PasswordChanged?.Invoke(this, e);
202 _inner.HandleCreated += (_, _) =>
203 {
204 if (!string.IsNullOrEmpty(_hint))
205 SendMessage(_inner.Handle, EM_SETCUEBANNER, (IntPtr)1, _hint);
206 };
207
208 SetStyle(
209 ControlStyles.AllPaintingInWmPaint |
210 ControlStyles.UserPaint |
211 ControlStyles.DoubleBuffer |
212 ControlStyles.ResizeRedraw, true);
213
214 BackColor = DreamineTheme.InputBackground;
216 Font = _inner.Font;
217 Height = 36;
218
219 Controls.Add(_inner);
220 }
221
238 protected override void OnLayout(LayoutEventArgs e)
239 {
240 base.OnLayout(e);
241 if (_inner == null) return;
242 _inner.SetBounds(6, (Height - _inner.PreferredHeight) / 2,
243 Width - 12, _inner.PreferredHeight);
244 }
245
262 protected override void OnPaint(PaintEventArgs e)
263 {
264 var g = e.Graphics;
265 var rect = new Rectangle(0, 0, Width - 1, Height - 1);
266 using var bgBrush = new SolidBrush(BackColor);
268 using var pen = new Pen(borderColor, 1.5f);
270 }
271
280 protected override Size DefaultSize => new(220, 36);
281}
static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam)
static void FillRoundedRect(Graphics g, Brush fill, Pen? border, Rectangle rect, float radius)
static readonly Color BorderFocus
static readonly Color TextPrimary
static readonly Color BorderNormal
static readonly Color InputBackground