SkinTextBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using LYFZ.ComponentLibrary.Properties;
  2. using LYFZ.OtherExpansion.SkinClass;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. namespace LYFZ.OtherExpansion.SkinControl
  9. {
  10. public class SkinTextBox : UserControl
  11. {
  12. private bool flag;
  13. private Cursor _cursor = Cursors.IBeam;
  14. private ControlState _mouseState;
  15. private ControlState _iconMouseState;
  16. private Bitmap mouseback;
  17. private Bitmap normlback;
  18. private bool _iconIsButton;
  19. private Image _icon;
  20. private IContainer components = null;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public WaterTextBox BaseText;
  25. public event EventHandler IconClick;
  26. [Category("Skin"), Description("悬浮时背景框。")]
  27. public Bitmap MouseBack
  28. {
  29. get
  30. {
  31. return this.mouseback;
  32. }
  33. set
  34. {
  35. if (this.mouseback != value)
  36. {
  37. this.mouseback = value;
  38. base.Invalidate();
  39. }
  40. }
  41. }
  42. [Category("Skin"), Description("正常状态时背景框。")]
  43. public Bitmap NormlBack
  44. {
  45. get
  46. {
  47. return this.normlback;
  48. }
  49. set
  50. {
  51. if (this.normlback != value)
  52. {
  53. this.normlback = value;
  54. base.Invalidate();
  55. }
  56. }
  57. }
  58. [Category("Skin"), Description("指定可以在编辑控件中输入的最大字符数。")]
  59. public int MaxLength
  60. {
  61. get
  62. {
  63. return this.BaseText.MaxLength;
  64. }
  65. set
  66. {
  67. this.BaseText.MaxLength = value;
  68. }
  69. }
  70. [Category("Skin"), Description("控件编辑控件的文本是否能够跨越多行。")]
  71. public bool Multiline
  72. {
  73. get
  74. {
  75. return this.BaseText.Multiline;
  76. }
  77. set
  78. {
  79. this.BaseText.Multiline = value;
  80. }
  81. }
  82. [Category("Skin"), Description("指示将为单行编辑控件的密码输入显示的字符。")]
  83. public char IsPasswordChat
  84. {
  85. get
  86. {
  87. return this.BaseText.PasswordChar;
  88. }
  89. set
  90. {
  91. this.BaseText.PasswordChar = value;
  92. }
  93. }
  94. [Category("Skin"), Description("控制能否更改编辑控件中的文本。")]
  95. public bool ReadOnly
  96. {
  97. get
  98. {
  99. return this.BaseText.ReadOnly;
  100. }
  101. set
  102. {
  103. this.BaseText.ReadOnly = value;
  104. }
  105. }
  106. [Category("Skin"), Description("指示编辑控件中的文本是否以默认的密码字符显示。")]
  107. public bool IsSystemPasswordChar
  108. {
  109. get
  110. {
  111. return this.BaseText.UseSystemPasswordChar;
  112. }
  113. set
  114. {
  115. this.BaseText.UseSystemPasswordChar = value;
  116. }
  117. }
  118. [Category("Skin"), Description("指示多行编辑控件是否自动换行。")]
  119. public bool WordWrap
  120. {
  121. get
  122. {
  123. return this.BaseText.WordWrap;
  124. }
  125. set
  126. {
  127. this.BaseText.WordWrap = value;
  128. }
  129. }
  130. [Category("Skin"), Description("用于显示控件中文本的字体。")]
  131. public override Font Font
  132. {
  133. get
  134. {
  135. return this.BaseText.Font;
  136. }
  137. set
  138. {
  139. this.BaseText.Font = value;
  140. base.Font = value;
  141. }
  142. }
  143. [Category("Skin"), Description("此组件的前景色,用于显示文本。")]
  144. public override Color ForeColor
  145. {
  146. get
  147. {
  148. return this.BaseText.ForeColor;
  149. }
  150. set
  151. {
  152. this.BaseText.ForeColor = value;
  153. base.ForeColor = value;
  154. }
  155. }
  156. [Category("Skin"), Description("多行编辑中的文本行,作为字符串值的数组。")]
  157. public string[] Lines
  158. {
  159. get
  160. {
  161. return this.BaseText.Lines;
  162. }
  163. set
  164. {
  165. this.BaseText.Lines = value;
  166. }
  167. }
  168. [Category("Skin"), Description("指示对于多行编辑控件,将为此控件显示哪些滚动条。")]
  169. public ScrollBars ScrollBars
  170. {
  171. get
  172. {
  173. return this.BaseText.ScrollBars;
  174. }
  175. set
  176. {
  177. this.BaseText.ScrollBars = value;
  178. }
  179. }
  180. [Category("Skin"), Description("指示应该如何对齐编辑控件的文本。")]
  181. public HorizontalAlignment TextAlign
  182. {
  183. get
  184. {
  185. return this.BaseText.TextAlign;
  186. }
  187. set
  188. {
  189. this.BaseText.TextAlign = value;
  190. }
  191. }
  192. [Category("Skin"), Description("文本框的图标")]
  193. public Image Icon
  194. {
  195. get
  196. {
  197. return this._icon;
  198. }
  199. set
  200. {
  201. if (this._icon != value)
  202. {
  203. this._icon = value;
  204. base.Invalidate(this.IconRect);
  205. this.PositionTextBox();
  206. }
  207. }
  208. }
  209. [Category("Skin"), Description("文本框的图标是否是按钮")]
  210. public bool IconIsButton
  211. {
  212. get
  213. {
  214. return this._iconIsButton;
  215. }
  216. set
  217. {
  218. this._iconIsButton = value;
  219. }
  220. }
  221. [Category("Skin"), Description("水印文字")]
  222. public string WaterText
  223. {
  224. get
  225. {
  226. return this.BaseText.WaterText;
  227. }
  228. set
  229. {
  230. this.BaseText.WaterText = value;
  231. }
  232. }
  233. [Category("Skin"), Description("水印颜色")]
  234. public Color WaterColor
  235. {
  236. get
  237. {
  238. return this.BaseText.WaterColor;
  239. }
  240. set
  241. {
  242. this.BaseText.WaterColor = value;
  243. }
  244. }
  245. [Browsable(true), Category("Skin"), Description("与控件关联的文本")]
  246. public override string Text
  247. {
  248. get
  249. {
  250. return this.BaseText.Text;
  251. }
  252. set
  253. {
  254. this.BaseText.Text = value;
  255. }
  256. }
  257. public override Cursor Cursor
  258. {
  259. get
  260. {
  261. return this._cursor;
  262. }
  263. set
  264. {
  265. this._cursor = value;
  266. }
  267. }
  268. protected ControlState MouseState
  269. {
  270. get
  271. {
  272. return this._mouseState;
  273. }
  274. set
  275. {
  276. this._mouseState = value;
  277. base.Invalidate();
  278. }
  279. }
  280. protected ControlState IconMouseState
  281. {
  282. get
  283. {
  284. return this._iconMouseState;
  285. }
  286. set
  287. {
  288. this._iconMouseState = value;
  289. base.Invalidate();
  290. }
  291. }
  292. protected Rectangle IconRect
  293. {
  294. get
  295. {
  296. return new Rectangle(base.Width - 23, 3, 20, 22);
  297. }
  298. }
  299. public SkinTextBox()
  300. {
  301. this.InitializeComponent();
  302. this.Init();
  303. this.InitEvents();
  304. this.BackColor = Color.Transparent;
  305. }
  306. public void Init()
  307. {
  308. base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
  309. base.UpdateStyles();
  310. }
  311. private void InitEvents()
  312. {
  313. this.BaseText.MouseEnter += new EventHandler(this.BaseText_MouseEnter);
  314. this.BaseText.MouseLeave += new EventHandler(this.BaseText_MouseLeave);
  315. this.BaseText.KeyDown += new KeyEventHandler(this.BaseText_KeyDown);
  316. this.BaseText.KeyPress += new KeyPressEventHandler(this.BaseText_KeyPress);
  317. this.BaseText.KeyUp += new KeyEventHandler(this.BaseText_KeyUp);
  318. }
  319. protected virtual void PositionTextBox()
  320. {
  321. if (this._icon != null && !this.flag)
  322. {
  323. base.Padding = new Padding(base.Padding.Left, base.Padding.Top, base.Padding.Right + 23, base.Padding.Bottom);
  324. this.flag = true;
  325. return;
  326. }
  327. if (this._icon == null && this.flag)
  328. {
  329. base.Padding = new Padding(base.Padding.Left, base.Padding.Top, base.Padding.Right - 23, base.Padding.Bottom);
  330. this.flag = false;
  331. }
  332. }
  333. public void AppendText(string str)
  334. {
  335. this.BaseText.AppendText(str);
  336. }
  337. private void OnIconClick()
  338. {
  339. if (this.IconClick != null)
  340. {
  341. this.IconClick(this, EventArgs.Empty);
  342. }
  343. }
  344. protected void BaseText_MouseLeave(object sender, EventArgs e)
  345. {
  346. this.MouseState = ControlState.Normal;
  347. }
  348. private void BaseText_MouseEnter(object sender, EventArgs e)
  349. {
  350. this.MouseState = ControlState.Hover;
  351. }
  352. private void BaseText_KeyUp(object sender, KeyEventArgs e)
  353. {
  354. this.OnKeyUp(e);
  355. }
  356. private void BaseText_KeyPress(object sender, KeyPressEventArgs e)
  357. {
  358. this.OnKeyPress(e);
  359. }
  360. private void BaseText_KeyDown(object sender, KeyEventArgs e)
  361. {
  362. this.OnKeyDown(e);
  363. }
  364. protected override void OnSizeChanged(EventArgs e)
  365. {
  366. base.OnSizeChanged(e);
  367. if (base.Height > 28)
  368. {
  369. this.BaseText.Multiline = true;
  370. return;
  371. }
  372. this.BaseText.Multiline = false;
  373. }
  374. protected override void OnPaint(PaintEventArgs e)
  375. {
  376. base.OnPaint(e);
  377. Graphics g = e.Graphics;
  378. g.SmoothingMode = SmoothingMode.AntiAlias;
  379. Bitmap mouse = (this.MouseBack == null) ? Resources.frameBorderEffect_mouseDownDraw : this.MouseBack;
  380. Bitmap norml = (this.NormlBack == null) ? Resources.frameBorderEffect_normalDraw : this.NormlBack;
  381. Bitmap btm = (this._mouseState == ControlState.Hover) ? mouse : norml;
  382. if (btm != null)
  383. {
  384. DrawHelper.RendererBackground(g, base.ClientRectangle, btm, true);
  385. }
  386. if (this._icon != null)
  387. {
  388. Rectangle iconRect = this.IconRect;
  389. if (this._iconMouseState == ControlState.Pressed)
  390. {
  391. iconRect.X++;
  392. iconRect.Y++;
  393. }
  394. g.DrawImage(this._icon, iconRect, 0, 0, this._icon.Width, this._icon.Height, GraphicsUnit.Pixel);
  395. }
  396. }
  397. protected override void OnMouseEnter(EventArgs e)
  398. {
  399. base.OnMouseEnter(e);
  400. this.MouseState = ControlState.Hover;
  401. }
  402. protected override void OnMouseMove(MouseEventArgs e)
  403. {
  404. base.OnMouseMove(e);
  405. if (this._icon == null || !this.IconRect.Contains(e.Location))
  406. {
  407. this.Cursor = Cursors.IBeam;
  408. return;
  409. }
  410. if (this._iconIsButton)
  411. {
  412. this.Cursor = Cursors.Hand;
  413. return;
  414. }
  415. this.Cursor = Cursors.IBeam;
  416. }
  417. protected override void OnMouseDown(MouseEventArgs e)
  418. {
  419. base.OnMouseDown(e);
  420. if (this._icon != null && this._iconIsButton && e.Button == MouseButtons.Left && this.IconRect.Contains(e.Location))
  421. {
  422. this.IconMouseState = ControlState.Pressed;
  423. }
  424. }
  425. protected override void OnMouseUp(MouseEventArgs e)
  426. {
  427. base.OnMouseUp(e);
  428. if (this._icon != null && this._iconIsButton)
  429. {
  430. this.IconMouseState = ControlState.Hover;
  431. if (e.Button == MouseButtons.Left && this.IconRect.Contains(e.Location))
  432. {
  433. this.OnIconClick();
  434. }
  435. }
  436. }
  437. protected override void OnMouseLeave(EventArgs e)
  438. {
  439. base.OnMouseLeave(e);
  440. this.MouseState = ControlState.Normal;
  441. }
  442. protected override void Dispose(bool disposing)
  443. {
  444. if (disposing && this.components != null)
  445. {
  446. this.components.Dispose();
  447. }
  448. base.Dispose(disposing);
  449. }
  450. private void InitializeComponent()
  451. {
  452. this.BaseText = new LYFZ.OtherExpansion.SkinControl.WaterTextBox();
  453. this.SuspendLayout();
  454. //
  455. // BaseText
  456. //
  457. this.BaseText.BorderStyle = System.Windows.Forms.BorderStyle.None;
  458. this.BaseText.Dock = System.Windows.Forms.DockStyle.Fill;
  459. this.BaseText.Font = new System.Drawing.Font("微软雅黑", 9.75F);
  460. this.BaseText.Location = new System.Drawing.Point(5, 5);
  461. this.BaseText.Margin = new System.Windows.Forms.Padding(0);
  462. this.BaseText.Name = "BaseText";
  463. this.BaseText.Size = new System.Drawing.Size(175, 18);
  464. this.BaseText.TabIndex = 1;
  465. this.BaseText.WaterColor = System.Drawing.Color.DarkGray;
  466. this.BaseText.WaterText = "";
  467. //
  468. // SkinTextBox
  469. //
  470. this.BackColor = System.Drawing.Color.Black;
  471. this.Controls.Add(this.BaseText);
  472. this.Cursor = System.Windows.Forms.Cursors.IBeam;
  473. this.DoubleBuffered = true;
  474. this.Margin = new System.Windows.Forms.Padding(0);
  475. this.MinimumSize = new System.Drawing.Size(0, 26);
  476. this.Name = "SkinTextBox";
  477. this.Padding = new System.Windows.Forms.Padding(5);
  478. this.Size = new System.Drawing.Size(185, 26);
  479. this.ResumeLayout(false);
  480. this.PerformLayout();
  481. }
  482. }
  483. }