ProgressBar.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (C) 2004 Novell, Inc.
  21. //
  22. // Autors:
  23. // Jordi Mas i Hernandez [email protected]
  24. //
  25. //
  26. // $Revision: 1.3 $
  27. // $Modtime: $
  28. // $Log: ProgressBar.cs,v $
  29. // Revision 1.3 2004/07/09 17:25:23 pbartok
  30. // - Removed usage of Rectangle for drawing. Miguel pointed out it's faster
  31. //
  32. // Revision 1.2 2004/07/09 17:17:46 miguel
  33. // 2004-07-09 Miguel de Icaza <[email protected]>
  34. //
  35. // * ProgressBar.cs: Fixed spelling for `block'
  36. //
  37. // drawProgressBar: renamed to `DrawProgressBar' to follow the coding
  38. // style guidelines.
  39. //
  40. // Avoid using the += on rect.X, that exposed a bug in the compiler.
  41. //
  42. // Revision 1.1 2004/07/09 05:21:25 pbartok
  43. // - Initial check-in
  44. //
  45. //
  46. using System.Drawing;
  47. using System.ComponentModel;
  48. using System.Drawing.Imaging;
  49. using System.Drawing.Drawing2D;
  50. namespace System.Windows.Forms
  51. {
  52. /* Scroll bar Theme painter class*/
  53. #region ThemePainter support
  54. internal class ThemePainter_ProgressBar
  55. {
  56. static private Color shadow = Color.FromArgb (255, 172, 168, 153);
  57. static private Color light = Color.FromArgb (255, 255, 255, 255);
  58. static private SolidBrush br_shadow = new SolidBrush (shadow);
  59. static private SolidBrush br_light = new SolidBrush (light);
  60. private static SolidBrush br_main = new SolidBrush (Color.FromArgb (255, 236, 233, 216));
  61. private static SolidBrush br_bar = new SolidBrush (Color.FromArgb (255, 49, 106, 197));
  62. private static int space_betweenblocks = 2;
  63. /* Draw a progress bar */
  64. static public void DrawProgressBar (Graphics dc, Rectangle area,
  65. Rectangle client_area, int barpos_pixels, int block_width)
  66. {
  67. int increment = block_width + space_betweenblocks;
  68. int x = client_area.X;
  69. /* Background*/
  70. dc.FillRectangle (br_main, area);
  71. /* Draw background*/
  72. while ((x - client_area.X) < barpos_pixels) {
  73. dc.FillRectangle (br_bar, x, client_area.Y, bloc_width, client_area.Height);
  74. x = x + bloc_width + space_betweenblocs;
  75. }
  76. /* Draw border */
  77. dc.FillRectangle (br_shadow, area.X, area.Y, area.Width, 1);
  78. dc.FillRectangle (br_shadow, area.X, area.Y, 1, area.Height);
  79. dc.FillRectangle (br_light, area.X, area.Y + area.Height - 1, area.Width, 1);
  80. dc.FillRectangle (br_light, area.X + area.Width - 1, area.Y, 1, area.Height);
  81. }
  82. }
  83. #endregion // ThemePainter support
  84. public sealed class ProgressBar : Control
  85. {
  86. #region Local Variables
  87. private int maximum;
  88. private int minimum;
  89. private int step;
  90. private int val;
  91. private Bitmap bmp_mem = null;
  92. private Graphics dc_mem = null;
  93. private Rectangle paint_area = new Rectangle ();
  94. private Rectangle client_area = new Rectangle ();
  95. #endregion // Local Variables
  96. #region Public Constructors
  97. public ProgressBar()
  98. {
  99. maximum = 100;
  100. minimum = 0;
  101. step = 10;
  102. val = 0;
  103. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  104. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  105. }
  106. #endregion // Public Constructors
  107. #region Public Instance Properties
  108. public int Maximum {
  109. get {
  110. return maximum;
  111. }
  112. set {
  113. if (value < 0)
  114. throw new ArgumentException(
  115. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  116. maximum = value;
  117. Invalidate ();
  118. }
  119. }
  120. public int Minimum {
  121. get {
  122. return minimum;
  123. }
  124. set {
  125. if (value < 0)
  126. throw new ArgumentException(
  127. string.Format("Value '{0}' must be greater than or equal to 0.", value ));
  128. minimum = value;
  129. Invalidate ();
  130. }
  131. }
  132. public int Step {
  133. get { return step; }
  134. set {
  135. step = value;
  136. Invalidate ();
  137. }
  138. }
  139. public int Value {
  140. get {
  141. return val;
  142. }
  143. set {
  144. if (value < Minimum || value > Maximum)
  145. throw new ArgumentException(
  146. string.Format("'{0}' is not a valid value for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'", value));
  147. val = value;
  148. Invalidate ();
  149. }
  150. }
  151. #endregion // Public Instance Properties
  152. #region Protected Instance Properties
  153. protected override CreateParams CreateParams {
  154. get {
  155. CreateParams createParams = base.CreateParams;
  156. createParams.ClassName = XplatUI.DefaultClassName;
  157. createParams.Style = (int) (
  158. WindowStyles.WS_CHILD |
  159. WindowStyles.WS_VISIBLE);
  160. return createParams;
  161. }
  162. }
  163. protected override ImeMode DefaultImeMode {
  164. get { return ImeMode.Disable; }
  165. }
  166. protected override Size DefaultSize {
  167. get { return new Size(100, 23); }
  168. }
  169. #endregion // Protected Instance Properties
  170. #region Public Instance Methods
  171. public void Increment (int value)
  172. {
  173. int newValue = Value + value;
  174. if (newValue < Minimum)
  175. newValue = Minimum;
  176. if (newValue > Maximum)
  177. newValue = Maximum;
  178. Value = newValue;
  179. Invalidate ();
  180. }
  181. public void PerformStep () {
  182. if (Value >= Maximum)
  183. return;
  184. Value = Value + Step;
  185. Invalidate ();
  186. }
  187. #endregion // Public Instance Methods
  188. private void UpdateAreas ()
  189. {
  190. paint_area.X = paint_area.Y = 0;
  191. paint_area.Width = Width;
  192. paint_area.Height = Height;
  193. client_area.X = client_area.Y = 2;
  194. client_area.Width = Width - 4;
  195. client_area.Height = Height - 4;
  196. }
  197. protected override void OnResize (EventArgs e)
  198. {
  199. //Console.WriteLine ("Onresize");
  200. base.OnResize (e);
  201. if (Width <= 0 || Height <= 0)
  202. return;
  203. UpdateAreas ();
  204. /* Area for double buffering */
  205. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  206. dc_mem = Graphics.FromImage (bmp_mem);
  207. }
  208. protected override void OnHandleCreated (EventArgs e)
  209. {
  210. base.OnHandleCreated(e);
  211. //Console.WriteLine ("OnHandleCreated");
  212. UpdateAreas ();
  213. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  214. dc_mem = Graphics.FromImage (bmp_mem);
  215. draw ();
  216. }
  217. public override string ToString()
  218. {
  219. return string.Format ("{0}, Minimum: {1}, Maximum: {2}, Value: {3}",
  220. GetType().FullName.ToString (),
  221. Maximum.ToString (),
  222. Minimum.ToString (),
  223. Value.ToString () );
  224. }
  225. /* Disable background painting to avoid flickering, since we do our painting*/
  226. protected override void OnPaintBackground (PaintEventArgs pevent)
  227. {
  228. // None
  229. }
  230. private void draw ()
  231. {
  232. int block_width, barpos_pixels;
  233. int steps = (Maximum - Minimum) / step;
  234. block_width = ((client_area.Height) * 2 ) / 3;
  235. barpos_pixels = ((Value - Minimum) * client_area.Width) / (Maximum - Minimum);
  236. //Console.WriteLine ("draw block witdh:{0} barpos: {1}", block_width, barpos_pixels);
  237. //Console.WriteLine ("draw Max {0} Min {1} Value {2}",
  238. // Maximum, Minimum, Value);
  239. ThemePainter_ProgressBar.DrawProgressBar (dc_mem, paint_area, client_area, barpos_pixels,
  240. block_width);
  241. }
  242. protected override void OnPaint (PaintEventArgs pevent)
  243. {
  244. if (Width <= 0 || Height <= 0 || Visible == false)
  245. return;
  246. /* Copies memory drawing buffer to screen*/
  247. draw();
  248. pevent.Graphics.DrawImage (bmp_mem, 0, 0);
  249. }
  250. }
  251. }