88using System . IO ;
99using System . Linq ;
1010using System . Net ;
11+ using System . Runtime . InteropServices ;
1112using System . Windows . Forms ;
1213using Windows . Devices . Bluetooth ;
1314using Windows . Devices . Bluetooth . GenericAttributeProfile ;
@@ -56,7 +57,7 @@ public MyApplicationContext()
5657 SoftwareUpdateTimer . Start ( ) ;
5758
5859 lightMode = IsLightMode ( ) ;
59- notifyIcon . Icon = GetIcon ( - 1 , "?" ) ;
60+ SetIcon ( - 1 , "?" ) ;
6061 notifyIcon . Text = "XBatteryStatus: Looking for paired controller" ;
6162 notifyIcon . Visible = true ;
6263
@@ -236,7 +237,7 @@ async private void FindBleController()
236237
237238 if ( pairedGamepads . Count == 0 )
238239 {
239- notifyIcon . Icon = GetIcon ( - 1 , "!" ) ;
240+ SetIcon ( - 1 , "!" ) ;
240241 notifyIcon . Text = "XBatteryStatus: No paired controller with battery service found" ;
241242 }
242243 else
@@ -245,7 +246,7 @@ async private void FindBleController()
245246
246247 if ( connectedGamepads . Count == 0 )
247248 {
248- notifyIcon . Icon = GetIcon ( - 1 , "!" ) ;
249+ SetIcon ( - 1 , "!" ) ;
249250 notifyIcon . Text = "XBatteryStatus: No controller is connected" ;
250251 }
251252 else
@@ -256,7 +257,7 @@ async private void FindBleController()
256257 }
257258 else
258259 {
259- notifyIcon . Icon = GetIcon ( - 1 , "!" ) ;
260+ SetIcon ( - 1 , "!" ) ;
260261 notifyIcon . Text = "XBatteryStatus: Bluetooth is turned off" ;
261262 }
262263
@@ -323,7 +324,7 @@ private async void ReadBattery()
323324 string notify = val . ToString ( ) + "% - " + connectedGamepad . Name ;
324325 notifyIcon . Text = "XBatteryStatus: " + notify ;
325326
326- notifyIcon . Icon = GetIcon ( val ) ;
327+ SetIcon ( val ) ;
327328
328329 if ( ( lastBattery > 15 && val <= 15 ) || ( lastBattery > 10 && val <= 10 ) || ( lastBattery > 5 && val <= 5 ) )
329330 {
@@ -433,6 +434,18 @@ public bool IsLightMode()
433434 return true ;
434435 }
435436
437+ [ System . Runtime . InteropServices . DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
438+ extern static bool DestroyIcon ( IntPtr handle ) ;
439+
440+ public void SetIcon ( int val , string s = "" )
441+ {
442+ if ( notifyIcon . Icon != null )
443+ {
444+ DestroyIcon ( notifyIcon . Icon . Handle ) ;
445+ }
446+ notifyIcon . Icon = GetIcon ( val , s ) ;
447+ }
448+
436449 public Icon GetIcon ( int val , string s = "" )
437450 {
438451 var icon = Properties . Resources . icon00 ;
0 commit comments