You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.9 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. namespace Customize.Controls
  12. {
  13. public class ExtendedTextPanel : Panel
  14. {
  15. private const int WS_EX_TRANSPARENT = 0x20;
  16. private bool isClicked = false;
  17. public Point mouseDownPoint; // 记录鼠标点击坐标
  18. public ExtendedTextPanel()
  19. {
  20. SetStyle(ControlStyles.Opaque, true);
  21. this.extendedPanel.BoarderColor = Color.Red;
  22. this.extendedPanel.BoarderSize = 2;
  23. this.extendedPanel.Location = new Point(0, 0);
  24. this.extendedPanel.Size = new Size(this.Width, this.Height);
  25. this.transTextBox.ForeColor = Color.Red;
  26. this.Controls.Add(this.extendedPanel);
  27. this.Resize += ExtendedTextPanel_Resize;
  28. }
  29. private void ExtendedTextPanel_Resize(object sender, EventArgs e)
  30. {
  31. this.extendedPanel.Location = new Point(0, 0);
  32. this.extendedPanel.Size = new Size(this.Width, this.Height);
  33. }
  34. private int opacity = 50;
  35. [DefaultValue(50)]
  36. public int Opacity
  37. {
  38. get
  39. {
  40. return this.opacity;
  41. }
  42. set
  43. {
  44. if (value < 0 || value > 100)
  45. throw new ArgumentException("value must be between 0 and 100");
  46. this.opacity = value;
  47. }
  48. }
  49. private string mode = "default";
  50. [DefaultValue("default")]
  51. public string Mode
  52. {
  53. get
  54. {
  55. return this.mode;
  56. }
  57. set
  58. {
  59. this.mode = value;
  60. }
  61. }
  62. private TransTextBox transTextBox = new TransTextBox();
  63. public TransTextBox TransTextBox
  64. {
  65. get
  66. {
  67. return this.transTextBox;
  68. }
  69. set
  70. {
  71. this.transTextBox = value;
  72. }
  73. }
  74. private BorderPanel extendedPanel = new BorderPanel();
  75. public BorderPanel ExtendedPanel
  76. {
  77. get
  78. {
  79. return this.extendedPanel;
  80. }
  81. set
  82. {
  83. this.extendedPanel = value;
  84. }
  85. }
  86. protected override CreateParams CreateParams
  87. {
  88. get
  89. {
  90. CreateParams cp = base.CreateParams;
  91. cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
  92. return cp;
  93. }
  94. }
  95. /**
  96. *
  97. */
  98. protected override void OnPaint(PaintEventArgs e)
  99. {
  100. Debug.WriteLine("=====ExtendedTextPanel");
  101. using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 255 / 100, this.BackColor)))
  102. {
  103. Pen p = new Pen(Color.Red, 2);
  104. if (this.mode == "text")
  105. {
  106. p.DashStyle = DashStyle.Dot;
  107. }
  108. e.Graphics.FillRectangle(brush, this.ClientRectangle);
  109. e.Graphics.Dispose();
  110. }
  111. base.OnPaint(e);
  112. }
  113. /**
  114. *
  115. */
  116. public void showTransTextBox()
  117. {
  118. this.transTextBox.Location = new Point(2, 2);
  119. this.transTextBox.AutoSize = true;
  120. this.transTextBox.Multiline = true;
  121. this.transTextBox.BorderStyle = BorderStyle.None;
  122. this.transTextBox.Size = new Size(this.Width - 4, this.Height - 4);
  123. this.transTextBox.ForeColor = Color.Red;
  124. this.extendedPanel.ImeMode = ImeMode.NoControl;
  125. this.extendedPanel.Controls.Add(this.transTextBox);
  126. this.transTextBox.LostFocus += new EventHandler(txt_LostFocus);
  127. this.transTextBox.GotFocus += new EventHandler(txt_GotFocus);
  128. this.transTextBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txt_MouseDown);
  129. this.transTextBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.txt_MouseMove);
  130. this.transTextBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.txt_MouseUp);
  131. }
  132. private void txt_LostFocus(object sender, EventArgs e)
  133. {
  134. Debug.WriteLine("txt_LostFocus");
  135. this.clearBoder();
  136. //Invalidate();
  137. }
  138. private void txt_GotFocus(object sender, EventArgs e)
  139. {
  140. Debug.WriteLine("txt_GotFocus");
  141. this.showBoder();
  142. ///Invalidate();
  143. }
  144. public void clearBoder()
  145. {
  146. this.extendedPanel.BoarderSize = 0;
  147. this.extendedPanel.Refresh();
  148. }
  149. public void showBoder()
  150. {
  151. this.extendedPanel.BoarderSize = 2;
  152. this.extendedPanel.Refresh();
  153. }
  154. private void txt_MouseDown(object sender, MouseEventArgs e)
  155. {
  156. if (e.Button == MouseButtons.Left)
  157. {
  158. Cursor.Current = Cursors.SizeAll;
  159. mouseDownPoint.X = Cursor.Position.X;
  160. mouseDownPoint.Y = Cursor.Position.Y;
  161. isClicked = true;
  162. }
  163. }
  164. private void txt_MouseMove(object sender, MouseEventArgs e)
  165. {
  166. if (isClicked)
  167. {
  168. this.Left = this.Left + (Cursor.Position.X - mouseDownPoint.X);
  169. this.Top = this.Top + (Cursor.Position.Y - mouseDownPoint.Y);
  170. mouseDownPoint.X = Cursor.Position.X;
  171. mouseDownPoint.Y = Cursor.Position.Y;
  172. }
  173. }
  174. private void txt_MouseUp(object sender, MouseEventArgs e)
  175. {
  176. isClicked = false;
  177. }
  178. }
  179. }