GTextBox.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1.  using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web.UI.Design;
  11. using System.Windows.Forms;
  12. using System.Windows.Forms.Design;
  13. using ControlDesigner = System.Windows.Forms.Design.ControlDesigner;
  14. namespace ImpulseVision
  15. {
  16. [Designer(typeof(ControlDesignerEx))] // ControlDesignerEx Добавляем для ограничения изменения размеров
  17. [DefaultProperty("TextPreview")]
  18. public class EgoldsGoogleTextBox : Control
  19. {
  20. #region -- Свойства --
  21. public string TextPreview { get; set; } = "Input text";
  22. private Font fontTextPreview = new Font("Arial", 8, FontStyle.Bold);
  23. public Font FontTextPreview
  24. {
  25. get => fontTextPreview;
  26. set
  27. {
  28. // Ограничение, чтобы размер шрифта заголовка нельзя было установить больше,
  29. // чем размер основного шрифта
  30. if (value.Size >= Font.Size)
  31. return;
  32. fontTextPreview = value;
  33. }
  34. }
  35. public Color BorderColor { get; set; } = FlatColors.Blue;
  36. public Color BorderColorNotActive { get; set; } = FlatColors.GrayDark;
  37. public string TextInput
  38. {
  39. get => tbInput.Text;
  40. set
  41. {
  42. tbInput.Text = value;
  43. if (!tbInput.Focused)
  44. TextPreviewAction(TextInput.Length > 0);
  45. }
  46. }
  47. public bool UseSystemPasswordChar
  48. {
  49. get => tbInput.UseSystemPasswordChar;
  50. set => tbInput.UseSystemPasswordChar = value;
  51. }
  52. public new string Text
  53. {
  54. get => tbInput.Text;
  55. set
  56. {
  57. tbInput.Text = value;
  58. if (!tbInput.Focused)
  59. TextPreviewAction(TextInput.Length > 0);
  60. }
  61. }
  62. public int SelectionStart
  63. {
  64. get => tbInput.SelectionStart;
  65. set => tbInput.SelectionStart = value;
  66. }
  67. public int TextLength
  68. {
  69. get => tbInput.TextLength;
  70. }
  71. #endregion
  72. #region -- События / Events --
  73. [Browsable(true)]
  74. public new event EventHandler TextChanged
  75. {
  76. add { tbInput.TextChanged += value; }
  77. remove { tbInput.TextChanged -= value; }
  78. }
  79. [Browsable(true)]
  80. public new event KeyPressEventHandler KeyPress
  81. {
  82. add { tbInput.KeyPress += value; }
  83. remove { tbInput.KeyPress -= value; }
  84. }
  85. #endregion
  86. #region -- Переменные --
  87. StringFormat SF = new StringFormat();
  88. int TopBorderOffset = 0;
  89. TextBox tbInput = new TextBox();
  90. Animation LocationTextPreviewAnim = new Animation();
  91. Animation FontSizeTextPreviewAnim = new Animation();
  92. #endregion
  93. public EgoldsGoogleTextBox()
  94. {
  95. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
  96. DoubleBuffered = true;
  97. Size = new Size(150, 40);
  98. Font = new Font("Arial", 11.25F, FontStyle.Regular);
  99. ForeColor = Color.Black;
  100. BackColor = Color.White;
  101. Cursor = Cursors.IBeam;
  102. SF.Alignment = StringAlignment.Center;
  103. SF.LineAlignment = StringAlignment.Center;
  104. AdjustTextBoxInput();
  105. Controls.Add(tbInput);
  106. LocationTextPreviewAnim.Value = tbInput.Location.Y;
  107. FontSizeTextPreviewAnim.Value = Font.Size;
  108. }
  109. protected override void OnCreateControl()
  110. {
  111. base.OnCreateControl();
  112. TextPreviewAction(TextInput.Length > 0);
  113. }
  114. private void AdjustTextBoxInput()
  115. {
  116. tbInput = new TextBox();
  117. tbInput.Name = "InputBox";
  118. tbInput.BorderStyle = BorderStyle.None;
  119. tbInput.BackColor = BackColor;
  120. tbInput.ForeColor = ForeColor;
  121. tbInput.Font = Font;
  122. tbInput.Visible = false;
  123. int offset = TextRenderer.MeasureText(TextPreview, FontTextPreview).Height / 2;
  124. tbInput.Location = new Point(5, Height / 2 - offset);
  125. tbInput.Size = new Size(Width - 10, tbInput.Height);
  126. tbInput.LostFocus += TbInput_LostFocus;
  127. tbInput.GotFocus += TbInput_GotFocus;
  128. }
  129. private void TbInput_GotFocus(object sender, EventArgs e)
  130. {
  131. TextPreviewAction(true);
  132. }
  133. private void TbInput_LostFocus(object sender, EventArgs e)
  134. {
  135. TextPreviewAction(false);
  136. }
  137. #region -- Обновление свойств tbInput --
  138. protected override void OnBackColorChanged(EventArgs e)
  139. {
  140. base.OnBackColorChanged(e);
  141. tbInput.BackColor = BackColor;
  142. }
  143. protected override void OnForeColorChanged(EventArgs e)
  144. {
  145. base.OnForeColorChanged(e);
  146. tbInput.ForeColor = ForeColor;
  147. }
  148. protected override void OnFontChanged(EventArgs e)
  149. {
  150. base.OnFontChanged(e);
  151. tbInput.Font = Font;
  152. }
  153. protected override void OnSizeChanged(EventArgs e)
  154. {
  155. base.OnSizeChanged(e);
  156. tbInput.Size = new Size(Width - 10, tbInput.Height);
  157. }
  158. #endregion
  159. protected override void OnPaint(PaintEventArgs e)
  160. {
  161. base.OnPaint(e);
  162. Graphics graph = e.Graphics;
  163. graph.SmoothingMode = SmoothingMode.HighQuality;
  164. graph.Clear(Parent.BackColor);
  165. TopBorderOffset = graph.MeasureString(TextPreview, FontTextPreview).ToSize().Height / 2;
  166. Font FontTextPreviewActual = new Font(FontTextPreview.FontFamily, FontSizeTextPreviewAnim.Value, FontTextPreview.Style);
  167. if (!tbInput.Visible && FontTextPreviewActual.Size <= FontTextPreview.Size)
  168. {
  169. tbInput.Visible = true;
  170. tbInput.Focus();
  171. }
  172. else if (tbInput.Visible && FontTextPreviewActual.Size > FontTextPreview.Size)
  173. {
  174. tbInput.Visible = false;
  175. }
  176. Rectangle rectBase = new Rectangle(0, TopBorderOffset, Width - 1, Height - 1 - TopBorderOffset);
  177. Size TextPreviewRectSize = graph.MeasureString(TextPreview, FontTextPreviewActual).ToSize();
  178. Rectangle rectTextPreview = new Rectangle(5, (int)LocationTextPreviewAnim.Value, TextPreviewRectSize.Width + 3, TextPreviewRectSize.Height);
  179. // Обводка
  180. graph.DrawRectangle(new Pen(tbInput.Text.Length > 0 || tbInput.Focused ?
  181. BorderColor : BorderColorNotActive), rectBase);
  182. // Заголовок/Описание
  183. graph.DrawRectangle(new Pen(Parent.BackColor), rectTextPreview);
  184. graph.FillRectangle(new SolidBrush(Parent.BackColor), rectTextPreview);
  185. // Цвет внутри
  186. graph.FillRectangle(new SolidBrush(BackColor), rectBase);
  187. graph.DrawString(TextPreview, FontTextPreviewActual,
  188. new SolidBrush(tbInput.Text.Length > 0 || tbInput.Focused ?
  189. BorderColor : BorderColorNotActive), rectTextPreview, SF);
  190. }
  191. private void TextPreviewAction(bool OnTop)
  192. {
  193. if (OnTop)
  194. {
  195. if (tbInput.Visible == false)
  196. {
  197. LocationTextPreviewAnim = new Animation("TextPreviewLocationY" + Handle, Invalidate, LocationTextPreviewAnim.Value, 0);
  198. FontSizeTextPreviewAnim = new Animation("TextPreviewFontSize" + Handle, Invalidate, FontSizeTextPreviewAnim.Value, FontTextPreview.Size);
  199. }
  200. else
  201. {
  202. tbInput.Focus();
  203. return;
  204. }
  205. }
  206. else
  207. {
  208. if (TextInput.Length == 0)
  209. {
  210. LocationTextPreviewAnim = new Animation("TextPreviewLocationY" + Handle, Invalidate, LocationTextPreviewAnim.Value, tbInput.Location.Y);
  211. FontSizeTextPreviewAnim = new Animation("TextPreviewFontSize" + Handle, Invalidate, FontSizeTextPreviewAnim.Value, Font.Size);
  212. }
  213. else
  214. {
  215. return;
  216. }
  217. }
  218. LocationTextPreviewAnim.StepDivider = 4;
  219. FontSizeTextPreviewAnim.StepDivider = 4;
  220. Animator.Request(LocationTextPreviewAnim, true);
  221. Animator.Request(FontSizeTextPreviewAnim, true);
  222. }
  223. protected override void OnMouseClick(MouseEventArgs e)
  224. {
  225. base.OnMouseClick(e);
  226. TextPreviewAction(true);
  227. }
  228. /// <summary>
  229. /// В этом классе переопределяем SelectionRules, и даем возможность только изменять ширину и перемещать объект
  230. /// </summary>
  231. class ControlDesignerEx : ControlDesigner
  232. {
  233. public override SelectionRules SelectionRules
  234. {
  235. get
  236. {
  237. SelectionRules sr = SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable | SelectionRules.Visible;
  238. return sr;
  239. }
  240. }
  241. }
  242. }
  243. }