RoundTextBox.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon
  11. {
  12. public partial class RoundTextBox : UserControl
  13. {
  14. TextBox box = new TextBox();
  15. public RoundTextBox()
  16. {
  17. InitializeComponent();
  18. try { UAS_MES_NEW.I18n.LanguageManager.Localize(this); } catch { }
  19. box.BorderStyle = BorderStyle.None;
  20. box.Width = this.Width;
  21. box.KeyDown += Box_KeyDown;
  22. this.Controls.Add(box);
  23. }
  24. private void Box_KeyDown(object sender, KeyEventArgs e)
  25. {
  26. UserKeyDown?.Invoke(sender, e);
  27. }
  28. public new char PassWordChar
  29. {
  30. get { return box.PasswordChar; }
  31. set
  32. {
  33. box.PasswordChar = value;
  34. }
  35. }
  36. [DefaultValue("")]
  37. public new string Text
  38. {
  39. get { return box.Text; }
  40. set
  41. {
  42. box.Text = value;
  43. }
  44. }
  45. public delegate void OnUserKeyDown(object sender, KeyEventArgs e);
  46. //定义事件
  47. public event OnUserKeyDown UserKeyDown;
  48. int r = 6;
  49. [Category("布局"), Description("倒角半径。")]
  50. public int R
  51. {
  52. get { return r; }
  53. set
  54. {
  55. r = value;
  56. }
  57. }
  58. private void RoundTextBox_Paint(object sender, PaintEventArgs e)
  59. {
  60. int w = R * 2;
  61. base.OnPaintBackground(e);
  62. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  63. using (GraphicsPath path = new GraphicsPath())
  64. {
  65. path.AddArc(0, 0, w, w, 180, 90);
  66. path.AddArc(this.Width - w - 1, 0, w, w, -90, 90);
  67. path.AddArc(this.Width - w - 1, this.Height - w - 1, w, w, 0, 90);
  68. path.AddArc(0, this.Height - w - 1, w, w, 90, 90);
  69. path.CloseFigure();
  70. e.Graphics.FillPath(Brushes.White, path);
  71. using (Pen pen = new Pen(Color.Gray))
  72. {
  73. e.Graphics.DrawPath(pen, path);
  74. }
  75. }
  76. }
  77. private void RoundTextBox_Resize(object sender, EventArgs e)
  78. {
  79. box.Left = 5;
  80. box.Top = 3;
  81. }
  82. }
  83. }