@@ -7,13 +7,12 @@ namespace percentage
7
7
{
8
8
class TrayIcon
9
9
{
10
- [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
10
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
11
11
static extern bool DestroyIcon ( IntPtr handle ) ;
12
12
13
- private const string iconFont = "Segoe UI" ;
14
- private const int iconFontSize = 14 ;
13
+ private const int fontSize = 18 ;
14
+ private const string font = "Segoe UI" ;
15
15
16
- private string batteryPercentage ;
17
16
private NotifyIcon notifyIcon ;
18
17
19
18
public TrayIcon ( )
@@ -23,82 +22,75 @@ public TrayIcon()
23
22
24
23
notifyIcon = new NotifyIcon ( ) ;
25
24
26
- // initialize contextMenu
27
25
contextMenu . MenuItems . AddRange ( new MenuItem [ ] { menuItem } ) ;
28
26
29
- // initialize menuItem
27
+ menuItem . Click += new System . EventHandler ( MenuItemClick ) ;
30
28
menuItem . Index = 0 ;
31
29
menuItem . Text = "E&xit" ;
32
- menuItem . Click += new System . EventHandler ( menuItem_Click ) ;
33
30
34
31
notifyIcon . ContextMenu = contextMenu ;
35
-
36
- batteryPercentage = "?" ;
37
-
38
32
notifyIcon . Visible = true ;
39
33
40
34
Timer timer = new Timer ( ) ;
41
- timer . Tick += new EventHandler ( timer_Tick ) ;
42
- timer . Interval = 1000 ; // in miliseconds
35
+ timer . Interval = 1000 ;
36
+ timer . Tick += new EventHandler ( TimerTick ) ;
43
37
timer . Start ( ) ;
44
38
}
45
39
46
- private void timer_Tick ( object sender , EventArgs e )
40
+ private Bitmap GetTextBitmap ( String text , Font font , Color fontColor )
47
41
{
48
- PowerStatus powerStatus = SystemInformation . PowerStatus ;
49
- batteryPercentage = ( powerStatus . BatteryLifePercent * 100 ) . ToString ( ) ;
50
-
51
- using ( Bitmap bitmap = new Bitmap ( DrawText ( batteryPercentage , new Font ( iconFont , iconFontSize ) , Color . White , Color . Black ) ) )
42
+ SizeF imageSize = GetStringImageSize ( text , font ) ;
43
+ Bitmap bitmap = new Bitmap ( ( int ) imageSize . Width , ( int ) imageSize . Height ) ;
44
+ using ( Graphics graphics = Graphics . FromImage ( bitmap ) )
52
45
{
53
- System . IntPtr intPtr = bitmap . GetHicon ( ) ;
54
- try
55
- {
56
- using ( Icon icon = Icon . FromHandle ( intPtr ) )
57
- {
58
- notifyIcon . Icon = icon ;
59
- notifyIcon . Text = batteryPercentage + "%" ;
60
- }
61
- }
62
- finally
46
+ graphics . Clear ( Color . FromArgb ( 0 , 0 , 0 , 0 ) ) ;
47
+ using ( Brush brush = new SolidBrush ( fontColor ) )
63
48
{
64
- DestroyIcon ( intPtr ) ;
49
+ graphics . DrawString ( text , font , brush , 0 , 0 ) ;
50
+ graphics . TextRenderingHint = System . Drawing . Text . TextRenderingHint . AntiAlias ;
51
+ graphics . Save ( ) ;
65
52
}
66
53
}
54
+ return bitmap ;
55
+ }
56
+
57
+ private static SizeF GetStringImageSize ( string text , Font font )
58
+ {
59
+ using ( Image image = new Bitmap ( 1 , 1 ) )
60
+ using ( Graphics graphics = Graphics . FromImage ( image ) )
61
+ return graphics . MeasureString ( text , font ) ;
67
62
}
68
63
69
- private void menuItem_Click ( object sender , EventArgs e )
64
+ private void MenuItemClick ( object sender , EventArgs e )
70
65
{
71
66
notifyIcon . Visible = false ;
72
67
notifyIcon . Dispose ( ) ;
73
68
Application . Exit ( ) ;
74
69
}
75
70
76
- private Image DrawText ( String text , Font font , Color textColor , Color backColor )
71
+ private void TimerTick ( object sender , EventArgs e )
77
72
{
78
- var textSize = GetImageSize ( text , font ) ;
79
- Image image = new Bitmap ( ( int ) textSize . Width , ( int ) textSize . Height ) ;
80
- using ( Graphics graphics = Graphics . FromImage ( image ) )
73
+ PowerStatus powerStatus = SystemInformation . PowerStatus ;
74
+ String percentage = ( powerStatus . BatteryLifePercent * 100 ) . ToString ( ) ;
75
+ bool isCharging = SystemInformation . PowerStatus . PowerLineStatus == PowerLineStatus . Online ;
76
+ String bitmapText = isCharging ? percentage + "*" : percentage ;
77
+ using ( Bitmap bitmap = new Bitmap ( GetTextBitmap ( bitmapText , new Font ( font , fontSize ) , Color . White ) ) )
81
78
{
82
- // paint the background
83
- graphics . Clear ( backColor ) ;
84
-
85
- // create a brush for the text
86
- using ( Brush textBrush = new SolidBrush ( textColor ) )
79
+ System . IntPtr intPtr = bitmap . GetHicon ( ) ;
80
+ try
87
81
{
88
- graphics . TextRenderingHint = System . Drawing . Text . TextRenderingHint . AntiAlias ;
89
- graphics . DrawString ( text , font , textBrush , 0 , 0 ) ;
90
- graphics . Save ( ) ;
82
+ using ( Icon icon = Icon . FromHandle ( intPtr ) )
83
+ {
84
+ notifyIcon . Icon = icon ;
85
+ String toolTipText = percentage + "%" + ( isCharging ? " Charging" : "" ) ;
86
+ notifyIcon . Text = toolTipText ;
87
+ }
88
+ }
89
+ finally
90
+ {
91
+ DestroyIcon ( intPtr ) ;
91
92
}
92
93
}
93
-
94
- return image ;
95
- }
96
-
97
- private static SizeF GetImageSize ( string text , Font font )
98
- {
99
- using ( Image image = new Bitmap ( 1 , 1 ) )
100
- using ( Graphics graphics = Graphics . FromImage ( image ) )
101
- return graphics . MeasureString ( text , font ) ;
102
94
}
103
95
}
104
96
}
0 commit comments