RoundTextBox.cs 2.3 KB

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