@@ -96,6 +96,8 @@ private void CloseIcon_MouseLeave(object? sender, EventArgs e)
9696
9797 private void Dashboard_Load ( object ? sender , EventArgs e )
9898 {
99+ RenderIcon ( _isCharging ) ;
100+
99101 if ( appSetting . Default . startMinimized )
100102 {
101103 this . Hide ( ) ;
@@ -523,6 +525,7 @@ private void RefreshBatteryStatus()
523525
524526 UpdateBatteryPercentage ( status ) ;
525527 UpdateBatteryChargeRemainingStatus ( status ) ;
528+ RenderIcon ( _isCharging ) ;
526529 }
527530
528531
@@ -597,6 +600,15 @@ private ContextMenuStrip InitializeContextMenu()
597600 contextMenu . Items . Clear ( ) ;
598601 contextMenu . TopLevel = true ;
599602
603+ ToolStripMenuItem BatteryPercentageToolStripItem = new ( "Show Battery Percentage" )
604+ {
605+ Text = "Show Percentage In Icon" + ( appSetting . Default . showBatteryPercentageInIcon ? "✔" : "" ) ,
606+ Name = "FullBatteryNotification" ,
607+ TextAlign = ContentAlignment . MiddleCenter ,
608+ Font = FontProvider . GetRegularFont ( 10.2F )
609+ } ;
610+ BatteryPercentageToolStripItem . Click += ShowBatteryPercentageInIcon_Click ! ;
611+
600612 ToolStripMenuItem FullBatteryNotificationToolStripItem = new ( "Full Battery Notification" )
601613 {
602614 Text = "Full Battery Notification" + ( appSetting . Default . fullBatteryNotification ? "✔" : "" ) ,
@@ -643,6 +655,7 @@ private ContextMenuStrip InitializeContextMenu()
643655 } ;
644656 viewSourceToolStripItem . Click += ViewSource_Click ! ;
645657
658+ contextMenu . Items . Add ( BatteryPercentageToolStripItem ) ;
646659 contextMenu . Items . Add ( FullBatteryNotificationToolStripItem ) ;
647660 contextMenu . Items . Add ( LowBatteryNotificationToolStripItem ) ;
648661 contextMenu . Items . Add ( startMinimizedToolStripItem ) ;
@@ -653,6 +666,14 @@ private ContextMenuStrip InitializeContextMenu()
653666 return contextMenu ;
654667 }
655668
669+ private void ShowBatteryPercentageInIcon_Click ( object ? sender , EventArgs e )
670+ {
671+ appSetting . Default . showBatteryPercentageInIcon = ! appSetting . Default . showBatteryPercentageInIcon ;
672+ appSetting . Default . Save ( ) ;
673+
674+ BatteryNotifierIcon . ContextMenuStrip = InitializeContextMenu ( ) ;
675+ }
676+
656677 private void StartMinimized_Click ( object ? sender , EventArgs e )
657678 {
658679 appSetting . Default . startMinimized = ! appSetting . Default . startMinimized ;
@@ -921,5 +942,38 @@ private void BatteryNotifierIcon_BalloonTipClosed(object? sender, EventArgs e)
921942 _batteryNotification . Stop ( ) ;
922943 _soundPlayingTimer . Stop ( ) ;
923944 }
945+
946+ public void RenderIcon ( bool isCharging )
947+ {
948+ if ( ! appSetting . Default . showBatteryPercentageInIcon )
949+ {
950+ Icon = Icon . FromHandle ( Resources . logo_charging . GetHicon ( ) ) ;
951+ }
952+ else
953+ {
954+ var status = SystemInformation . PowerStatus ;
955+ var percentage = ( int ) Math . Round ( status . BatteryLifePercent * 100 , 0 ) ;
956+ Icon = RenderBadge ( Resources . logo , 180 , 180 , 220 , isCharging ? "⚡" : percentage . ToString ( ) ) ;
957+ }
958+ }
959+
960+ private static Icon RenderBadge ( Bitmap bitmap , int width , int height , int textWidth , string batteryPercentage )
961+ {
962+ Graphics g = Graphics . FromImage ( bitmap ) ;
963+ g . SmoothingMode = System . Drawing . Drawing2D . SmoothingMode . AntiAlias ;
964+
965+ Rectangle textborder = new ( bitmap . Width / 2 - textWidth / 2 , bitmap . Height / 2 - height / 3 , textWidth , height ) ;
966+
967+ StringFormat stringFormat = new ( )
968+ {
969+ Alignment = StringAlignment . Center ,
970+ LineAlignment = StringAlignment . Center
971+ } ;
972+
973+ g . FillEllipse ( Brushes . DarkGreen , bitmap . Width / 2 - ( width / 2 ) , bitmap . Height / 2 - height / 3 , width , height ) ;
974+ g . DrawString ( batteryPercentage . ToString ( ) , FontProvider . GetBoldFont ( 96 ) , Brushes . White , textborder , stringFormat ) ;
975+
976+ return Icon . FromHandle ( bitmap . GetHicon ( ) ) ;
977+ }
924978 }
925979}
0 commit comments