IconBox.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication
  11. {
  12. internal class IconBox : UserControl
  13. {
  14. private Bitmap renderSurface = null;
  15. private Bitmap icon = null;
  16. public Bitmap Icon
  17. {
  18. get
  19. {
  20. return icon;
  21. }
  22. set
  23. {
  24. if (value == null)
  25. {
  26. value = new Bitmap(1, 1);
  27. using (Graphics g = Graphics.FromImage(value))
  28. {
  29. g.Clear(Color.Transparent);
  30. }
  31. }
  32. icon = value;
  33. if (renderSurface != null)
  34. {
  35. renderSurface.Dispose();
  36. }
  37. renderSurface = null;
  38. Invalidate();
  39. }
  40. }
  41. protected override void OnPaint(PaintEventArgs e)
  42. {
  43. e.Graphics.Clear(this.BackColor);
  44. Rectangle srcBounds = new Rectangle(new Point(0, 0), this.icon.Size);
  45. Rectangle dstBounds = new Rectangle(new Point(0, 0), this.ClientSize);
  46. e.Graphics.DrawImage(this.Icon, dstBounds, srcBounds, GraphicsUnit.Pixel);
  47. base.OnPaint(e);
  48. }
  49. public IconBox()
  50. {
  51. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  52. // This call is required by the Windows.Forms Form Designer.
  53. InitializeComponent();
  54. this.ResizeRedraw = true;
  55. }
  56. #region Component Designer generated code
  57. /// <summary>
  58. /// Required method for Designer support - do not modify
  59. /// the contents of this method with the code editor.
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. }
  64. #endregion
  65. }
  66. }