PictureBox.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // Authors:
  23. // Jackson Harper ([email protected])
  24. //
  25. using System;
  26. using System.Drawing;
  27. using System.Drawing.Imaging;
  28. namespace System.Windows.Forms {
  29. public class PictureBox : Control {
  30. private Image image;
  31. private PictureBoxSizeMode size_mode;
  32. private BorderStyle border_style;
  33. private bool redraw;
  34. private bool recalc;
  35. private EventHandler frame_handler;
  36. public PictureBox ()
  37. {
  38. redraw = true;
  39. recalc = true;
  40. }
  41. public PictureBoxSizeMode SizeMode {
  42. get { return size_mode; }
  43. set {
  44. if (size_mode == value)
  45. return;
  46. size_mode = value;
  47. UpdateSize ();
  48. Redraw (true);
  49. Invalidate ();
  50. }
  51. }
  52. public Image Image {
  53. get { return image; }
  54. set {
  55. StopAnimation ();
  56. image = value;
  57. UpdateSize ();
  58. if (ImageAnimator.CanAnimate (image)) {
  59. frame_handler = new EventHandler (OnAnimateImage);
  60. ImageAnimator.Animate (image, frame_handler);
  61. }
  62. Redraw (true);
  63. Invalidate ();
  64. }
  65. }
  66. public BorderStyle BorderStyle {
  67. get { return border_style; }
  68. set {
  69. border_style = value;
  70. Redraw (true);
  71. }
  72. }
  73. public new bool CausesValidation {
  74. get { return base.CausesValidation; }
  75. set { base.CausesValidation = value; }
  76. }
  77. public new ImeMode ImeMode {
  78. get { return base.ImeMode; }
  79. set { base.ImeMode = value; }
  80. }
  81. public override RightToLeft RightToLeft {
  82. get { return base.RightToLeft; }
  83. set { base.RightToLeft = value; }
  84. }
  85. public new int TabIndex {
  86. get { return base.TabIndex; }
  87. set { base.TabIndex = value; }
  88. }
  89. public new bool TabStop {
  90. get { return base.TabStop; }
  91. set { base.TabStop = value; }
  92. }
  93. public override string Text {
  94. get { return base.Text; }
  95. set { base.Text = value; }
  96. }
  97. protected override CreateParams CreateParams {
  98. get {
  99. CreateParams createParams = base.CreateParams;
  100. createParams.ClassName = String.Empty;
  101. createParams.Style = (int) (WindowStyles.WS_CHILD |
  102. WindowStyles.WS_VISIBLE);
  103. return createParams;
  104. }
  105. }
  106. protected override ImeMode DefaultImeMode {
  107. get { return base.DefaultImeMode; }
  108. }
  109. public override Font Font {
  110. get { return base.Font; }
  111. set { base.Font = value; }
  112. }
  113. public override Color ForeColor {
  114. get { return base.ForeColor; }
  115. set { base.ForeColor = value; }
  116. }
  117. protected override Size DefaultSize {
  118. get { return new Size (100, 50); }
  119. }
  120. protected override void Dispose (bool disposing)
  121. {
  122. if (image != null) {
  123. StopAnimation ();
  124. image.Dispose ();
  125. image = null;
  126. }
  127. base.Dispose (disposing);
  128. }
  129. protected override void OnPaint (PaintEventArgs pe)
  130. {
  131. if (this.Width <= 0 || this.Height <= 0 || this.Visible == false)
  132. return;
  133. Draw ();
  134. pe.Graphics.DrawImage (this.ImageBuffer, pe.ClipRectangle, pe.ClipRectangle, GraphicsUnit.Pixel);
  135. }
  136. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  137. {
  138. if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
  139. width = image.Width;
  140. height = image.Height;
  141. }
  142. base.SetBoundsCore (x, y, width, height, specified);
  143. }
  144. private void StopAnimation ()
  145. {
  146. if (frame_handler == null)
  147. return;
  148. ImageAnimator.StopAnimate (image, frame_handler);
  149. frame_handler = null;
  150. }
  151. private void UpdateSize ()
  152. {
  153. if (image == null)
  154. return;
  155. if (size_mode == PictureBoxSizeMode.AutoSize)
  156. ClientSize = image.Size;
  157. }
  158. private void Redraw (bool recalc)
  159. {
  160. redraw = true;
  161. this.recalc = recalc;
  162. }
  163. private void OnAnimateImage (object sender, EventArgs e)
  164. {
  165. // This is called from a worker thread,BeginInvoke is used
  166. // so the control is updated from the correct thread
  167. BeginInvoke (new EventHandler (UpdateAnimatedImage), new object [] { this, e });
  168. }
  169. private void UpdateAnimatedImage (object sender, EventArgs e)
  170. {
  171. ImageAnimator.UpdateFrames (image);
  172. Redraw (false);
  173. Refresh ();
  174. }
  175. [MonoTODO ("Borders and stuff, and move into the Theme")]
  176. private void Draw ()
  177. {
  178. if (redraw) {
  179. DeviceContext.DrawImage (image, 0, 0);
  180. }
  181. redraw = false;
  182. }
  183. }
  184. }