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.

445 lines
14 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. namespace Apewer.Surface
  8. {
  9. /// <summary></summary>
  10. internal class BlockCombo : BaseControl
  11. {
  12. private bool _delemiter = false;
  13. // private bool _lonely = false;
  14. private bool _statehover = false, _statefocus = false;
  15. private List<string> _value = new List<string>();
  16. private List<string> _alias = new List<string>();
  17. private List<Color> _color = new List<Color>();
  18. // private Panel _body = new Panel();
  19. private Panel _left = new Panel();
  20. // private Panel _middle = new Panel();
  21. private Panel _right = new Panel();
  22. private BlockLabel _label = new BlockLabel();
  23. private ComboBox _input = new ComboBox();
  24. #region this
  25. private void VarInit()
  26. {
  27. _label.Text = "Caption";
  28. _input.Text = Name;
  29. for (int i = 1; i <= 12; i++)
  30. {
  31. //_input.Items.Add(i.ToString());
  32. }
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public BlockCombo()
  38. {
  39. this.Size = new Size(300, 40);
  40. VarInit();
  41. ControlInit();
  42. ControlAdjust();
  43. EventInit();
  44. GoColor();
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. public new void Dispose()
  50. {
  51. if (_left != null) _left.Dispose();
  52. if (_right != null) _right.Dispose();
  53. if (_label != null) _label.Dispose();
  54. if (_input != null) _input.Dispose();
  55. base.Dispose();
  56. }
  57. #endregion
  58. #region surface
  59. private void ControlInit()
  60. {
  61. Controls.Add(_left);
  62. Controls.Add(_right);
  63. _left.Controls.Add(_label);
  64. _right.Controls.Add(_input);
  65. Padding = new Padding(1);
  66. _left.Dock = DockStyle.Left;
  67. _left.Padding = new Padding(8, 0, 8, 0);
  68. _left.Width = 100;
  69. _label.Dock = DockStyle.Fill;
  70. _label.TextAlign = ContentAlignment.MiddleCenter;
  71. _right.Dock = DockStyle.Right;
  72. _input.Font = FormsUtility.DefaultFont;
  73. _input.Left = 9;
  74. _input.FlatStyle = FlatStyle.Flat;
  75. _input.DrawMode = DrawMode.OwnerDrawVariable;
  76. _input.DropDownStyle = ComboBoxStyle.DropDown;
  77. _input.ItemHeight = 20;
  78. _input.BackColor = FormsUtility.GraceWall;
  79. }
  80. private void ControlAdjust()
  81. {
  82. var vfontoffset = FormsUtility.MsyhExist ? 0 : 2;
  83. _right.Width = Width - Padding.Left - Padding.Right - _left.Width - (_delemiter ? 1 : 0);
  84. _input.Width = _right.Width - 9 - 4;
  85. _input.Top = (_right.Height - 22) / 2;
  86. }
  87. private void GoColor()
  88. {
  89. if (_statefocus)
  90. {
  91. this.BackColor = _focusborder;
  92. _left.BackColor = _focusleft;
  93. _left.ForeColor = _focuscaption;
  94. _right.BackColor = _focusright;
  95. _label.ForeColor = _focuscaption;
  96. _input.ForeColor = Locked ? _label.ForeColor : _focustext;
  97. // _middle.BackColor = _left.BackColor; // this.BackColor;
  98. }
  99. else
  100. {
  101. if (_statehover)
  102. {
  103. this.BackColor = _hoverborder;
  104. _left.BackColor = _hoverleft;
  105. _left.ForeColor = _hovercaption;
  106. _right.BackColor = _hoverright;
  107. _label.ForeColor = _hovercaption;
  108. _input.ForeColor = Locked ? _label.ForeColor : _hovertext;
  109. // _middle.BackColor = _left.BackColor;
  110. }
  111. else
  112. {
  113. this.BackColor = _normalborder;
  114. _left.BackColor = _normalleft;
  115. _left.ForeColor = _normalcaption;
  116. _right.BackColor = _normalright;
  117. _label.ForeColor = _normalcaption;
  118. _input.ForeColor = Locked ? _label.ForeColor : _normaltext;
  119. // _middle.BackColor = _left.BackColor;
  120. }
  121. }
  122. _input.BackColor = _right.BackColor;
  123. }
  124. #endregion
  125. #region accessor
  126. /// <summary></summary>
  127. public event EventHandler Changed;
  128. /// <summary>添加选项。</summary>
  129. /// <param name="value"></param>
  130. public void Add(string value)
  131. {
  132. Add(value, value);
  133. }
  134. /// <summary>添加选项。</summary>
  135. /// <param name="value"></param>
  136. /// <param name="alias"></param>
  137. public void Add(string value, string alias)
  138. {
  139. Add(value, alias, Color.Black);
  140. }
  141. /// <summary>添加选项。</summary>
  142. /// <param name="value"></param>
  143. /// <param name="alias"></param>
  144. /// <param name="color"></param>
  145. public void Add(string value, string alias, Color color)
  146. {
  147. if (value != null)
  148. {
  149. var v = value;
  150. var a = string.IsNullOrEmpty(alias) ? v : alias;
  151. var c = (color == null) ? Color.Black : color;
  152. _value.Add(v);
  153. _alias.Add(a);
  154. _color.Add(c);
  155. _input.Items.Add(a);
  156. }
  157. }
  158. /// <summary>清除所有选项。</summary>
  159. public void Clean()
  160. {
  161. Clear();
  162. }
  163. /// <summary>清除所有选项。</summary>
  164. public void Clear()
  165. {
  166. _value.Clear();
  167. _alias.Clear();
  168. _color.Clear();
  169. _input.Items.Clear();
  170. }
  171. #endregion
  172. #region event
  173. private void EventInit()
  174. {
  175. this.Resize += Event_Main_Resize;
  176. this.MouseMove += Event_Caption_MouseMove;
  177. this.MouseLeave += Event_Caption_MouseLeave;
  178. this.MouseDown += Event_Caption_MouseDown;
  179. _left.MouseMove += Event_Caption_MouseMove;
  180. _left.MouseLeave += Event_Caption_MouseLeave;
  181. _left.MouseDown += Event_Caption_MouseDown;
  182. _right.MouseMove += Event_Caption_MouseMove;
  183. _right.MouseLeave += Event_Caption_MouseLeave;
  184. _right.MouseDown += Event_Caption_MouseDown;
  185. _label.MouseMove += Event_Caption_MouseMove;
  186. _label.MouseLeave += Event_Caption_MouseLeave;
  187. _label.MouseDown += Event_Caption_MouseDown;
  188. _input.MouseMove += Event_Caption_MouseMove;
  189. _input.MouseLeave += Event_Caption_MouseLeave;
  190. _input.GotFocus += Event_GotFocus;
  191. _input.LostFocus += Event_LostFocus;
  192. _input.TextChanged += Event_Input_TextChanged;
  193. _input.DrawItem += Event_Input_DrawItem;
  194. _input.SelectedIndexChanged += Event_Input_SelectedIndexChanged;
  195. }
  196. private void Event_Input_SelectedIndexChanged(object sender, EventArgs e)
  197. {
  198. if (Changed != null) Changed(this, new EventArgs());
  199. }
  200. private void Event_Input_DrawItem(object sender, DrawItemEventArgs e)
  201. {
  202. if (e.Index < 0) return;
  203. e.DrawBackground();
  204. e.DrawFocusRectangle();
  205. var vs = _input.Items[e.Index].ToString();
  206. var vx = e.Bounds.X + 7;
  207. var vy = e.Bounds.Y + 1; // e.Bounds.Y + (_input.ItemHeight - 16) / 2;
  208. var vb = new SolidBrush(e.ForeColor);
  209. if (_color != null)
  210. {
  211. if (e.Index < _color.Count)
  212. {
  213. vb.Dispose();
  214. vb = new SolidBrush(_color[e.Index]);
  215. }
  216. }
  217. e.Graphics.DrawString(vs, FormsUtility.DefaultFont, vb, vx, vy);
  218. vb.Dispose();
  219. }
  220. private void Event_Input_TextChanged(object sender, EventArgs e)
  221. {
  222. if ((Changed != null) && (!Locked)) Changed(this, new EventArgs());
  223. }
  224. private void Event_LostFocus(object sender, EventArgs e)
  225. {
  226. _statefocus = false; GoColor();
  227. }
  228. private void Event_GotFocus(object sender, EventArgs e)
  229. {
  230. _statefocus = true; GoColor();
  231. }
  232. private void Event_Caption_MouseMove(object sender, MouseEventArgs e)
  233. {
  234. }
  235. private void Event_Caption_MouseDown(object sender, MouseEventArgs e)
  236. {
  237. _input.Focus();
  238. }
  239. private void Event_Main_Resize(object sender, EventArgs e)
  240. {
  241. ControlAdjust();
  242. }
  243. private void Event_Caption_MouseLeave(object sender, EventArgs e)
  244. {
  245. _statehover = false; GoColor();
  246. }
  247. private void Event_caption_mousemove(object sender, MouseEventArgs e)
  248. {
  249. _statehover = true; GoColor();
  250. }
  251. #endregion
  252. #region property
  253. /// <summary>标签文本。</summary>
  254. public string Caption
  255. {
  256. get { return _label.Text; }
  257. set { _label.Text = string.IsNullOrEmpty(value) ? "" : value; }
  258. }
  259. /// <summary>内容文本。</summary>
  260. public new string Text
  261. {
  262. get { return _input.Text; }
  263. set { _input.Text = string.IsNullOrEmpty(value) ? "" : value; }
  264. }
  265. /// <summary>内容文本最大长度。</summary>
  266. public int Capacity
  267. {
  268. get { return _input.MaxLength; }
  269. set { _input.MaxLength = (value < 0) ? 0 : value; }
  270. }
  271. /// <summary>
  272. ///
  273. /// </summary>
  274. public float FontSize
  275. {
  276. get { return _input.Font.Size; }
  277. set { _input.Font = new Font(FormsUtility.DefaultFontName, value); }
  278. }
  279. /// <summary>独立控件,不接受 Tab 键事件。</summary>
  280. public bool Lonely
  281. {
  282. get { return !_input.TabStop; } // return _lonely; }
  283. set { _input.TabStop = !value; } // _lonely = value; _input.AcceptsTab = !value; }
  284. }
  285. /// <summary>锁定内容,禁止编辑。</summary>
  286. public override bool Locked
  287. {
  288. get { return !_input.Enabled; }
  289. set { _input.Enabled = !value; GoColor(); }
  290. }
  291. /// <summary>能够直接输入内容。</summary>
  292. public bool CanInput
  293. {
  294. get { return (_input.DropDownStyle == ComboBoxStyle.DropDown); }
  295. set { _input.DropDownStyle = value ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList; }
  296. }
  297. /// <summary>内容为空。</summary>
  298. public bool IsEmpty
  299. {
  300. get { return string.IsNullOrEmpty(_input.Text); }
  301. }
  302. /// <summary></summary>
  303. public int MenuCount
  304. {
  305. get { return _input.MaxDropDownItems; }
  306. set { _input.MaxDropDownItems = (value > 0) ? value : 1; }
  307. }
  308. #endregion
  309. #region color
  310. private Color _normalborder = FormsUtility.GraceBorder;
  311. private Color _normalleft = FormsUtility.White;
  312. private Color _normalright = FormsUtility.White;
  313. private Color _normalcaption = FormsUtility.GraceLocked;
  314. private Color _normaltext = FormsUtility.Black;
  315. private Color _hoverborder = FormsUtility.GraceSilver;
  316. private Color _hoverleft = FormsUtility.White;
  317. private Color _hoverright = FormsUtility.White;
  318. private Color _hovercaption = FormsUtility.GraceLocked;
  319. private Color _hovertext = FormsUtility.Black;
  320. private Color _focusborder = FormsUtility.GraceSilver; // FormsUtility.silver;
  321. private Color _focusleft = FormsUtility.White; // FormsUtility.wall;
  322. private Color _focusright = FormsUtility.White;
  323. private Color _focuscaption = FormsUtility.GraceMinor; // FormsUtility.gray;
  324. private Color _focustext = FormsUtility.Black;
  325. /// <summary></summary>
  326. public Color NormalBorder { get { return _normalborder; } set { if (value != null) _normalborder = value; GoColor(); } }
  327. /// <summary></summary>
  328. public Color NormalLeft { get { return _normalleft; } set { if (value != null) _normalleft = value; GoColor(); } }
  329. /// <summary></summary>
  330. public Color NormalRight { get { return _normalright; } set { if (value != null) _normalright = value; GoColor(); } }
  331. /// <summary></summary>
  332. public Color NormalCaption { get { return _normalcaption; } set { if (value != null) _normalcaption = value; GoColor(); } }
  333. /// <summary></summary>
  334. public Color NormalText { get { return _normaltext; } set { if (value != null) _normaltext = value; GoColor(); } }
  335. /// <summary></summary>
  336. public Color HoverBorder { get { return _hoverborder; } set { if (value != null) _hoverborder = value; GoColor(); } }
  337. /// <summary></summary>
  338. public Color HoverLeft { get { return _hoverleft; } set { if (value != null) _hoverleft = value; GoColor(); } }
  339. /// <summary></summary>
  340. public Color HoverRight { get { return _hoverright; } set { if (value != null) _hoverright = value; GoColor(); } }
  341. /// <summary></summary>
  342. public Color HoverCaption { get { return _hovercaption; } set { if (value != null) _hovercaption = value; GoColor(); } }
  343. /// <summary></summary>
  344. public Color HoverText { get { return _hovertext; } set { if (value != null) _hovertext = value; GoColor(); } }
  345. /// <summary></summary>
  346. public Color FocusBorder { get { return _focusborder; } set { if (value != null) _focusborder = value; GoColor(); } }
  347. /// <summary></summary>
  348. public Color FocusLeft { get { return _focusleft; } set { if (value != null) _focusleft = value; GoColor(); } }
  349. /// <summary></summary>
  350. public Color FocusRight { get { return _focusright; } set { if (value != null) _focusright = value; GoColor(); } }
  351. /// <summary></summary>
  352. public Color FocusCaption { get { return _focuscaption; } set { if (value != null) _focuscaption = value; GoColor(); } }
  353. /// <summary></summary>
  354. public Color FocusText { get { return _focustext; } set { if (value != null) _focustext = value; GoColor(); } }
  355. #endregion
  356. }
  357. }
  358. #endif