private void timer_Tick(object sender, EventArgs e) { PowerStatus powerStatus = SystemInformation.PowerStatus; bool fullyCharged = (powerStatus.BatteryLifePercent == 1.0); BatteryChargeStatus chargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus; bool charging = chargeStatus.HasFlag(BatteryChargeStatus.Charging); bool critical = chargeStatus.HasFlag(BatteryChargeStatus.Critical); bool noBattery = chargeStatus.HasFlag(BatteryChargeStatus.NoSystemBattery); PowerLineStatus powerLineStatus = SystemInformation.PowerStatus.PowerLineStatus; bool pluggedIn = (powerLineStatus == PowerLineStatus.Online); batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString(); Color fontColor = Color.White; if (!noBattery) { if (charging || (pluggedIn && fullyCharged)) { fontColor = Color.Lime; } else if (critical) { fontColor = Color.Red; } } using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), fontColor, Color.Black))) { System.IntPtr intPtr = bitmap.GetHicon(); try { using (Icon icon = Icon.FromHandle(intPtr)) { string description = ""; if (noBattery) { description = "No Battery Detected"; } else if (pluggedIn) { if (charging) { description = string.Format("Charging ({0}%)", batteryPercentage); } else if (fullyCharged) { description = string.Format("Fully Charged ({0}%)", batteryPercentage); } else { description = string.Format("Not Charging ({0}%)", batteryPercentage); } } else { int totalRemaining = SystemInformation.PowerStatus.BatteryLifeRemaining; if (totalRemaining > 0) { TimeSpan timeSpan = TimeSpan.FromSeconds(totalRemaining); description = string.Format("{1} hr {2:D2} min ({0}%) remaining", batteryPercentage, timeSpan.Hours, timeSpan.Minutes); } else { description = string.Format("{0}% remaining", batteryPercentage); } } notifyIcon.Icon = icon; notifyIcon.Text = description; descriptionMenuItem.Text = description; } } finally { DestroyIcon(intPtr); } } }
private void iconUpdate() { PowerStatus powerStatus = SystemInformation.PowerStatus; batteryPercentage = (int)Math.Round((powerStatus.BatteryLifePercent * 100), 0); Color foreground = Color.White; BatteryChargeStatus batteryChargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus; bool charging = batteryChargeStatus.HasFlag(BatteryChargeStatus.Charging); if (charging) { foreground = Color.FromArgb(255, 0, 255, 0); } else if (batteryPercentage < 30) { foreground = Color.FromArgb(255, 255, 0, 0); } else { foreground = Color.FromArgb(255, 255, 255, 255); } // Color background = Color.Black; Color background = Color.Transparent; string drawMe = batteryPercentage.ToString(); Font fontToUse; Brush brushToUse = new SolidBrush(foreground); Rectangle rect; Bitmap bitmapText; Graphics g; if (batteryPercentage == 100) { // reduce size to fit "100" fontToUse = new Font("Tahoma", 20, FontStyle.Regular, GraphicsUnit.Pixel); } else { fontToUse = new Font("Tahoma", 28, FontStyle.Regular, GraphicsUnit.Pixel); } rect = new Rectangle(-6, 2, 42, 32); bitmapText = new Bitmap(32, 32); g = Graphics.FromImage(bitmapText); g.Clear(background); using (SolidBrush brush = new SolidBrush(background)) { g.FillRectangle(brush, 0, 0, 32, 32); } g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(drawMe, fontToUse, brushToUse, rect, sf); g.Save(); System.IntPtr intPtr = bitmapText.GetHicon(); try { using (Icon icon = Icon.FromHandle(intPtr)) { notifyIcon.Icon = icon; if (!charging) { int seconds = SystemInformation.PowerStatus.BatteryLifeRemaining; int mins = seconds / 60; int hours = mins / 60; mins = mins % 60; notifyIcon.Text = hours + ":" + mins + " remaining"; } else { notifyIcon.Text = "Charging"; } } } finally { DestroyIcon(intPtr); } // hIcon = (bitmapText.GetHicon()); // notifyIcon.Icon = System.Drawing.Icon.FromHandle(hIcon); // notifyIcon.Text = "Remaining " + batteryPercentage.ToString() + "%"; }