DropDownContainer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable.Events;
  35. using XPTable.Models;
  36. using XPTable.Renderers;
  37. using XPTable.Win32;
  38. namespace XPTable.Editors
  39. {
  40. /// <summary>
  41. /// Summary description for DropDownContainer.
  42. /// </summary>
  43. [ToolboxItem(false)]
  44. public class DropDownContainer : Form
  45. {
  46. #region Class Data
  47. /// <summary>
  48. /// The DropDownCellEditor that owns the DropDownContainer
  49. /// </summary>
  50. private DropDownCellEditor editor;
  51. /// <summary>
  52. /// The Control displayed in the DropDownContainer
  53. /// </summary>
  54. private Control dropdownControl;
  55. /// <summary>
  56. /// A Panel that provides the black border around the DropDownContainer
  57. /// </summary>
  58. private Panel panel;
  59. #endregion
  60. #region Constructor
  61. /// <summary>
  62. /// Initializes a new instance of the DropDownContainer class with the
  63. /// specified DropDownCellEditor owner
  64. /// </summary>
  65. public DropDownContainer(DropDownCellEditor editor) : base()
  66. {
  67. if (editor == null)
  68. {
  69. throw new ArgumentNullException("editor", "DropDownCellEditor cannot be null");
  70. }
  71. this.editor = editor;
  72. this.ControlBox = false;
  73. this.MaximizeBox = false;
  74. this.MinimizeBox = false;
  75. this.FormBorderStyle = FormBorderStyle.None;
  76. this.ShowInTaskbar = false;
  77. this.StartPosition = FormStartPosition.Manual;
  78. this.TabStop = false;
  79. this.TopMost = true;
  80. this.dropdownControl = null;
  81. this.panel = new Panel();
  82. this.panel.AutoScroll = false;
  83. this.panel.BorderStyle = BorderStyle.FixedSingle;
  84. this.panel.Size = this.Size;
  85. this.Controls.Add(this.panel);
  86. this.SizeChanged += new EventHandler(DropDownContainer_SizeChanged);
  87. }
  88. #endregion
  89. #region Methods
  90. /// <summary>
  91. /// Displays the DropDownContainer to the user
  92. /// </summary>
  93. public void ShowDropDown()
  94. {
  95. this.FlushPaintMessages();
  96. this.Show();
  97. }
  98. /// <summary>
  99. /// Hides the DropDownContainer from the user
  100. /// </summary>
  101. public void HideDropDown()
  102. {
  103. this.FlushPaintMessages();
  104. this.Hide();
  105. }
  106. /// <summary>
  107. /// Processes any Paint messages in the message queue
  108. /// </summary>
  109. private void FlushPaintMessages()
  110. {
  111. MSG msg = new MSG();
  112. while (NativeMethods.PeekMessage(ref msg, IntPtr.Zero, (int) WindowMessage.WM_PAINT, (int) WindowMessage.WM_PAINT, 1 /*PM_REMOVE*/))
  113. {
  114. NativeMethods.TranslateMessage(ref msg);
  115. NativeMethods.DispatchMessage(ref msg);
  116. }
  117. }
  118. #endregion
  119. #region Properties
  120. /// <summary>
  121. /// Gets or sets the Control displayed in the DropDownContainer
  122. /// </summary>
  123. public Control Control
  124. {
  125. get
  126. {
  127. return this.dropdownControl;
  128. }
  129. set
  130. {
  131. if (value != this.dropdownControl)
  132. {
  133. this.panel.Controls.Clear();
  134. this.dropdownControl = value;
  135. if (value != null)
  136. {
  137. this.panel.Controls.Add(value);
  138. }
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Gets the required creation parameters when the control handle is created
  144. /// </summary>
  145. protected override CreateParams CreateParams
  146. {
  147. get
  148. {
  149. CreateParams cparams = base.CreateParams;
  150. cparams.ExStyle |= (int) WindowExtendedStyles.WS_EX_TOOLWINDOW;
  151. if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major > 5)
  152. {
  153. cparams.ExStyle |= (int) WindowExtendedStyles.WS_EX_NOACTIVATE;
  154. }
  155. cparams.ClassStyle |= 0x800 /*CS_SAVEBITS*/;
  156. return cparams;
  157. }
  158. }
  159. /// <summary>
  160. /// Handler for the DropDownContainer's SizeChanged event
  161. /// </summary>
  162. /// <param name="sender">The object that raised the event</param>
  163. /// <param name="e">An EventArgs that contains the event data</param>
  164. private void DropDownContainer_SizeChanged(object sender, EventArgs e)
  165. {
  166. this.panel.Size = this.Size;
  167. }
  168. #endregion
  169. }
  170. }