Added a simple way to customize the sensor gadget background. If any of the files gadget_background.png, gadget_image.png, gadget_foreground.png, gadget_bar_background.png or gadget_bar_foreground.png is present in the working directory then these files are used instead of the built in files.
authormoel.mich
Wed, 22 Feb 2012 23:32:06 +0000
changeset 3422486df89b9e5
parent 341 5172a92c5c20
child 343 46b6b567f14f
Added a simple way to customize the sensor gadget background. If any of the files gadget_background.png, gadget_image.png, gadget_foreground.png, gadget_bar_background.png or gadget_bar_foreground.png is present in the working directory then these files are used instead of the built in files.
GUI/SensorGadget.cs
     1.1 --- a/GUI/SensorGadget.cs	Thu Feb 16 22:10:30 2012 +0000
     1.2 +++ b/GUI/SensorGadget.cs	Wed Feb 22 23:32:06 2012 +0000
     1.3 @@ -40,6 +40,7 @@
     1.4  using System.Drawing;
     1.5  using System.Drawing.Imaging;
     1.6  using System.Windows.Forms;
     1.7 +using System.IO;
     1.8  using OpenHardwareMonitor.Hardware;
     1.9  
    1.10  namespace OpenHardwareMonitor.GUI {
    1.11 @@ -48,8 +49,10 @@
    1.12      private UnitManager unitManager;
    1.13  
    1.14      private Image back = Utilities.EmbeddedResources.GetImage("gadget.png");
    1.15 +    private Image image = null;
    1.16 +    private Image fore = null;
    1.17      private Image barBack = Utilities.EmbeddedResources.GetImage("barback.png");
    1.18 -    private Image barBlue = Utilities.EmbeddedResources.GetImage("barblue.png");
    1.19 +    private Image barFore = Utilities.EmbeddedResources.GetImage("barblue.png");
    1.20      private const int topBorder = 6;
    1.21      private const int bottomBorder = 7;
    1.22      private const int leftBorder = 6;
    1.23 @@ -103,6 +106,42 @@
    1.24        this.alignRightStringFormat.Alignment = StringAlignment.Far;
    1.25        this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;
    1.26  
    1.27 +      if (File.Exists("gadget_background.png")) {
    1.28 +        try { 
    1.29 +          Image newBack = new Bitmap("gadget_background.png");
    1.30 +          back.Dispose();
    1.31 +          back = newBack;
    1.32 +        } catch { }
    1.33 +      }
    1.34 +
    1.35 +      if (File.Exists("gadget_image.png")) {
    1.36 +        try {
    1.37 +          image = new Bitmap("gadget_image.png"); 
    1.38 +        } catch {}
    1.39 +      }
    1.40 +
    1.41 +      if (File.Exists("gadget_foreground.png")) {
    1.42 +        try {
    1.43 +          fore = new Bitmap("gadget_foreground.png"); 
    1.44 +        } catch { }
    1.45 +      }
    1.46 +
    1.47 +      if (File.Exists("gadget_bar_background.png")) {
    1.48 +        try {
    1.49 +          Image newBarBack = new Bitmap("gadget_bar_background.png");
    1.50 +          barBack.Dispose();
    1.51 +          barBack = newBarBack;
    1.52 +        } catch { }
    1.53 +      }
    1.54 +
    1.55 +      if (File.Exists("gadget_bar_foreground.png")) {
    1.56 +        try {
    1.57 +          Image newBarColor = new Bitmap("gadget_bar_foreground.png");
    1.58 +          barFore.Dispose();
    1.59 +          barFore = newBarColor;
    1.60 +        } catch { }
    1.61 +      }
    1.62 +
    1.63        this.Location = new Point(
    1.64          settings.GetValue("sensorGadget.Location.X", 100),
    1.65          settings.GetValue("sensorGadget.Location.Y", 100)); 
    1.66 @@ -245,8 +284,8 @@
    1.67        back.Dispose();
    1.68        back = null;
    1.69  
    1.70 -      barBlue.Dispose();
    1.71 -      barBlue = null;
    1.72 +      barFore.Dispose();
    1.73 +      barFore = null;
    1.74  
    1.75        barBack.Dispose();
    1.76        barBack = null;
    1.77 @@ -254,6 +293,16 @@
    1.78        background.Dispose();
    1.79        background = null;
    1.80  
    1.81 +      if (image != null) {
    1.82 +        image.Dispose();
    1.83 +        image = null;
    1.84 +      }
    1.85 +
    1.86 +      if (fore != null) {
    1.87 +        fore.Dispose();
    1.88 +        fore = null;
    1.89 +      }
    1.90 +
    1.91        base.Dispose();
    1.92      }
    1.93  
    1.94 @@ -397,6 +446,33 @@
    1.95        this.Size = new Size(width, y);
    1.96      }
    1.97  
    1.98 +    private void DrawImageWidthBorder(Graphics g, int width, int height, 
    1.99 +      Image back, int t, int b, int l, int r) 
   1.100 +    {
   1.101 +      GraphicsUnit u = GraphicsUnit.Pixel;
   1.102 +
   1.103 +      g.DrawImage(back, new Rectangle(0, 0, l, t),
   1.104 +            new Rectangle(0, 0, l, t), u);
   1.105 +      g.DrawImage(back, new Rectangle(l, 0, width - l - r, t),
   1.106 +        new Rectangle(l, 0, back.Width - l - r, t), u);
   1.107 +      g.DrawImage(back, new Rectangle(width - r, 0, r, t),
   1.108 +        new Rectangle(back.Width - r, 0, r, t), u);
   1.109 +
   1.110 +      g.DrawImage(back, new Rectangle(0, t, l, height - t - b),
   1.111 +        new Rectangle(0, t, l, back.Height - t - b), u);
   1.112 +      g.DrawImage(back, new Rectangle(l, t, width - l - r, height - t - b),
   1.113 +        new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
   1.114 +      g.DrawImage(back, new Rectangle(width - r, t, r, height - t - b),
   1.115 +        new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
   1.116 +
   1.117 +      g.DrawImage(back, new Rectangle(0, height - b, l, b),
   1.118 +        new Rectangle(0, back.Height - b, l, b), u);
   1.119 +      g.DrawImage(back, new Rectangle(l, height - b, width - l - r, b),
   1.120 +        new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
   1.121 +      g.DrawImage(back, new Rectangle(width - r, height - b, r, b),
   1.122 +        new Rectangle(back.Width - r, back.Height - b, r, b), u);
   1.123 +    }
   1.124 +
   1.125      private void DrawBackground(Graphics g) {
   1.126        int w = Size.Width;
   1.127        int h = Size.Height;      
   1.128 @@ -407,33 +483,36 @@
   1.129          background = new Bitmap(w, h, PixelFormat.Format32bppPArgb);
   1.130          using (Graphics graphics = Graphics.FromImage(background)) {
   1.131  
   1.132 -          int t = topBorder;
   1.133 -          int b = bottomBorder;
   1.134 -          int l = leftBorder;
   1.135 -          int r = rightBorder;
   1.136 +          DrawImageWidthBorder(graphics, w, h, back, topBorder, bottomBorder, 
   1.137 +            leftBorder, rightBorder);    
   1.138 +      
   1.139 +          if (fore != null)
   1.140 +            DrawImageWidthBorder(graphics, w, h, fore, topBorder, bottomBorder,
   1.141 +            leftBorder, rightBorder);
   1.142  
   1.143 -          GraphicsUnit u = GraphicsUnit.Pixel;
   1.144 +          if (image != null) {
   1.145 +            int width = w - leftBorder - rightBorder;
   1.146 +            int height = h - topBorder - bottomBorder;
   1.147 +            float xRatio = width / (float)image.Width;
   1.148 +            float yRatio = height / (float)image.Height;
   1.149 +            float destWidth, destHeight;
   1.150 +            float xOffset, yOffset;
   1.151 +            if (xRatio < yRatio) {
   1.152 +              destWidth = width;
   1.153 +              destHeight = image.Height * xRatio;
   1.154 +              xOffset = 0;
   1.155 +              yOffset = 0.5f * (height - destHeight);
   1.156 +            } else {
   1.157 +              destWidth = image.Width * yRatio;
   1.158 +              destHeight = height;
   1.159 +              xOffset = 0.5f * (width - destWidth);
   1.160 +              yOffset = 0;
   1.161 +            }
   1.162  
   1.163 -          graphics.DrawImage(back, new Rectangle(0, 0, l, t),
   1.164 -            new Rectangle(0, 0, l, t), u);
   1.165 -          graphics.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
   1.166 -            new Rectangle(l, 0, back.Width - l - r, t), u);
   1.167 -          graphics.DrawImage(back, new Rectangle(w - r, 0, r, t),
   1.168 -            new Rectangle(back.Width - r, 0, r, t), u);
   1.169 -
   1.170 -          graphics.DrawImage(back, new Rectangle(0, t, l, h - t - b),
   1.171 -            new Rectangle(0, t, l, back.Height - t - b), u);
   1.172 -          graphics.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
   1.173 -            new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
   1.174 -          graphics.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
   1.175 -            new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
   1.176 -
   1.177 -          graphics.DrawImage(back, new Rectangle(0, h - b, l, b),
   1.178 -            new Rectangle(0, back.Height - b, l, b), u);
   1.179 -          graphics.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
   1.180 -            new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
   1.181 -          graphics.DrawImage(back, new Rectangle(w - r, h - b, r, b),
   1.182 -            new Rectangle(back.Width - r, back.Height - b, r, b), u);
   1.183 +            graphics.DrawImage(image,
   1.184 +              new RectangleF(leftBorder + xOffset, topBorder + yOffset, 
   1.185 +                destWidth, destHeight));
   1.186 +          }
   1.187          }
   1.188        }
   1.189  
   1.190 @@ -448,9 +527,9 @@
   1.191          new RectangleF(barBack.Width * progress, 0, 
   1.192            (1 - progress) * barBack.Width, barBack.Height), 
   1.193          GraphicsUnit.Pixel);
   1.194 -      g.DrawImage(barBlue,
   1.195 +      g.DrawImage(barFore,
   1.196          new RectangleF(x, y, width * progress, height),
   1.197 -        new RectangleF(0, 0, progress * barBlue.Width, barBlue.Height),
   1.198 +        new RectangleF(0, 0, progress * barFore.Width, barFore.Height),
   1.199          GraphicsUnit.Pixel);
   1.200      }
   1.201