RoundTextBox.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. box.BorderStyle = BorderStyle.None;
  19. box.Width = this.Width;
  20. box.KeyDown += Box_KeyDown;
  21. this.Controls.Add(box);
  22. }
  23. private void Box_KeyDown(object sender, KeyEventArgs e)
  24. {
  25. UserKeyDown?.Invoke(sender, e);
  26. }
  27. public new char PassWordChar
  28. {
  29. get { return box.PasswordChar; }
  30. set
  31. {
  32. box.PasswordChar = value;
  33. }
  34. }
  35. [DefaultValue("")]
  36. public new string Text
  37. {
  38. get { return box.Text; }
  39. set
  40. {
  41. box.Text = value;
  42. }
  43. }
  44. public delegate void OnUserKeyDown(object sender, KeyEventArgs e);
  45. //定义事件
  46. public event OnUserKeyDown UserKeyDown;
  47. int r = 6;
  48. [Category("布局"), Description("倒角半径。")]
  49. public int R
  50. {
  51. get { return r; }
  52. set
  53. {
  54. r = value;
  55. }
  56. }
  57. private void RoundTextBox_Paint(object sender, PaintEventArgs e)
  58. {
  59. int w = R * 2;
  60. base.OnPaintBackground(e);
  61. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  62. using (GraphicsPath path = new GraphicsPath())
  63. {
  64. path.AddArc(0, 0, w, w, 180, 90);
  65. path.AddArc(this.Width - w - 1, 0, w, w, -90, 90);
  66. path.AddArc(this.Width - w - 1, this.Height - w - 1, w, w, 0, 90);
  67. path.AddArc(0, this.Height - w - 1, w, w, 90, 90);
  68. path.CloseFigure();
  69. e.Graphics.FillPath(Brushes.White, path);
  70. using (Pen pen = new Pen(Color.Gray))
  71. {
  72. e.Graphics.DrawPath(pen, path);
  73. }
  74. }
  75. }
  76. private void RoundTextBox_Resize(object sender, EventArgs e)
  77. {
  78. box.Left = 5;
  79. box.Top = 3;
  80. }
  81. }
  82. }