1111namespace OpenHardwareMonitor . GUI {
1212 public class SensorNotifyIcon : IDisposable {
1313
14- private UnitManager unitManager ;
14+ private readonly UnitManager unitManager ;
1515
16- private ISensor sensor ;
17- private NotifyIconAdv notifyIcon ;
18- private Bitmap bitmap ;
19- private Graphics graphics ;
16+ private readonly ISensor sensor ;
17+ private readonly NotifyIconAdv notifyIcon ;
18+ private readonly Bitmap bitmap ;
19+ private readonly Graphics graphics ;
2020 private Color color ;
2121 private Color darkColor ;
2222 private Brush brush ;
2323 private Brush darkBrush ;
24- private Pen pen ;
25- private Font font ;
26- private Font smallFont ;
24+ private readonly Pen pen ;
25+ private readonly Font font ;
26+ private readonly Font smallFont ;
2727
2828 public SensorNotifyIcon ( SystemTray sensorSystemTray , ISensor sensor ,
2929 PersistentSettings settings , UnitManager unitManager ) {
3030
3131 this . unitManager = unitManager ;
3232 this . sensor = sensor ;
33- this . notifyIcon = new NotifyIconAdv ( ) ;
33+ notifyIcon = new NotifyIconAdv ( ) ;
3434
3535 //todo: set defaultColor depending on the taskbar color
3636 var defaultColor = Color . FromArgb ( 0xff , 0x00 , 0xff , 0xff ) ;
@@ -39,109 +39,97 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
3939 // sensor.SensorType == SensorType.Level)
4040 // defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
4141
42- Color = settings . GetValue ( new Identifier ( sensor . Identifier ,
43- "traycolor" ) . ToString ( ) , defaultColor ) ;
42+ Color = settings . GetValue ( new Identifier ( sensor . Identifier , "traycolor" ) . ToString ( ) , defaultColor ) ;
4443
45- this . pen = new Pen ( Color . FromArgb ( 96 , Color . Black ) ) ;
44+ pen = new Pen ( Color . FromArgb ( 96 , Color . Black ) ) ;
4645
47- ContextMenu contextMenu = new ContextMenu ( ) ;
48- MenuItem hideShowItem = new MenuItem ( "Hide/Show" ) ;
46+ var contextMenu = new ContextMenu ( ) ;
47+ var hideShowItem = new MenuItem ( "Hide/Show" ) ;
4948 hideShowItem . DefaultItem = true ;
50- hideShowItem . Click += delegate ( object obj , EventArgs args ) {
51- sensorSystemTray . SendHideShowCommand ( ) ;
52- } ;
49+ hideShowItem . Click += ( sender , args ) => sensorSystemTray . SendHideShowCommand ( ) ;
5350 contextMenu . MenuItems . Add ( hideShowItem ) ;
5451 contextMenu . MenuItems . Add ( new MenuItem ( "-" ) ) ;
55- MenuItem removeItem = new MenuItem ( "Remove Sensor" ) ;
56- removeItem . Click += delegate ( object obj , EventArgs args ) {
57- sensorSystemTray . Remove ( this . sensor ) ;
58- } ;
52+ var removeItem = new MenuItem ( "Remove Sensor" ) ;
53+ removeItem . Click += ( sender , args ) => sensorSystemTray . Remove ( this . sensor ) ;
5954 contextMenu . MenuItems . Add ( removeItem ) ;
60- MenuItem colorItem = new MenuItem ( "Change Color..." ) ;
61- colorItem . Click += delegate ( object obj , EventArgs args ) {
62- ColorDialog dialog = new ColorDialog ( ) ;
55+ var colorItem = new MenuItem ( "Change Color..." ) ;
56+ colorItem . Click += ( obj , args ) => {
57+ var dialog = new ColorDialog ( ) ;
6358 dialog . Color = Color ;
6459 if ( dialog . ShowDialog ( ) == DialogResult . OK ) {
6560 Color = dialog . Color ;
66- settings . SetValue ( new Identifier ( sensor . Identifier ,
67- "traycolor" ) . ToString ( ) , Color ) ;
61+ settings . SetValue ( new Identifier ( sensor . Identifier , "traycolor" ) . ToString ( ) , Color ) ;
6862 }
6963 } ;
7064 contextMenu . MenuItems . Add ( colorItem ) ;
7165 contextMenu . MenuItems . Add ( new MenuItem ( "-" ) ) ;
72- MenuItem exitItem = new MenuItem ( "Exit" ) ;
73- exitItem . Click += delegate ( object obj , EventArgs args ) {
74- sensorSystemTray . SendExitCommand ( ) ;
75- } ;
66+ var exitItem = new MenuItem ( "Exit" ) ;
67+ exitItem . Click += ( obj , args ) => sensorSystemTray . SendExitCommand ( ) ;
7668 contextMenu . MenuItems . Add ( exitItem ) ;
77- this . notifyIcon . ContextMenu = contextMenu ;
78- this . notifyIcon . DoubleClick += delegate ( object obj , EventArgs args ) {
79- sensorSystemTray . SendHideShowCommand ( ) ;
80- } ;
69+ notifyIcon . ContextMenu = contextMenu ;
70+ notifyIcon . DoubleClick += ( obj , args ) => sensorSystemTray . SendHideShowCommand ( ) ;
8171
8272 // get the default dpi to create an icon with the correct size
8373 float dpiX , dpiY ;
84- using ( Bitmap b = new Bitmap ( 1 , 1 , PixelFormat . Format32bppArgb ) ) {
74+ using ( var b = new Bitmap ( 1 , 1 , PixelFormat . Format32bppArgb ) ) {
8575 dpiX = b . HorizontalResolution ;
8676 dpiY = b . VerticalResolution ;
8777 }
8878
8979 // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
90- int width = ( int ) Math . Round ( 16 * dpiX / 96 ) ;
91- int height = ( int ) Math . Round ( 16 * dpiY / 96 ) ;
80+ var width = ( int ) Math . Round ( 16 * dpiX / 96 ) ;
81+ var height = ( int ) Math . Round ( 16 * dpiY / 96 ) ;
9282
9383 // make sure it does never get smaller than 16x16
9484 width = width < 16 ? 16 : width ;
9585 height = height < 16 ? 16 : height ;
9686
9787 // adjust the font size to the icon size
98- FontFamily family = new FontFamily ( "Segoe UI" ) ; // SystemFonts.MessageBoxFont.FontFamily;
88+ var family = new FontFamily ( "Segoe UI" ) ; // SystemFonts.MessageBoxFont.FontFamily;
9989 float baseSize ;
10090 switch ( family . Name ) {
10191 case "Segoe UI" : baseSize = 15 ; break ;
10292 case "Tahoma" : baseSize = 11 ; break ;
10393 default : baseSize = 12 ; break ;
10494 }
10595
106- this . font = new Font ( family ,
96+ font = new Font ( family ,
10797 baseSize * width / 16.0f , GraphicsUnit . Pixel ) ;
108- this . smallFont = new Font ( family ,
98+ smallFont = new Font ( family ,
10999 0.75f * baseSize * width / 16.0f , GraphicsUnit . Pixel ) ;
110100
111- this . bitmap = new Bitmap ( width , height , PixelFormat . Format32bppArgb ) ;
112- this . graphics = Graphics . FromImage ( this . bitmap ) ;
101+ bitmap = new Bitmap ( width , height , PixelFormat . Format32bppArgb ) ;
102+ graphics = Graphics . FromImage ( bitmap ) ;
113103
114104 if ( Environment . OSVersion . Version . Major > 5 ) {
115- this . graphics . TextRenderingHint = TextRenderingHint . ClearTypeGridFit ;
116- this . graphics . SmoothingMode = SmoothingMode . HighQuality ;
105+ graphics . TextRenderingHint = TextRenderingHint . ClearTypeGridFit ;
106+ graphics . SmoothingMode = SmoothingMode . HighQuality ;
117107 }
118108 }
119109
120- public ISensor Sensor {
121- get { return sensor ; }
122- }
110+ public ISensor Sensor => sensor ;
123111
124112 public Color Color {
125- get { return color ; }
113+ get => color ;
126114 set {
127- this . color = value ;
128- this . darkColor = Color . FromArgb ( 255 ,
129- this . color . R / 3 ,
130- this . color . G / 3 ,
131- this . color . B / 3 ) ;
132- Brush brush = this . brush ;
133- this . brush = new SolidBrush ( this . color ) ;
134- if ( brush != null )
135- brush . Dispose ( ) ;
136- Brush darkBrush = this . darkBrush ;
137- this . darkBrush = new SolidBrush ( this . darkColor ) ;
138- if ( darkBrush != null )
139- darkBrush . Dispose ( ) ;
115+ color = value ;
116+ darkColor = Color . FromArgb ( 255 ,
117+ color . R / 3 ,
118+ color . G / 3 ,
119+ color . B / 3 ) ;
120+ var tmpBrush = brush ;
121+ brush = new SolidBrush ( color ) ;
122+ if ( tmpBrush != null )
123+ tmpBrush . Dispose ( ) ;
124+ var tmpDarkBrush = darkBrush ;
125+ darkBrush = new SolidBrush ( darkColor ) ;
126+ if ( tmpDarkBrush != null )
127+ tmpDarkBrush . Dispose ( ) ;
140128 }
141129 }
142130
143131 public void Dispose ( ) {
144- Icon icon = notifyIcon . Icon ;
132+ var icon = notifyIcon . Icon ;
145133 notifyIcon . Icon = null ;
146134 if ( icon != null )
147135 icon . Dispose ( ) ;
@@ -164,70 +152,79 @@ private string GetString() {
164152
165153 switch ( sensor . SensorType ) {
166154 case SensorType . Voltage :
167- return string . Format ( "{0 :F1}", sensor . Value ) ;
155+ return $ " { sensor . Value : F1} ";
168156 case SensorType . Clock :
169- return string . Format ( "{0:F1}" , 1e-3f * sensor . Value ) ;
157+ return $ " { 1e-3f * sensor . Value : F1 } " ;
170158 case SensorType . Load :
171- return string . Format ( "{0 :F0}", sensor . Value ) ;
159+ return $ " { sensor . Value : F0} ";
172160 case SensorType . Temperature :
173161 if ( unitManager . TemperatureUnit == TemperatureUnit . Fahrenheit )
174- return string . Format ( "{0:F0}" ,
175- UnitManager . CelsiusToFahrenheit ( sensor . Value ) ) ;
162+ return $ "{ UnitManager . CelsiusToFahrenheit ( sensor . Value ) : F0} ";
176163 else
177- return string . Format ( "{0 :F0}", sensor . Value ) ;
164+ return $ " { sensor . Value : F0} ";
178165 case SensorType . Fan :
179- return string . Format ( "{0:F1}" , 1e-3f * sensor . Value ) ;
166+ return $ " { 1e-3f * sensor . Value : F1 } " ;
180167 case SensorType . Flow :
181- return string . Format ( "{0:F1}" , 1e-3f * sensor . Value ) ;
168+ return $ " { 1e-3f * sensor . Value : F1 } " ;
182169 case SensorType . Control :
183- return string . Format ( "{0 :F0}", sensor . Value ) ;
170+ return $ " { sensor . Value : F0} ";
184171 case SensorType . Level :
185- return string . Format ( "{0 :F0}", sensor . Value ) ;
172+ return $ " { sensor . Value : F0} ";
186173 case SensorType . Power :
187174 case SensorType . Data :
188175 return sensor . Value . Value < 10
189- ? string . Format ( "{0 :0.00}", sensor . Value ) . Substring ( 0 , 3 )
190- : string . Format ( "{0 :F0}", sensor . Value ) ;
176+ ? $ " { sensor . Value : 0.00} ". Substring ( 0 , 3 )
177+ : $ " { sensor . Value : F0} ";
191178 case SensorType . Factor :
192- return string . Format ( "{0 :F1}", sensor . Value ) ;
179+ return $ " { sensor . Value : F1} ";
193180 }
194181 return "-" ;
195182 }
196183
197184 private Icon CreateTransparentIcon ( ) {
198- string text = GetString ( ) ;
199- int count = 0 ;
200- //for (int i = 0; i < text.Length; i++)
201- // if ((text[i] >= '0' && text[i] <= '9') || text[i] == '-')
202- // count++;
203- //bool small = count > 2;
204- bool small = text . Length > 2 ;
205-
206- graphics . Clear ( Color . Black ) ;
185+ var text = GetString ( ) ;
186+ var small = text . Length > 2 ;
187+
188+ var defaultBackColor = Color . Transparent ;
189+ var transparentIcon = false ;
190+ try {
191+ graphics . Clear ( defaultBackColor ) ;
192+ } catch ( Exception ) {
193+ try {
194+ defaultBackColor = SystemTools . GetTaskbarColor ( ) ;
195+ }
196+ catch ( Exception ) {
197+ defaultBackColor = Color . Black ;
198+ transparentIcon = true ;
199+ }
200+ graphics . Clear ( defaultBackColor ) ;
201+ }
202+
207203 TextRenderer . DrawText ( graphics , text , small ? smallFont : font ,
208- new Point ( - 4 , small ? 1 : - 2 ) , color , Color . Black ) ;
204+ new Point ( - 4 , small ? 1 : - 2 ) , color , defaultBackColor ) ;
209205
210- BitmapData data = bitmap . LockBits (
206+ var data = bitmap . LockBits (
211207 new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ,
212208 ImageLockMode . ReadOnly , PixelFormat . Format32bppArgb ) ;
213209
214- IntPtr Scan0 = data . Scan0 ;
210+ var scan0 = data . Scan0 ;
215211
216- int numBytes = bitmap . Width * bitmap . Height * 4 ;
217- byte [ ] bytes = new byte [ numBytes ] ;
218- Marshal . Copy ( Scan0 , bytes , 0 , numBytes ) ;
212+ var numBytes = bitmap . Width * bitmap . Height * 4 ;
213+ var bytes = new byte [ numBytes ] ;
214+ Marshal . Copy ( scan0 , bytes , 0 , numBytes ) ;
219215 bitmap . UnlockBits ( data ) ;
220216
221- byte red , green , blue ;
222- for ( int i = 0 ; i < bytes . Length ; i += 4 ) {
223- blue = bytes [ i ] ;
224- green = bytes [ i + 1 ] ;
225- red = bytes [ i + 2 ] ;
217+ if ( transparentIcon ) {
218+ for ( var i = 0 ; i < bytes . Length ; i += 4 ) {
219+ var blue = bytes [ i ] ;
220+ var green = bytes [ i + 1 ] ;
221+ var red = bytes [ i + 2 ] ;
226222
227- bytes [ i ] = color . B ;
228- bytes [ i + 1 ] = color . G ;
229- bytes [ i + 2 ] = color . R ;
230- bytes [ i + 3 ] = ( byte ) ( 0.3 * red + 0.59 * green + 0.11 * blue ) ;
223+ bytes [ i ] = color . B ;
224+ bytes [ i + 1 ] = color . G ;
225+ bytes [ i + 2 ] = color . R ;
226+ bytes [ i + 3 ] = ( byte ) ( 0.3 * red + 0.59 * green + 0.11 * blue ) ;
227+ }
231228 }
232229
233230 return IconFactory . Create ( bytes , bitmap . Width , bitmap . Height ,
@@ -241,15 +238,15 @@ private Icon CreatePercentageIcon() {
241238 graphics . Clear ( Color . Black ) ;
242239 }
243240 graphics . FillRectangle ( darkBrush , 0.5f , - 0.5f , bitmap . Width - 2 , bitmap . Height ) ;
244- float value = sensor . Value . GetValueOrDefault ( ) ;
245- float y = 0.16f * ( 100 - value ) ;
241+ var value = sensor . Value . GetValueOrDefault ( ) ;
242+ var y = 0.16f * ( 100 - value ) ;
246243 graphics . FillRectangle ( brush , 0.5f , - 0.5f + y , bitmap . Width - 2 , bitmap . Height - y ) ;
247244 graphics . DrawRectangle ( pen , 1 , 0 , bitmap . Width - 3 , bitmap . Height - 1 ) ;
248245
249- BitmapData data = bitmap . LockBits (
246+ var data = bitmap . LockBits (
250247 new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ,
251248 ImageLockMode . ReadOnly , PixelFormat . Format32bppArgb ) ;
252- byte [ ] bytes = new byte [ bitmap . Width * bitmap . Height * 4 ] ;
249+ var bytes = new byte [ bitmap . Width * bitmap . Height * 4 ] ;
253250 Marshal . Copy ( data . Scan0 , bytes , 0 , bytes . Length ) ;
254251 bitmap . UnlockBits ( data ) ;
255252
@@ -258,7 +255,7 @@ private Icon CreatePercentageIcon() {
258255 }
259256
260257 public void Update ( ) {
261- Icon icon = notifyIcon . Icon ;
258+ var icon = notifyIcon . Icon ;
262259
263260 switch ( sensor . SensorType ) {
264261 case SensorType . Load :
@@ -274,7 +271,7 @@ public void Update() {
274271 if ( icon != null )
275272 icon . Dispose ( ) ;
276273
277- string format = "" ;
274+ var format = "" ;
278275 switch ( sensor . SensorType ) {
279276 case SensorType . Voltage : format = "\n {0}: {1:F2} V" ; break ;
280277 case SensorType . Clock : format = "\n {0}: {1:F0} MHz" ; break ;
@@ -288,7 +285,7 @@ public void Update() {
288285 case SensorType . Data : format = "\n {0}: {1:F0} GB" ; break ;
289286 case SensorType . Factor : format = "\n {0}: {1:F3} GB" ; break ;
290287 }
291- string formattedValue = string . Format ( format , sensor . Name , sensor . Value ) ;
288+ var formattedValue = string . Format ( format , sensor . Name , sensor . Value ) ;
292289
293290 if ( sensor . SensorType == SensorType . Temperature &&
294291 unitManager . TemperatureUnit == TemperatureUnit . Fahrenheit )
@@ -298,10 +295,10 @@ public void Update() {
298295 UnitManager . CelsiusToFahrenheit ( sensor . Value ) ) ;
299296 }
300297
301- string hardwareName = sensor . Hardware . Name ;
298+ var hardwareName = sensor . Hardware . Name ;
302299 hardwareName = hardwareName . Substring ( 0 ,
303300 Math . Min ( 63 - formattedValue . Length , hardwareName . Length ) ) ;
304- string text = hardwareName + formattedValue ;
301+ var text = hardwareName + formattedValue ;
305302 if ( text . Length > 63 )
306303 text = null ;
307304
0 commit comments