DropDownButtonBase.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using CustomControls.Enumerations;
  6. using CustomControls.HelperClasses;
  7. namespace CustomControls.BaseClasses
  8. {
  9. public class DropDownButtonBase:Control,ISupportInitialize,IMessageFilter
  10. {
  11. #region "Variables"
  12. public event EventHandler DropDown;
  13. public event EventHandler BeginInitialization;
  14. public event EventHandler EndInitialization;
  15. private Enumerations.ButtonState _State=Enumerations.ButtonState.Normal;
  16. private Image _Image= null;
  17. private ContentAlignment _TextAlign=ContentAlignment.MiddleLeft;
  18. private ContentAlignment _ImageAlign=ContentAlignment.MiddleRight;
  19. private StringFormat strFormat;
  20. private int _DropButtonWidth=10;
  21. protected DropdownFormBase dropDownForm;
  22. protected Control _HostedControl;
  23. private Form hostForm;
  24. private int _ListWidth=120;
  25. private int _ListHeight=20;
  26. private bool _CanDropDown=true;
  27. private bool selecting=false;
  28. #endregion
  29. #region "Properties"
  30. override protected System.Drawing.Size DefaultSize
  31. {
  32. get{return new System.Drawing.Size(100, 20);}
  33. }
  34. protected int DropDownButtonWidth
  35. {
  36. get
  37. {
  38. if(CanDropDown){return _DropButtonWidth;}
  39. else{return 0;}
  40. }
  41. set
  42. {
  43. if(value!=_DropButtonWidth)
  44. {
  45. _DropButtonWidth=value;
  46. Invalidate();
  47. }
  48. }
  49. }
  50. [Category("Behavior")]
  51. [DefaultValue(typeof(Boolean),"True")]
  52. public bool CanDropDown
  53. {
  54. get{return _CanDropDown;}
  55. set
  56. {
  57. if(value!=_CanDropDown)
  58. {
  59. _CanDropDown= value;
  60. Invalidate();
  61. }
  62. }
  63. }
  64. [Category("Appearance")]
  65. [DefaultValue(typeof(System.Drawing.Image),"null")]
  66. public Image Image
  67. {
  68. get{return _Image;}
  69. set
  70. {
  71. if (value !=_Image)
  72. {
  73. _Image= value;
  74. Invalidate();
  75. }
  76. }
  77. }
  78. protected virtual Enumerations.ButtonState State
  79. {
  80. get{return _State;}
  81. set
  82. {
  83. if(value!=_State)
  84. {
  85. _State= value;
  86. OnStateChanged(new EventArgs());
  87. Invalidate();
  88. }
  89. }
  90. }
  91. [Category("Appearance")]
  92. [DefaultValue(typeof(ContentAlignment),"MiddleLeft")]
  93. public ContentAlignment TextAlign
  94. {
  95. get{return _TextAlign;}
  96. set
  97. {
  98. if (value!=_TextAlign)
  99. {
  100. _TextAlign= value;
  101. Invalidate();
  102. }
  103. }
  104. }
  105. [Category("Appearance")]
  106. [DefaultValue(typeof(ContentAlignment),"MiddleRight")]
  107. public ContentAlignment ImageAlign
  108. {
  109. get{return _ImageAlign;}
  110. set
  111. {
  112. if (value!=_ImageAlign)
  113. {
  114. _ImageAlign= value;
  115. Invalidate();
  116. }
  117. }
  118. }
  119. public override string Text
  120. {
  121. get{return base.Text;}
  122. set
  123. {
  124. if (value!=base.Text)
  125. {
  126. base.Text= value;
  127. Invalidate();
  128. base.OnTextChanged(new EventArgs());
  129. }
  130. }
  131. }
  132. protected Rectangle PushRectangle
  133. {
  134. get{return new Rectangle(1,1,Width-2-DropDownButtonWidth,Height-2);}
  135. }
  136. protected Rectangle DropRectangle
  137. {
  138. get
  139. {
  140. if(CanDropDown){return new Rectangle(Width-DropDownButtonWidth-1,1,DropDownButtonWidth,Height-2);}
  141. else{return Rectangle.Empty;}
  142. }
  143. }
  144. protected virtual Control HostedControl
  145. {
  146. get{return _HostedControl;}
  147. }
  148. protected int Listwidth
  149. {
  150. get{return _ListWidth;}
  151. set
  152. {
  153. if (value !=_ListWidth)
  154. {
  155. _ListWidth=value;
  156. }
  157. }
  158. }
  159. protected int ListHeight
  160. {
  161. get{return _ListHeight;}
  162. set
  163. {
  164. if (value !=_ListHeight)
  165. {
  166. _ListHeight=value;
  167. }
  168. }
  169. }
  170. #endregion
  171. #region "Contructors"
  172. public DropDownButtonBase()
  173. {
  174. SetStyle(ControlStyles.UserPaint, true);
  175. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  176. SetStyle(ControlStyles.DoubleBuffer, true);
  177. SetStyle(ControlStyles.FixedHeight,true);
  178. SetStyle(ControlStyles.FixedWidth, true);
  179. strFormat= new StringFormat(StringFormatFlags.NoWrap);
  180. strFormat.Alignment=StringAlignment.Center;
  181. dropDownForm = new DropdownFormBase();
  182. dropDownForm.FormBorderStyle = FormBorderStyle.None;
  183. dropDownForm.StartPosition = FormStartPosition.Manual;
  184. dropDownForm.TopMost = true;
  185. dropDownForm.ShowInTaskbar = false;
  186. dropDownForm.BackColor = SystemColors.Window;
  187. dropDownForm.TabStop=false;
  188. dropDownForm.Size=GetDefaultSize();
  189. dropDownForm.LostFocus+= new EventHandler(dropDownForm_LostFocus);
  190. if (HostedControl!=null)
  191. {
  192. HostedControl.Location=new Point(2,2);
  193. HostedControl.Size=new Size(dropDownForm.Width-4,dropDownForm.Height-4);
  194. HostedControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right |AnchorStyles.Top;
  195. HostedControl.Parent=dropDownForm;
  196. HostedControl.LostFocus+=new EventHandler(dropDownForm_LostFocus);
  197. }
  198. Application.AddMessageFilter(this);
  199. }
  200. #endregion
  201. #region "Overrides"
  202. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  203. {
  204. if (Image!=null)
  205. {
  206. DrawImage(e.Graphics,GetImageRectangle(e.Graphics));
  207. }
  208. if (Text!=string.Empty)
  209. {
  210. DrawText(e.Graphics,GetTextRect(e.Graphics));
  211. }
  212. if (CanDropDown){ DrawArrow(e.Graphics,DropRectangle);}
  213. }
  214. protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
  215. {
  216. PaintBackground(e.Graphics,ClientRectangle);
  217. }
  218. protected override void OnResize(System.EventArgs e)
  219. {
  220. base.OnResize(e);
  221. Invalidate();
  222. }
  223. protected override void OnMouseEnter(EventArgs e)
  224. {
  225. base.OnMouseEnter(e);
  226. if (State!=Enumerations.ButtonState.DropDown) {State= Enumerations.ButtonState.Hot;}
  227. }
  228. protected override void OnMouseLeave(EventArgs e)
  229. {
  230. base.OnMouseLeave(e);
  231. if (State!=Enumerations.ButtonState.DropDown){ State= Enumerations.ButtonState.Normal;}
  232. }
  233. protected override void OnMouseWheel(MouseEventArgs e)
  234. {
  235. base.OnMouseWheel (e);
  236. if (e.Delta<0){ OnNextItem();}
  237. else{OnPrevItem();}
  238. }
  239. protected override void OnMouseDown(MouseEventArgs e)
  240. {
  241. base.OnMouseDown(e);
  242. if(e.Button==MouseButtons.Left)
  243. {
  244. Focus();
  245. if(e.X<Width-DropDownButtonWidth)
  246. {
  247. State= Enumerations.ButtonState.Pushed;
  248. selecting=true;
  249. }
  250. else if(CanDropDown)
  251. {
  252. if (State==Enumerations.ButtonState.DropDown)
  253. {
  254. State= Enumerations.ButtonState.Hot;
  255. }
  256. else{State= Enumerations.ButtonState.DropDown;}
  257. }
  258. else{State= Enumerations.ButtonState.Pushed;}
  259. }
  260. }
  261. protected override void OnMouseUp(MouseEventArgs e)
  262. {
  263. base.OnMouseUp(e);
  264. if (State!=Enumerations.ButtonState.DropDown)
  265. {
  266. State= Enumerations.ButtonState.Hot;
  267. }
  268. if(e.X<Width-DropDownButtonWidth && e.X>0 && selecting)
  269. {
  270. selecting=false;
  271. OnSelectItem(true);
  272. }
  273. }
  274. protected override void OnGotFocus(EventArgs e)
  275. {
  276. base.OnGotFocus(e);
  277. Invalidate();
  278. }
  279. protected override void OnLostFocus(EventArgs e)
  280. {
  281. base.OnLostFocus(e);
  282. if(!this.ContainsFocus && !dropDownForm.ContainsFocus )
  283. {
  284. State= Enumerations.ButtonState.Normal;
  285. }
  286. Invalidate();
  287. }
  288. protected override void OnKeyDown(KeyEventArgs e)
  289. {
  290. base.OnKeyDown (e);
  291. if(e.KeyData==Keys.Space)
  292. {
  293. State=Enumerations.ButtonState.Pushed;
  294. }
  295. else if (e.KeyData== Keys.Enter)
  296. {
  297. OnClick(new EventArgs());
  298. OnSelectItem(true);
  299. }
  300. }
  301. protected override void OnKeyUp(KeyEventArgs e)
  302. {
  303. base.OnKeyUp (e);
  304. if(e.KeyData==Keys.Space)
  305. {
  306. if (RectangleToScreen(DisplayRectangle).Contains(Cursor.Position))
  307. {State=Enumerations.ButtonState.Hot; }
  308. else
  309. {State=Enumerations.ButtonState.Normal;}
  310. OnClick(new EventArgs());
  311. OnSelectItem(true);
  312. }
  313. }
  314. #endregion
  315. #region "virtual"
  316. protected virtual void OnPrevItem()
  317. {}
  318. protected virtual void OnNextItem()
  319. {}
  320. protected virtual void OnPgUp()
  321. {}
  322. protected virtual void OnPgDown()
  323. {}
  324. protected virtual void OnBeginInit(EventArgs e)
  325. {
  326. if(BeginInitialization!=null)
  327. {
  328. BeginInitialization(this,e);
  329. }
  330. }
  331. protected virtual void OnEndInit(EventArgs e)
  332. {
  333. if (EndInitialization!=null)
  334. {
  335. EndInitialization(this,e);
  336. }
  337. }
  338. protected virtual void OnStateChanged(EventArgs e)
  339. {
  340. if (State==Enumerations.ButtonState.DropDown)
  341. {
  342. ShowDropDownForm();
  343. }
  344. else
  345. {
  346. HideDropDownForm();
  347. }
  348. }
  349. protected virtual void ShowDropDownForm()
  350. {
  351. dropDownForm.Location=GetBestDisplayPoint( PointToScreen(new Point(0,Height)),dropDownForm.Size,this.Height);
  352. OnDropDown(new EventArgs());
  353. dropDownForm.Show();
  354. this.Focus();
  355. }
  356. protected virtual void HideDropDownForm()
  357. {
  358. dropDownForm.Hide();
  359. }
  360. protected virtual void OnDropDown(EventArgs e)
  361. {
  362. if(DropDown!=null)
  363. {
  364. DropDown(this,e);
  365. }
  366. }
  367. protected virtual void PaintBackground(Graphics g, Rectangle bounds)
  368. {
  369. Color BorderColor=CustomControls.BaseClasses.AppColors.HighlightColor;
  370. SolidBrush BackBrush= new SolidBrush(this.Parent.BackColor);
  371. Pen BorderPen = new Pen(BorderColor);
  372. if(Enabled)
  373. {
  374. switch (State)
  375. {
  376. case Enumerations.ButtonState.Normal:
  377. {
  378. if (Focused){BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;}
  379. g.FillRectangle(BackBrush,bounds);
  380. break;
  381. }
  382. case Enumerations.ButtonState.Hot:
  383. {
  384. BackBrush.Color=CustomControls.BaseClasses.AppColors.HighlightColor;
  385. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  386. g.FillRectangle(Brushes.White,bounds);
  387. g.FillRectangle(BackBrush,bounds);
  388. break;
  389. }
  390. case Enumerations.ButtonState.Pushed:
  391. {
  392. BackBrush.Color=CustomControls.BaseClasses.AppColors.HighlightColorDark;
  393. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  394. g.FillRectangle(Brushes.White,bounds);
  395. g.FillRectangle(BackBrush,PushRectangle);
  396. if(CanDropDown)
  397. {
  398. BackBrush.Color=CustomControls.BaseClasses.AppColors.HighlightColor;
  399. g.FillRectangle(BackBrush,DropRectangle);
  400. }
  401. break;
  402. }
  403. case Enumerations.ButtonState.DropDown:
  404. {
  405. BackBrush.Color=CustomControls.BaseClasses.AppColors.HighlightColorDark;
  406. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  407. g.FillRectangle(Brushes.White,bounds);
  408. g.FillRectangle(BackBrush,DropRectangle);
  409. BackBrush.Color=CustomControls.BaseClasses.AppColors.HighlightColorLight;
  410. g.FillRectangle(BackBrush,PushRectangle);
  411. break;
  412. }
  413. }
  414. }
  415. else
  416. {
  417. BorderColor=CustomControls.BaseClasses.AppColors.ToolbarBackColor;
  418. BackBrush.Color=CustomControls.BaseClasses.AppColors.ControlColor ;
  419. g.FillRectangle(BackBrush,bounds);
  420. }
  421. BorderPen.Color=BorderColor;
  422. g.DrawRectangle(BorderPen,new Rectangle(bounds.X,bounds.Y,bounds.Width-1,bounds.Height-1));
  423. {
  424. if(CanDropDown){g.DrawLine(BorderPen,Width-DropDownButtonWidth-2,0,Width-DropDownButtonWidth -2,Height);}
  425. }
  426. BackBrush.Dispose();
  427. BorderPen.Dispose();
  428. }
  429. protected virtual void DrawText(Graphics g,Rectangle bounds)
  430. {
  431. using (SolidBrush brush= new SolidBrush(SystemColors.ControlText))
  432. {
  433. StringFormat StrFormat= new StringFormat(StringFormatFlags.NoWrap);
  434. StrFormat.Alignment=StringAlignment.Center;
  435. if (Enabled)
  436. {
  437. switch (State)
  438. {
  439. case Enumerations.ButtonState.Normal:
  440. case Enumerations.ButtonState.DropDown:
  441. case Enumerations.ButtonState.Hot:
  442. {
  443. g.DrawString(Text,Font,brush,bounds,StrFormat );
  444. break;
  445. }
  446. case Enumerations.ButtonState.Pushed:
  447. {
  448. brush.Color=SystemColors.HighlightText;
  449. g.DrawString(Text,Font,brush,bounds,StrFormat );
  450. break;
  451. }
  452. }
  453. }
  454. else
  455. {
  456. brush.Color=SystemColors.GrayText;
  457. g.DrawString(Text,Font,brush,bounds,StrFormat);
  458. }
  459. }
  460. }
  461. protected virtual void DrawImage(Graphics g,Rectangle bounds )
  462. {
  463. if (Enabled)
  464. {
  465. switch (State)
  466. {
  467. case Enumerations.ButtonState.Normal:
  468. case Enumerations.ButtonState.Pushed:
  469. case Enumerations.ButtonState.DropDown:
  470. {
  471. g.DrawImage(Image,bounds);
  472. break;
  473. }
  474. case Enumerations.ButtonState.Hot:
  475. {
  476. DrawImageDisabled(g,new Rectangle(bounds.X+1,bounds.Y+1,bounds.Width,bounds.Height),Image);
  477. g.DrawImage(Image,new Rectangle(bounds.X-1,bounds.Y-1,bounds.Width,bounds.Height));
  478. break;
  479. }
  480. }
  481. }
  482. else
  483. {
  484. DrawImageDisabled(g,bounds,Image);
  485. }
  486. }
  487. protected virtual void OnSelectItem(bool DefaultValue)
  488. {}
  489. #endregion
  490. #region "Implementation"
  491. protected void DrawImageDisabled(Graphics g,Rectangle bounds, Image image)
  492. {
  493. Bitmap DisabledImage = new Bitmap((Bitmap) image);
  494. Color shadowColor = SystemColors.ControlDark;
  495. Color transparent1 = Color.FromArgb(0, 0, 0, 0);
  496. for(int pixelX = 0; pixelX < image.Width; pixelX++)
  497. {
  498. for(int pixelY = 0; pixelY < image.Height; pixelY++)
  499. {
  500. Color pixel=DisabledImage.GetPixel(pixelX, pixelY);
  501. if ( pixel!= transparent1 )
  502. DisabledImage.SetPixel(pixelX, pixelY, shadowColor);
  503. }
  504. }
  505. g.DrawImage(DisabledImage, bounds);
  506. }
  507. private Rectangle GetTextRect(Graphics g)
  508. {
  509. SizeF textSize=g.MeasureString(Text,Font,Width,strFormat);
  510. return PositionRect(new Rectangle(3,3,Width-6-DropDownButtonWidth-1,Height-6),new Rectangle(0,0,(int)textSize.Width+3,(int)textSize.Height+3),TextAlign);
  511. }
  512. private Rectangle GetImageRectangle(Graphics g)
  513. {
  514. Rectangle pRect= new Rectangle(3,3,Width-6-DropDownButtonWidth-1,Height-6);
  515. return PositionRect(pRect,new Rectangle(0,0,Image.Width,Image.Height),ImageAlign);
  516. }
  517. private Rectangle PositionRect(Rectangle parentRect, Rectangle childRect, ContentAlignment position)
  518. {
  519. int cWidth= Math.Min(parentRect.Width,childRect.Width);
  520. int cHeight=Math.Min(parentRect.Height,childRect.Height);
  521. switch(position)
  522. {
  523. case ContentAlignment.TopLeft:
  524. {
  525. return new Rectangle(parentRect.X,parentRect.Y,cWidth,cHeight);
  526. }
  527. case ContentAlignment.MiddleLeft:
  528. {
  529. return new Rectangle(parentRect.X,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  530. }
  531. case ContentAlignment.BottomLeft:
  532. {
  533. return new Rectangle(parentRect.X,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  534. }
  535. case ContentAlignment.TopCenter:
  536. {
  537. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y,cWidth,cHeight);
  538. }
  539. case ContentAlignment.MiddleCenter:
  540. {
  541. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  542. }
  543. case ContentAlignment.BottomCenter:
  544. {
  545. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  546. }
  547. case ContentAlignment.TopRight:
  548. {
  549. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y,cWidth,cHeight);
  550. }
  551. case ContentAlignment.MiddleRight:
  552. {
  553. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  554. }
  555. case ContentAlignment.BottomRight:
  556. {
  557. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  558. }
  559. default:{return parentRect;}
  560. }
  561. }
  562. private int CenterSegment(int parentSegment, int childSegment)
  563. {
  564. return Math.Max(0,(parentSegment-childSegment)/2);
  565. }
  566. private Point[] GetArrowPoligon(Rectangle ButtonRectangle)
  567. {
  568. Point[] pts = new Point[3];
  569. pts[0] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2-2 , ButtonRectangle.Top + ButtonRectangle.Height/2-1);
  570. pts[1] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2 +3, ButtonRectangle.Top + ButtonRectangle.Height/2-1);
  571. pts[2] = new Point(ButtonRectangle.Left + ButtonRectangle.Width/2, (ButtonRectangle.Top + ButtonRectangle.Height/2-1) + 3);
  572. return pts;
  573. }
  574. private void DrawArrow(Graphics g,Rectangle bounds)
  575. {
  576. Color ArrowColor=SystemColors.ControlText;
  577. if (!Enabled){ArrowColor=AppColors.ControlColorDark;}
  578. else if (State==Enumerations.ButtonState.DropDown){ArrowColor=AppColors.WindowColor; }
  579. g.FillPolygon(new SolidBrush(ArrowColor),GetArrowPoligon(bounds));
  580. }
  581. private Size GetDefaultSize()
  582. {
  583. return new Size(this.Width,20);
  584. }
  585. private void dropDownForm_LostFocus(object sender, EventArgs e)
  586. {
  587. if ( !this.ContainsFocus && !this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position))
  588. {
  589. State=Enumerations.ButtonState.Normal;
  590. }
  591. }
  592. public void BeginInit()
  593. {
  594. OnBeginInit(new EventArgs());
  595. }
  596. public void EndInit()
  597. {
  598. hostForm=this.FindForm();
  599. if (hostForm!=null)
  600. {
  601. hostForm.Move+=new EventHandler(hostForm_MoveResize);
  602. hostForm.Resize+=new EventHandler(hostForm_MoveResize);
  603. dropDownForm.Owner=hostForm;
  604. }
  605. OnEndInit(new EventArgs());
  606. }
  607. private void hostForm_MoveResize(object sender, EventArgs e)
  608. {
  609. State=Enumerations.ButtonState.Normal;
  610. }
  611. public bool PreFilterMessage(ref Message msg)
  612. {
  613. if(Enabled && (this.ContainsFocus || dropDownForm.ContainsFocus) )
  614. {
  615. int code = (int)msg.WParam;
  616. switch (msg.Msg)
  617. {
  618. case (int)CustomControls.Enumerations.Msgs.WM_LBUTTONDOWN:
  619. case(int)CustomControls.Enumerations.Msgs.WM_RBUTTONDOWN:
  620. {
  621. if (State==Enumerations.ButtonState.DropDown && (!this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position) && !dropDownForm.RectangleToScreen(dropDownForm.DisplayRectangle).Contains(Cursor.Position)))
  622. {
  623. State=Enumerations.ButtonState.Normal;
  624. }
  625. return false;
  626. }
  627. case (int)CustomControls.Enumerations.Msgs.WM_SYSKEYUP:
  628. {
  629. if(ModifierKeys==Keys.Alt)
  630. {
  631. if (code==(int)Keys.Down ||code==(int)Keys.Up )
  632. {
  633. ToggleDropDown();
  634. return true;
  635. }
  636. }
  637. break;
  638. }
  639. case(int)CustomControls.Enumerations.Msgs.WM_KEYDOWN:
  640. {
  641. if(code==(int)Keys.F4)
  642. {
  643. ToggleDropDown();
  644. return true;
  645. }
  646. else if(code ==(int)Keys.Escape && State==Enumerations.ButtonState.DropDown)
  647. {
  648. ToggleDropDown();
  649. return true;
  650. }
  651. else if(code ==(int)Keys.Enter && State==Enumerations.ButtonState.DropDown)
  652. {
  653. ToggleDropDown();
  654. OnSelectItem(false);
  655. return true;
  656. }
  657. else if(this.ContainsFocus )
  658. {
  659. if(code==(int)Keys.Down)
  660. {
  661. OnNextItem();
  662. return true;
  663. }
  664. else if(code==(int)Keys.Up)
  665. {
  666. OnPrevItem();
  667. return true;
  668. }
  669. else if(code==(int)Keys.PageUp)
  670. {
  671. OnPgUp();
  672. return true;
  673. }
  674. else if(code==(int)Keys.PageDown)
  675. {
  676. OnPgDown();
  677. return true;
  678. }
  679. }
  680. break;
  681. }
  682. }
  683. }
  684. return false;
  685. }
  686. private void ToggleDropDown()
  687. {
  688. if (State==Enumerations.ButtonState.Normal || State==Enumerations.ButtonState.Hot ) {State=Enumerations.ButtonState.DropDown; }
  689. else if(State==Enumerations.ButtonState.DropDown)
  690. {
  691. if(this.RectangleToScreen(DisplayRectangle).Contains(Cursor.Position))
  692. {
  693. State=Enumerations.ButtonState.Hot;
  694. }
  695. else
  696. {
  697. State=Enumerations.ButtonState.Normal;
  698. }
  699. }
  700. }
  701. private Point GetBestDisplayPoint(Point OriginPoint, Size WindowSize, int yJump)
  702. {
  703. int tmpX=OriginPoint.X;
  704. int tmpY=OriginPoint.Y;
  705. int ScreenHeight=Screen.PrimaryScreen.WorkingArea.Height;
  706. int ScreenWidth=Screen.PrimaryScreen.WorkingArea.Width;
  707. if(ScreenHeight- tmpY-WindowSize.Height<0)
  708. {
  709. tmpY-=WindowSize.Height+yJump;
  710. }
  711. tmpX=Math.Min( Math.Max(0,tmpX),ScreenWidth-WindowSize.Width);
  712. return new Point(tmpX,tmpY);
  713. }
  714. #endregion
  715. }
  716. }
  717. namespace CustomControls.HelperClasses
  718. {
  719. public struct ItemSelectedEventArgs
  720. {
  721. object _SelectedItem;
  722. int _SelectedIndex;
  723. public ItemSelectedEventArgs(object SelectedItem, int SelectedIndex )
  724. {
  725. this._SelectedItem=SelectedItem;
  726. this._SelectedIndex =SelectedIndex ;
  727. }
  728. public int SelectedIndex
  729. {
  730. get{return _SelectedIndex;}
  731. }
  732. public object SelectedItem
  733. {
  734. get{return _SelectedItem;}
  735. }
  736. }
  737. }