DropDownListBase.cs 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using CustomControls.HelperClasses;
  7. using CustomControls.Enumerations;
  8. namespace CustomControls.BaseClasses
  9. {
  10. public class DropDownListBase:Control,ISupportInitialize,IMessageFilter
  11. {
  12. #region "Variable Declaration"
  13. public event EventHandler BeginInitialization;
  14. public event EventHandler EndInitialization;
  15. protected DropdownFormBase dropDownForm;
  16. private int buttonWidth=14;
  17. protected Control _HostedControl;
  18. protected EditControl editControl;
  19. private Form hostForm;
  20. private StringFormat strFormat;
  21. private HorizontalAlignment _TextAlign=HorizontalAlignment.Left;
  22. private Enumerations.CustomBorderStyle _CustomBorderStyle=Enumerations.CustomBorderStyle.Hot;
  23. private bool _DroppedDown=false;
  24. private int _ListWidth=120;
  25. private int _ListHeight=20;
  26. private Enumerations.State _State=State.Normal;
  27. private Color _BackColor=SystemColors.Window;
  28. private ComboBoxStyle _DropDownStyle=ComboBoxStyle.DropDown;
  29. private bool _Initialized=false;
  30. #endregion
  31. #region "Properties"
  32. [Browsable(false)]
  33. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  34. public bool Initialized
  35. {
  36. get{return _Initialized;}
  37. }
  38. [Browsable(false)]
  39. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  40. public virtual bool DroppedDown
  41. {
  42. get{return _DroppedDown;}
  43. set
  44. {
  45. if (value!=_DroppedDown)
  46. {
  47. _DroppedDown=value;
  48. OnDropDownChanged(new EventArgs());
  49. }
  50. }
  51. }
  52. [Category("Appearance")]
  53. [DefaultValue(typeof(ComboBoxStyle),"DropDown")]
  54. public virtual ComboBoxStyle DropDownStyle
  55. {
  56. get{return _DropDownStyle;}
  57. set
  58. {
  59. if (value!=_DropDownStyle)
  60. {
  61. _DropDownStyle=value;
  62. OnDropDownStyleChanged(new EventArgs());
  63. }
  64. }
  65. }
  66. protected virtual Control HostedControl
  67. {
  68. get{return _HostedControl;}
  69. }
  70. public override string Text
  71. {
  72. get
  73. {
  74. return editControl.Text;
  75. }
  76. set
  77. {
  78. if(value !=editControl.Text)
  79. {
  80. editControl.Text = value;
  81. //OnTextChanged(new EventArgs());
  82. Invalidate();
  83. }
  84. }
  85. }
  86. [DefaultValue(typeof(Enumerations.CustomBorderStyle),"Hot")]
  87. [Category("Appearance")]
  88. public virtual Enumerations.CustomBorderStyle CustomBorderStyle
  89. {
  90. get{return _CustomBorderStyle;}
  91. set
  92. {
  93. if (value!=_CustomBorderStyle)
  94. {
  95. _CustomBorderStyle=value;
  96. RefreshEditRectangle();
  97. this.Invalidate();
  98. }
  99. }
  100. }
  101. protected int Listwidth
  102. {
  103. get{return _ListWidth;}
  104. set
  105. {
  106. if (value !=_ListWidth)
  107. {
  108. _ListWidth=value;
  109. }
  110. }
  111. }
  112. protected int ListHeight
  113. {
  114. get{return _ListHeight;}
  115. set
  116. {
  117. if (value !=_ListHeight)
  118. {
  119. _ListHeight=value;
  120. }
  121. }
  122. }
  123. public override Color BackColor
  124. {
  125. get
  126. {
  127. return _BackColor;
  128. }
  129. set
  130. {
  131. if (value !=_BackColor)
  132. {
  133. _BackColor = value;
  134. Invalidate();
  135. }
  136. }
  137. }
  138. protected Enumerations.State State
  139. {
  140. get{return _State;}
  141. set
  142. {
  143. if (value!=_State)
  144. {
  145. _State=value;
  146. OnStateChanged(new EventArgs());
  147. }
  148. }
  149. }
  150. protected override Size DefaultSize
  151. {
  152. get
  153. {
  154. return new Size(120,20);
  155. }
  156. }
  157. protected new Rectangle ClientRectangle
  158. {
  159. get
  160. {
  161. if (CustomBorderStyle==Enumerations.CustomBorderStyle.None)
  162. {
  163. return base.ClientRectangle;
  164. }
  165. else
  166. {
  167. return new Rectangle(1,1,base.ClientRectangle.Width-2,base.ClientRectangle.Height-2);
  168. }
  169. }
  170. }
  171. [Category("Appearance")]
  172. [DefaultValue(typeof(HorizontalAlignment),"Left")]
  173. public HorizontalAlignment TextAlign
  174. {
  175. get{return _TextAlign;}
  176. set
  177. {
  178. if (value!=_TextAlign)
  179. {
  180. _TextAlign=value;
  181. editControl.TextAlign=value;
  182. Invalidate();
  183. }
  184. }
  185. }
  186. protected Rectangle EditRectangle
  187. {
  188. get
  189. {
  190. if (DropDownStyle!=ComboBoxStyle.Simple)
  191. {
  192. Rectangle cr=ClientRectangle;
  193. return new Rectangle(cr.X,cr.Y,cr.Width-buttonWidth-1,cr.Height);
  194. }
  195. else{return ClientRectangle;}
  196. }
  197. }
  198. protected Rectangle ButtonRectangle
  199. {
  200. get
  201. {
  202. if (DropDownStyle!=ComboBoxStyle.Simple)
  203. {
  204. Rectangle cr=ClientRectangle;
  205. return new Rectangle(cr.Right-buttonWidth,cr.Top,buttonWidth,cr.Height);
  206. }
  207. else
  208. {
  209. return Rectangle.Empty;
  210. }
  211. }
  212. }
  213. #endregion
  214. #region "Constructor"
  215. public DropDownListBase()
  216. {
  217. SetStyle(ControlStyles.AllPaintingInWmPaint,true);
  218. SetStyle(ControlStyles.UserPaint, true);
  219. SetStyle(ControlStyles.DoubleBuffer, true);
  220. SetStyle(ControlStyles.FixedHeight,true);
  221. SetStyle(ControlStyles.FixedWidth, true);
  222. SetStyle(ControlStyles.ResizeRedraw, true);
  223. SetStyle(ControlStyles.Selectable, false);
  224. editControl= new EditControl();
  225. editControl.AutoSize=false;
  226. RefreshEditRectangle();
  227. editControl.MouseEnter+= new EventHandler(TopMouseEnter);
  228. editControl.MouseLeave+=new EventHandler(TopMouseLeave);
  229. editControl.GotFocus+= new EventHandler(TopGotFocus);
  230. editControl.LostFocus+= new EventHandler(TopLostFocus);
  231. editControl.MouseDown+=new MouseEventHandler(TopMouseDown);
  232. editControl.TextChanged+= new EventHandler(editControl_TextChanged);
  233. this.Controls.Add(editControl);
  234. dropDownForm = new DropdownFormBase();
  235. dropDownForm.FormBorderStyle = FormBorderStyle.None;
  236. dropDownForm.StartPosition = FormStartPosition.Manual;
  237. dropDownForm.TopMost = true;
  238. dropDownForm.ShowInTaskbar = false;
  239. dropDownForm.BackColor = SystemColors.Window;
  240. dropDownForm.TabStop=false;
  241. dropDownForm.Size=GetDefaultSize();
  242. dropDownForm.LostFocus+= new EventHandler(dropDownForm_LostFocus);
  243. if (HostedControl!=null)
  244. {
  245. HostedControl.Location=new Point(2,2);
  246. HostedControl.Size=new Size(dropDownForm.Width-4,dropDownForm.Height-4);
  247. HostedControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right |AnchorStyles.Top;
  248. HostedControl.Parent=dropDownForm;
  249. HostedControl.LostFocus+=new EventHandler(dropDownForm_LostFocus);
  250. }
  251. strFormat= new StringFormat(StringFormatFlags.NoWrap);
  252. Application.AddMessageFilter(this);
  253. }
  254. #endregion
  255. #region "Overrides"
  256. protected override void OnPaint(PaintEventArgs e)
  257. {
  258. DrawButton(e.Graphics);
  259. DrawBorder(e.Graphics);
  260. DrawButtonLLVLine(e.Graphics);
  261. if(DropDownStyle==ComboBoxStyle.DropDownList)
  262. {
  263. strFormat.Alignment= HAlignToStrAlign(TextAlign);
  264. DrawText(e.Graphics,Text,Font,EditRectangle,strFormat);
  265. }
  266. }
  267. private StringAlignment HAlignToStrAlign(HorizontalAlignment hAlign)
  268. {
  269. switch (hAlign)
  270. {
  271. case HorizontalAlignment.Center:
  272. {
  273. return StringAlignment.Center;
  274. }
  275. case HorizontalAlignment.Left:
  276. {
  277. return StringAlignment.Near;
  278. }
  279. case HorizontalAlignment.Right:
  280. {
  281. return StringAlignment.Far;
  282. }
  283. default :
  284. {
  285. return StringAlignment.Near;
  286. }
  287. }
  288. }
  289. protected override void OnPaintBackground(PaintEventArgs pevent)
  290. {
  291. using (SolidBrush backBrush= new SolidBrush(AppColors.WindowColor))
  292. {
  293. if (!Enabled)
  294. {
  295. backBrush.Color=AppColors.ControlColor;
  296. }
  297. pevent.Graphics.FillRectangle(backBrush,ClientRectangle);
  298. }
  299. }
  300. protected override void OnResize(EventArgs e)
  301. {
  302. base.OnResize (e);
  303. RefreshEditRectangle();
  304. Invalidate();
  305. }
  306. protected override void OnMouseEnter(EventArgs e)
  307. {
  308. base.OnMouseEnter(e);
  309. TopMouseEnter(this,e);
  310. }
  311. private void TopMouseEnter(object sender, EventArgs e)
  312. {
  313. if (!DroppedDown )
  314. {
  315. this.State=State.Hot;
  316. }
  317. }
  318. protected override void OnMouseLeave(EventArgs e)
  319. {
  320. base.OnMouseLeave(e);
  321. TopMouseLeave(this,e);
  322. }
  323. private void TopMouseLeave(object sender, EventArgs e)
  324. {
  325. if (!DroppedDown && !ContainsFocus && !editControl.Capture)
  326. {
  327. this.State=State.Normal;
  328. }
  329. }
  330. protected override void OnLostFocus(EventArgs e)
  331. {
  332. base.OnLostFocus(e);
  333. TopLostFocus(this,e);
  334. }
  335. private void TopLostFocus(object sender, EventArgs e)
  336. {
  337. if(!this.ContainsFocus && !dropDownForm.ContainsFocus )
  338. {
  339. DroppedDown=false;
  340. this.State=State.Normal;
  341. }
  342. }
  343. protected override void OnGotFocus(EventArgs e)
  344. {
  345. //base.OnGotFocus(e);
  346. TopGotFocus(this,e);
  347. }
  348. private void TopGotFocus(object sender, EventArgs e)
  349. {
  350. base.OnGotFocus(e);
  351. editControl.Focus();
  352. if(!DroppedDown){this.State=State.Hot;}
  353. }
  354. protected override void OnMouseDown(MouseEventArgs e)
  355. {
  356. base.OnMouseDown (e);
  357. TopMouseDown(this,e);
  358. }
  359. private void TopMouseDown(object sender, MouseEventArgs e)
  360. {
  361. Focus();
  362. if (DropDownStyle==ComboBoxStyle.DropDown)
  363. {
  364. if (ButtonRectangle.Contains(new Point(e.X,e.Y)))
  365. {
  366. OnButtonClick(new EventArgs());
  367. }
  368. }
  369. else
  370. {
  371. if(ClientRectangle.Contains(new Point(e.X,e.Y)) )
  372. {
  373. OnButtonClick(new EventArgs());
  374. }
  375. }
  376. if(DroppedDown && !this.DisplayRectangle.Contains(new Point(e.X,e.Y)))
  377. {
  378. DroppedDown=false;
  379. }
  380. }
  381. protected override void OnMouseWheel(MouseEventArgs e)
  382. {
  383. base.OnMouseWheel (e);
  384. if (e.Delta<0){ OnNextItem();}
  385. else{OnPrevItem();}
  386. }
  387. protected override void OnLeave(EventArgs e)
  388. {
  389. base.OnLeave(e);
  390. OnItemSelected(DroppedDown);
  391. }
  392. protected override void OnEnabledChanged(EventArgs e)
  393. {
  394. base.OnEnabledChanged (e);
  395. if (Enabled){editControl.BackColor=AppColors.WindowColor;}
  396. else{editControl.BackColor=AppColors.ControlColor;}
  397. }
  398. #endregion
  399. #region "Virtual"
  400. protected virtual void OnPrevItem()
  401. {}
  402. protected virtual void OnNextItem()
  403. {}
  404. protected virtual void OnScrollUp()
  405. {}
  406. protected virtual void OnScrollDown()
  407. {}
  408. protected virtual void OnPgUp()
  409. {}
  410. protected virtual void OnPgDown()
  411. {}
  412. protected virtual void OnBeginInit(EventArgs e)
  413. {
  414. if(BeginInitialization!=null)
  415. {
  416. BeginInitialization(this,e);
  417. }
  418. }
  419. protected virtual void OnEndInit(EventArgs e)
  420. {
  421. if (EndInitialization!=null)
  422. {
  423. EndInitialization(this,e);
  424. }
  425. }
  426. protected virtual void DrawText(Graphics g,string text,Font font,Rectangle bounds,StringFormat strformat)
  427. {
  428. using (SolidBrush textBrush= new SolidBrush(BaseClasses.AppColors.TextColor))
  429. {
  430. if(!Enabled){textBrush.Color=AppColors.ControlColorDark;}
  431. g.DrawString(text,font,textBrush,GetTextRectangle(font.Height,bounds),strFormat);
  432. }
  433. }
  434. protected virtual void OnDropDownChanged(EventArgs e)
  435. {
  436. if (DroppedDown){ShowDropDownForm();}
  437. else
  438. {
  439. if (ContainsFocus)
  440. {State=State.Hot;}
  441. else
  442. {State= State.Normal;}
  443. HideDropDownForm();
  444. }
  445. }
  446. protected virtual void OnShowButtonChanged(EventArgs e)
  447. {
  448. RefreshEditRectangle();
  449. Invalidate();
  450. }
  451. protected virtual void OnStateChanged(EventArgs e)
  452. {
  453. Invalidate();
  454. }
  455. protected virtual void OnDropDownStyleChanged(EventArgs e)
  456. {
  457. if(DropDownStyle==ComboBoxStyle.DropDownList)
  458. {
  459. editControl.Visible=false;
  460. SetStyle(ControlStyles.Selectable,true);
  461. }
  462. else
  463. {
  464. editControl.Visible=true;
  465. SetStyle(ControlStyles.Selectable,false);
  466. }
  467. RefreshEditRectangle();
  468. Invalidate();
  469. }
  470. protected virtual void OnButtonClick(EventArgs e)
  471. {
  472. DroppedDown=!_DroppedDown;
  473. if (DroppedDown)
  474. {
  475. State=State.DropDown;
  476. }
  477. else
  478. {
  479. State=State.Hot;
  480. }
  481. }
  482. protected virtual void OnItemSelected(bool Dropped)
  483. {}
  484. protected virtual void OnDropDown(System.EventArgs e)
  485. {
  486. dropDownForm.Size=new Size(this.Listwidth,this.ListHeight);
  487. }
  488. #endregion
  489. #region "Implementation"
  490. public bool PreFilterMessage(ref Message msg)
  491. {
  492. if(Enabled && (this.ContainsFocus || dropDownForm.ContainsFocus) )
  493. {
  494. int code = (int)msg.WParam;
  495. switch (msg.Msg)
  496. {
  497. case (int)CustomControls.Enumerations.Msgs.WM_MOUSEWHEEL:
  498. {
  499. if((int)msg.WParam>0)
  500. {
  501. if(DroppedDown){OnScrollUp();}
  502. else{OnPrevItem();}
  503. }
  504. else
  505. {
  506. if(DroppedDown){OnScrollDown();}
  507. else{OnNextItem();}
  508. }
  509. return true;
  510. }
  511. case (int)CustomControls.Enumerations.Msgs.WM_LBUTTONDOWN:
  512. case(int)CustomControls.Enumerations.Msgs.WM_RBUTTONDOWN:
  513. {
  514. if (DroppedDown && (!this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position) && !dropDownForm.RectangleToScreen(dropDownForm.DisplayRectangle).Contains(Cursor.Position)))
  515. {
  516. DroppedDown=false;
  517. }
  518. return false;
  519. }
  520. case (int)CustomControls.Enumerations.Msgs.WM_SYSKEYUP:
  521. {
  522. if(ModifierKeys==Keys.Alt)
  523. {
  524. if (code==(int)Keys.Down ||code==(int)Keys.Up )
  525. {
  526. DroppedDown=!DroppedDown;
  527. return true;
  528. }
  529. }
  530. break;
  531. }
  532. case(int)CustomControls.Enumerations.Msgs.WM_KEYDOWN:
  533. {
  534. if(DropDownStyle!=ComboBoxStyle.DropDownList){editControl.Focus();}
  535. if(code==(int)Keys.F4)
  536. {
  537. DroppedDown=!DroppedDown;
  538. return true;
  539. }
  540. else if(code ==(int)Keys.Escape && DroppedDown)
  541. {
  542. DroppedDown=false;
  543. return true;
  544. }
  545. else if(code ==(int)Keys.Enter )
  546. {
  547. bool flag=false;
  548. if (DroppedDown)
  549. {
  550. flag=true;
  551. DroppedDown=false;
  552. }
  553. OnItemSelected(flag);
  554. return true;
  555. }
  556. else if(this.ContainsFocus ||dropDownForm.ContainsFocus)
  557. {
  558. if(code==(int)Keys.Down)
  559. {
  560. OnNextItem();
  561. return true;
  562. }
  563. else if(code==(int)Keys.Up)
  564. {
  565. OnPrevItem();
  566. return true;
  567. }
  568. else if(code==(int)Keys.PageUp)
  569. {
  570. OnPgUp();
  571. return true;
  572. }
  573. else if(code==(int)Keys.PageDown)
  574. {
  575. OnPgDown();
  576. return true;
  577. }
  578. }
  579. break;
  580. }
  581. }
  582. }
  583. return false;
  584. }
  585. public void BeginInit()
  586. {
  587. _Initialized=false;
  588. OnBeginInit(new EventArgs());
  589. }
  590. public void EndInit()
  591. {
  592. hostForm=this.FindForm();
  593. if (hostForm!=null)
  594. {
  595. hostForm.Move+=new EventHandler(hostForm_MoveResize);
  596. hostForm.Resize+=new EventHandler(hostForm_MoveResize);
  597. dropDownForm.Owner=hostForm;
  598. _Initialized=true;
  599. }
  600. OnEndInit(new EventArgs());
  601. }
  602. private void hostForm_MoveResize(object sender, EventArgs e)
  603. {
  604. DroppedDown=false;
  605. }
  606. private void dropDownForm_LostFocus(object sender, EventArgs e)
  607. {
  608. if ( !this.ContainsFocus && !this.dropDownForm.ContainsFocus &&!this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position))
  609. {
  610. DroppedDown=false;
  611. }
  612. }
  613. protected virtual void DrawBorder(Graphics g)
  614. {
  615. switch(CustomBorderStyle)
  616. {
  617. case Enumerations.CustomBorderStyle.None:
  618. {
  619. break;
  620. }
  621. case Enumerations.CustomBorderStyle.FixedSingle:
  622. {
  623. {g.DrawRectangle(Pens.Black,new Rectangle(0,0, this.DisplayRectangle.Width-1,this.DisplayRectangle.Height-1));}
  624. break;
  625. }
  626. case Enumerations.CustomBorderStyle.Hot:
  627. {
  628. if (!Enabled )
  629. {
  630. {
  631. g.DrawRectangle(new Pen(CustomControls.BaseClasses.AppColors.ToolbarBackColor),new Rectangle(0,0, this.DisplayRectangle.Width-1,this.DisplayRectangle.Height-1));}
  632. }
  633. else
  634. {
  635. switch(State)
  636. {
  637. case State.Normal:
  638. {
  639. {
  640. Color borderColor=(this.Parent!=null)?Parent.BackColor:CustomControls.BaseClasses.AppColors.ControlColor;
  641. g.DrawRectangle(new Pen(borderColor),new Rectangle(0,0, this.DisplayRectangle.Width-1,this.DisplayRectangle.Height-1));}
  642. break;
  643. }
  644. case State.Hot:
  645. case State.DropDown:
  646. {
  647. {g.DrawRectangle(new Pen(CustomControls.BaseClasses.AppColors.HighlightColorDarkDark),new Rectangle(0,0, this.Width-1,this.Height-1));}
  648. break;
  649. }
  650. }
  651. }
  652. break;
  653. }
  654. }
  655. }
  656. protected virtual void DrawButton(Graphics g )
  657. {
  658. if (DropDownStyle!=ComboBoxStyle.Simple)
  659. {
  660. Color backColor=(this.Parent!=null)?Parent.BackColor:CustomControls.BaseClasses.AppColors.ControlColor;
  661. Color arrowColor=CustomControls.BaseClasses.AppColors.TextColor;
  662. Rectangle backRect=ButtonRectangle;
  663. if (Enabled)
  664. {
  665. switch (State)
  666. {
  667. case State.Normal:
  668. {
  669. if (CustomBorderStyle!=Enumerations.CustomBorderStyle.FixedSingle)
  670. {
  671. backRect=new Rectangle(backRect.Left+1,backRect.Top+1,backRect.Width-2,backRect.Height-2);
  672. }
  673. break;
  674. }
  675. case State.Hot:
  676. {
  677. backColor=CustomControls.BaseClasses.AppColors.HighlightColor;
  678. break;
  679. }
  680. case State.DropDown:
  681. {
  682. backColor=CustomControls.BaseClasses.AppColors.HighlightColorDark;
  683. arrowColor=CustomControls.BaseClasses.AppColors.WindowColor;
  684. break;
  685. }
  686. }
  687. }
  688. else
  689. {
  690. if (CustomBorderStyle!=Enumerations.CustomBorderStyle.FixedSingle)
  691. {
  692. backRect=new Rectangle(backRect.Left+1,backRect.Top+1,backRect.Width-2,backRect.Height-2);
  693. }
  694. arrowColor=CustomControls.BaseClasses.AppColors.ControlColorDark;
  695. }
  696. g.FillRectangle(new SolidBrush(backColor), backRect);
  697. DrawArrow(g,backRect,arrowColor);
  698. }
  699. }
  700. private Point[] GetArrowPoligon(Rectangle ButtonRectangle)
  701. {
  702. Point[] pts = new Point[3];
  703. pts[0] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2-2 , ButtonRectangle.Top + ButtonRectangle.Height/2-1);
  704. pts[1] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2 +3, ButtonRectangle.Top + ButtonRectangle.Height/2-1);
  705. pts[2] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2, (ButtonRectangle.Top + ButtonRectangle.Height/2-1) + 3);
  706. return pts;
  707. }
  708. private void DrawArrow(Graphics g,Rectangle bounds,Color ArrowColor)
  709. {
  710. g.FillPolygon(new SolidBrush(ArrowColor),GetArrowPoligon(bounds));
  711. }
  712. private void DrawButtonLLVLine(Graphics g)
  713. {
  714. if(DropDownStyle!=ComboBoxStyle.Simple )
  715. {
  716. bool drawLine=false;
  717. Color lineColor=SystemColors.ControlText;
  718. switch (CustomBorderStyle)
  719. {
  720. case Enumerations.CustomBorderStyle.FixedSingle:
  721. {
  722. drawLine=true;
  723. break;
  724. }
  725. case Enumerations.CustomBorderStyle.Hot:
  726. {
  727. if (Enabled )
  728. {
  729. if(State!=State.Normal)
  730. {
  731. drawLine=true;
  732. lineColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  733. }
  734. }
  735. else
  736. {
  737. drawLine=true;
  738. lineColor=CustomControls.BaseClasses.AppColors.ToolbarBackColor;
  739. }
  740. break;
  741. }
  742. }
  743. if (drawLine)
  744. {
  745. Rectangle cr=ClientRectangle;
  746. Point pt= new Point(cr.Right-buttonWidth-1,cr.Top);
  747. Point pb= new Point(cr.Right-buttonWidth-1, cr.Bottom);
  748. g.DrawLine(new Pen(lineColor),pt, pb);
  749. }
  750. }
  751. }
  752. protected virtual void ShowDropDownForm()
  753. {
  754. if(!Initialized){EndInit();}
  755. dropDownForm.Location=GetBestDisplayPoint( PointToScreen(new Point(0,Height)),dropDownForm.Size,this.Height);
  756. OnDropDown(new System.EventArgs());
  757. dropDownForm.Show();
  758. if(DropDownStyle!=ComboBoxStyle.DropDownList){editControl.Focus();}
  759. else{this.Focus();}
  760. }
  761. protected virtual void HideDropDownForm()
  762. {
  763. dropDownForm.Hide();
  764. }
  765. private Point GetBestDisplayPoint(Point OriginPoint, Size WindowSize, int yJump)
  766. {
  767. int tmpX=OriginPoint.X;
  768. int tmpY=OriginPoint.Y;
  769. int ScreenHeight=Screen.PrimaryScreen.WorkingArea.Height;
  770. int ScreenWidth=Screen.PrimaryScreen.WorkingArea.Width;
  771. if(ScreenHeight- tmpY-WindowSize.Height<0)
  772. {
  773. tmpY-=WindowSize.Height+yJump;
  774. }
  775. tmpX=Math.Min( Math.Max(0,tmpX),ScreenWidth-WindowSize.Width);
  776. return new Point(tmpX,tmpY);
  777. }
  778. private void RefreshEditRectangle()
  779. {
  780. editControl.Size= new Size(EditRectangle.Width,EditRectangle.Height);
  781. editControl.Location= new Point(ClientRectangle.Left, ClientRectangle.Top);
  782. }
  783. private void editControl_TextChanged(object sender,EventArgs e)
  784. {
  785. OnTextChanged(e);
  786. }
  787. private Size GetDefaultSize()
  788. {
  789. return new Size(this.Width,20);
  790. }
  791. protected RectangleF GetTextRectangle(int TextHeight,Rectangle bounds)
  792. {
  793. return new RectangleF(bounds.X+2 , bounds.Y + Math.Max(0, (bounds.Height - TextHeight) / 2), Math.Max(bounds.Width-4, 1), bounds.Height - Math.Max(0, (bounds.Height - TextHeight) / 2));
  794. }
  795. #endregion
  796. }
  797. }
  798. namespace CustomControls.Enumerations
  799. {
  800. #region "Enumerations"
  801. public enum CustomBorderStyle
  802. {
  803. None=0,
  804. FixedSingle=1,
  805. Hot=2
  806. }
  807. public enum State
  808. {
  809. Normal=0,
  810. Hot=1,
  811. DropDown=2
  812. }
  813. public enum DisplayPosition
  814. {
  815. TopLeft=0,
  816. TopRight=1,
  817. BottonLeft=2,
  818. BottomRight=3
  819. }
  820. public enum ButtonState
  821. {
  822. Normal=0,
  823. Hot=1,
  824. Pushed=2,
  825. DropDown=3
  826. }
  827. #endregion
  828. }