BaseContainer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. namespace LYFZ.ComponentLibrary
  8. {
  9. /// <summary>
  10. /// Summary description for BaseContainer.
  11. /// </summary>
  12. public abstract class BaseContainer : System.Windows.Forms.GroupBox, IGradientContainer
  13. {
  14. #region Constants
  15. private const int IDEFAULT_BORDERWIDTH = 6; // Default value of BorderWidth Property
  16. private Size ODEFAULT_SIZEBORDERPIXELINDET = new Size(16, 16); // Default value of moSizeBorderPixelIndent
  17. private static Color ODEFAULT_GRADIENTTOPCOLOR = Color.FromArgb(225, 225, 183); // Default value of GradientTopColor Property
  18. private static Color ODEFAULT_GRADIENTBOTTOMCOLOR = Color.FromArgb(167, 168, 127); // Default value of GradientBottomColor Property
  19. private static Color ODEFAULT_HEADINGTEXTCOLOR = Color.FromArgb(57, 66, 1); // Default value of HeaderTextColor Property
  20. private static Color ODEFAULT_INTERIORTOPCOLOR = Color.FromArgb(245, 243, 219); // Default value of InteriorGradientTopColor Property
  21. private static Color ODEFAULT_INTERIORBOTTOMCOLOR = Color.FromArgb(214, 209, 153); // Default value of InteriorGradientBottomColor Property
  22. private static Color ODEFAULT_SHADOWCOLOR = Color.FromArgb(142, 143, 116); // Default value of ShadowColor Property
  23. // These values are used in LinerGradientBrush's blend property to specify the Factor and Postion
  24. // When the values are changed the gradient is drawn differently
  25. protected Single[] IARR_RELATIVEINTENSITIES = {0.0F, 0.32F, 1.0F}; // Values for Factor property of blend
  26. protected Single[] IARR_RELATIVEPOSITIONS = {0.0F, 0.44F, 1.0F}; // Values for Position property of blend
  27. #endregion
  28. #region Private Data Members
  29. // Defining the data member corresponding to different Properties and initializing default values
  30. private int miBorderWidth = IDEFAULT_BORDERWIDTH; // BorderWidth Property
  31. private Color moGradientTopColor = ODEFAULT_GRADIENTTOPCOLOR; // GradientTopColor Property
  32. private Color moGradientBottomColor = ODEFAULT_GRADIENTBOTTOMCOLOR; // GradientBottomColor Property
  33. private Color moHeadingTextColor = ODEFAULT_HEADINGTEXTCOLOR; // HeaderTextColor Property
  34. private Color moInteriorTopColor = ODEFAULT_INTERIORTOPCOLOR; // InteriorTopColor Property
  35. private Color moInteriorBottomColor = ODEFAULT_INTERIORBOTTOMCOLOR; // InteriorBottomColor Property
  36. private Color moShadowColor = ODEFAULT_SHADOWCOLOR; // ShadowColor Property
  37. #endregion
  38. #region Protected Members
  39. protected Size mosizeBorderPixelIndent; // Size of the radius of the curves at the corners
  40. protected SizeF moTextSize; // Size(In Floating Point) of the text in pixels based on the font
  41. // This property defines the border within which the whole control is to be drawn.
  42. protected Rectangle BorderRectangle
  43. {
  44. get
  45. {
  46. Rectangle rc = this.ClientRectangle; // We reduce the size of drawing to show everything properly.
  47. return new Rectangle(1, 1, rc.Width - 3, rc.Height - 3);
  48. }
  49. }
  50. // This property defines the color of shadow of the control
  51. public Color ShadowColor
  52. {
  53. get
  54. {
  55. return moShadowColor;
  56. }
  57. set
  58. {
  59. moShadowColor = value;
  60. this.Invalidate();
  61. }
  62. }
  63. // This property defines the color of the header text
  64. public Color FontColor
  65. {
  66. get
  67. {
  68. return moHeadingTextColor;
  69. }
  70. set
  71. {
  72. moHeadingTextColor = value;
  73. }
  74. }
  75. // This property defines the Top Color of the BorderGradient
  76. Color IGradientBorderColor.BorderTopColor
  77. {
  78. get
  79. {
  80. return moGradientTopColor;
  81. }
  82. set
  83. {
  84. moGradientTopColor = value;
  85. }
  86. }
  87. // This property defines the Bottom Color of the BorderGradient
  88. Color IGradientBorderColor.BorderBottomColor
  89. {
  90. get
  91. {
  92. return moGradientBottomColor;
  93. }
  94. set
  95. {
  96. moGradientBottomColor = value;
  97. }
  98. }
  99. // This property defines the Top Color of the Background Gradient
  100. Color IGradientBackgroundColor.BackgroundTopColor
  101. {
  102. get
  103. {
  104. return moInteriorTopColor;
  105. }
  106. set
  107. {
  108. moInteriorTopColor = value;
  109. }
  110. }
  111. // This property defines the Bottom Color of the Background Gradient
  112. Color IGradientBackgroundColor.BackgroundBottomColor
  113. {
  114. get
  115. {
  116. return moInteriorBottomColor;
  117. }
  118. set
  119. {
  120. moInteriorBottomColor = value;
  121. }
  122. }
  123. #endregion
  124. #region Public Property
  125. // The colorscheme property which is to be implemented by the Child Classes
  126. public abstract EnmColorScheme ColorScheme{get;set;}
  127. // The BorderWidth Values are accessed and intialised using this property
  128. public int BorderWidth
  129. {
  130. get
  131. {
  132. return miBorderWidth;
  133. }
  134. set
  135. {
  136. miBorderWidth = value;
  137. this.Invalidate();
  138. }
  139. }
  140. #endregion
  141. #region Overridable Properties
  142. // This property is being used in the OnPaint Method to paint the border
  143. protected virtual Brush InteriorRegionPathBrush
  144. {
  145. get
  146. {
  147. // Brush of LinearGradient type is created to draw gradient
  148. System.Drawing.Drawing2D.LinearGradientBrush brush=
  149. new System.Drawing.Drawing2D.LinearGradientBrush(this.ClientRectangle,
  150. moGradientTopColor,
  151. moGradientBottomColor,
  152. LinearGradientMode.Vertical);
  153. // Blend is used to define the blending method for the gradient
  154. Blend blend=new Blend();
  155. blend.Factors = IARR_RELATIVEINTENSITIES;
  156. blend.Positions = IARR_RELATIVEPOSITIONS;
  157. brush.Blend = blend;
  158. return brush;
  159. }
  160. }
  161. // This Property is used in the OnPaint Method to define the region property of the control
  162. protected virtual GraphicsPath ExteriorRegionPath
  163. {
  164. get
  165. {
  166. Rectangle oRectangle = new Rectangle(this.BorderRectangle.X, this.BorderRectangle.Y, this.BorderRectangle.Width + 3, this.BorderRectangle.Height + 3);
  167. Size oSize = new Size(mosizeBorderPixelIndent.Width + 2, mosizeBorderPixelIndent.Height + 2);
  168. return this.GetRoundedRectanglarPath(oRectangle, oSize);
  169. }
  170. }
  171. // This property is Used in the OnPaint Method to define path to draw the control
  172. protected virtual GraphicsPath InteriorRegionPath
  173. {
  174. get
  175. {
  176. Rectangle oRectangle = new Rectangle(this.BorderRectangle.X + 1, this.BorderRectangle.Y + 1, this.BorderRectangle.Width - 2, this.BorderRectangle.Height - 2);
  177. Size oSize = new Size(mosizeBorderPixelIndent.Width - 2, mosizeBorderPixelIndent.Height - 2);
  178. return this.GetRoundedRectanglarPath(oRectangle, oSize);
  179. }
  180. }
  181. #endregion
  182. public BaseContainer():base()
  183. {
  184. // This method is to specify to the OS that this control has its own OnPaint Method and
  185. // to use it. The double buffering is used so that the control does not flicker when the
  186. // Invalidate method is called.
  187. this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.DoubleBuffer,true);
  188. mosizeBorderPixelIndent = ODEFAULT_SIZEBORDERPIXELINDET;
  189. }
  190. #region Overridable Methods
  191. // This procedure draws the Shadows for the outer Borders and gets called from OnPaint Method
  192. protected virtual void DrawBorder(Graphics aoGraphics, Rectangle aoRectangle)
  193. {
  194. Pen oPen;
  195. Size oSize = new Size(mosizeBorderPixelIndent.Width, mosizeBorderPixelIndent.Height);
  196. Rectangle oRectangle = new Rectangle(aoRectangle.X, aoRectangle.Y, aoRectangle.Width, aoRectangle.Height);
  197. SizeF szText = aoGraphics.MeasureString(this.Text, this.Font);
  198. // We are looping 3 times for a 3 pixel wide shadow.
  199. for(int i = 0;i<3;i++)
  200. {
  201. // Creates a pen to draw Lines and Arcs Dark To Light
  202. oPen = new Pen(Color.FromArgb((2 - i + 1) * 64, moShadowColor));
  203. // Draws a shadow arc for the Top Right corner
  204. aoGraphics.DrawArc(oPen, oRectangle.Right - oSize.Width,
  205. oRectangle.Top + 2,oSize.Width,
  206. oSize.Height, 270, 90);
  207. // Draws a vertical shadow line for the right side
  208. aoGraphics.DrawLine(oPen, oRectangle.Right, oRectangle.Top + (Single)(oSize.Height / 2),
  209. oRectangle.Right, oRectangle.Bottom - (Single)(oSize.Height / 2));
  210. // Draws a shadow arc for bottom right corner
  211. aoGraphics.DrawArc(oPen, oRectangle.Right - oSize.Width,
  212. oRectangle.Bottom - oSize.Height,
  213. oSize.Width, oSize.Height, 0, 90);
  214. // Draws a horizontal shadow line for the bottom
  215. aoGraphics.DrawLine(oPen, oRectangle.Right - (Single)(oSize.Width / 2),
  216. oRectangle.Bottom, oRectangle.Left + (Single)(oSize.Width / 2),
  217. oRectangle.Bottom);
  218. // Creates a pen to draw lines and arcs Light to Dark
  219. oPen = new Pen(Color.FromArgb((2 - i) * 127, moShadowColor));
  220. // Draw a shadow arc for the bottom left corner
  221. aoGraphics.DrawArc(oPen, oRectangle.Left + 2, oRectangle.Bottom - oSize.Height,
  222. oSize.Width, oSize.Height, 90, 90);
  223. // Increasing the Rectangles X and Y position
  224. oRectangle.X += 1;
  225. oRectangle.Y += 1;
  226. // Reducing Height and width of the rectangle
  227. oRectangle.Width -= 2;
  228. oRectangle.Height -= 2;
  229. // Reducing the radius size of the arcs to draw the arcs properly
  230. oSize.Height -= 2;
  231. oSize.Width -= 2;
  232. }
  233. }
  234. // This Method is called from OnPaint Method to draw the Interior Part
  235. protected virtual void DrawInterior(Graphics aoGraphics)
  236. {
  237. // Create rectangle to draw interior
  238. Rectangle oRcInterior = new Rectangle(this.BorderRectangle.X + miBorderWidth + 1, this.BorderRectangle.Y + 12 + miBorderWidth, this.BorderRectangle.Width - (miBorderWidth * 2), this.BorderRectangle.Height - (12 + miBorderWidth * 2));
  239. SolidBrush oSolidBrush;
  240. for(int Index = 1;Index>=0; Index --)
  241. {
  242. // Define Shadow Brushes Dark to Light
  243. oSolidBrush = new SolidBrush(Color.FromArgb(127 * (2 - Index), moShadowColor));
  244. Pen oPen = new Pen(oSolidBrush);
  245. // Draws vertical line on Left side
  246. aoGraphics.DrawLine(oPen, oRcInterior.X, oRcInterior.Y, oRcInterior.X, oRcInterior.Bottom);
  247. // Draws horizontal lines on the top
  248. aoGraphics.DrawLine(oPen, oRcInterior.X, oRcInterior.Y, oRcInterior.Right, oRcInterior.Y);
  249. // Increasing the X and Y postion of the rectangle
  250. oRcInterior.X += 1;
  251. oRcInterior.Y += 1;
  252. // Reducing the height and width of the rectangle
  253. oRcInterior.Width -= 2;
  254. oRcInterior.Height -= 2;
  255. }
  256. // Brush of LinearGradient type is created to draw gradient
  257. LinearGradientBrush oLinearGradient = new LinearGradientBrush(oRcInterior,
  258. moInteriorTopColor,
  259. moInteriorBottomColor,
  260. LinearGradientMode.Vertical);
  261. // Blend is used to define the blend of the gradient
  262. Blend oBlend = new Blend();
  263. oBlend.Factors = IARR_RELATIVEINTENSITIES;
  264. oBlend.Positions = IARR_RELATIVEPOSITIONS;
  265. oLinearGradient.Blend = oBlend;
  266. // Fill the rectangle using Gradient Brush created above
  267. aoGraphics.FillRectangle(oLinearGradient, oRcInterior);
  268. }
  269. #endregion
  270. #region Private methods
  271. // This function is used to get Rectangular GraphicsPath with Rounded Corner
  272. private GraphicsPath GetRoundedRectanglarPath(Rectangle aoRectangle, Size aoSize)
  273. {
  274. GraphicsPath oExteriorGraphicPath = new GraphicsPath();
  275. // Add top horizontal line to the Graphics Path Object
  276. oExteriorGraphicPath.AddLine(aoRectangle.Left + (Single)(aoSize.Height / 2), aoRectangle.Top, aoRectangle.Right - (Single)(aoSize.Height / 2), aoRectangle.Top);
  277. // Add arc for the top right corner curve to the Graphics Path object
  278. oExteriorGraphicPath.AddArc(aoRectangle.Right - aoSize.Width, aoRectangle.Top, aoSize.Width, aoSize.Height, 270, 90);
  279. // Add right vertical line to the Graphics Path object
  280. oExteriorGraphicPath.AddLine(aoRectangle.Right, aoRectangle.Top + aoSize.Height, aoRectangle.Right, aoRectangle.Bottom - (Single)(aoSize.Height / 2));
  281. // Add the bottom right corner curve to the Graphics object
  282. oExteriorGraphicPath.AddArc(aoRectangle.Right - aoSize.Width, aoRectangle.Bottom - aoSize.Height, aoSize.Width, aoSize.Height, 0, 90);
  283. // Add the bottom horizontal line to the Graphics Path object
  284. oExteriorGraphicPath.AddLine(aoRectangle.Right - (Single)(aoSize.Width / 2), aoRectangle.Bottom, aoRectangle.Left + (Single)(aoSize.Width / 2), aoRectangle.Bottom);
  285. // Add arc for the bottom left curve to the Graphics object
  286. oExteriorGraphicPath.AddArc(aoRectangle.Left, aoRectangle.Bottom - aoSize.Height, aoSize.Width, aoSize.Height, 90, 90);
  287. // Add left vertical line to the Graphics Path object
  288. oExteriorGraphicPath.AddLine(aoRectangle.Left, aoRectangle.Bottom - (Single)(aoSize.Height / 2), aoRectangle.Left, aoRectangle.Top + (Single)(aoSize.Height / 2));
  289. // Add arc for the top left curve to the Graphics object
  290. oExteriorGraphicPath.AddArc(aoRectangle.Left, aoRectangle.Top, aoSize.Width, aoSize.Height, 180, 90);
  291. return oExteriorGraphicPath;
  292. }
  293. #endregion
  294. #region Overriden Events
  295. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  296. {
  297. // Get the size of the string in pixels for the string for a font
  298. this.moTextSize = e.Graphics.MeasureString(this.Text, this.Font);
  299. // Original Smoothing is Saved and Smoothing mode mode is change to AntiAlias
  300. SmoothingMode oldSmooting = e.Graphics.SmoothingMode;
  301. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  302. // Draws shadow border for the control
  303. DrawBorder(e.Graphics, this.BorderRectangle);
  304. // Fill the rectangle that represents the border with gradient
  305. e.Graphics.FillPath(this.InteriorRegionPathBrush, this.InteriorRegionPath);
  306. // Draws the gradient background with shadows
  307. DrawInterior(e.Graphics);
  308. // Defines string format to center the string
  309. StringFormat oStringFormat = new StringFormat();
  310. // The rectangle where the text is to be drawn
  311. RectangleF oRectangleF =
  312. new RectangleF(this.BorderRectangle.X + (Single)(this.mosizeBorderPixelIndent.Width / 2) + 8,
  313. this.BorderRectangle.Y + 2,
  314. moTextSize.Width + (Single)(this.mosizeBorderPixelIndent.Width / 2),
  315. moTextSize.Height);
  316. // Drawing the string in the rectangle
  317. e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(moHeadingTextColor), oRectangleF, oStringFormat);
  318. // Reseting the smoothingmode back to original for OS purposes.
  319. e.Graphics.SmoothingMode = oldSmooting;
  320. // Using the graphics path property regionpath to define the non rectangular shape for the control
  321. this.Region = new Region(this.ExteriorRegionPath);
  322. }
  323. #endregion
  324. }
  325. }