Skip to content

Commit b2f255c

Browse files
committed
Added option to show battery icon instead of number in tray.
1 parent e19f1b8 commit b2f255c

File tree

5 files changed

+152
-25
lines changed

5 files changed

+152
-25
lines changed

Cloud2BatteryMonitorUI/Cloud2BatteryMonitorUI.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<ClInclude Include="SettingsHelper.h">
4242
<Filter>Header Files</Filter>
4343
</ClInclude>
44+
<ClInclude Include="resource1.h">
45+
<Filter>Header Files</Filter>
46+
</ClInclude>
47+
<ClInclude Include="resource2.h">
48+
<Filter>Header Files</Filter>
49+
</ClInclude>
4450
</ItemGroup>
4551
<ItemGroup>
4652
<ResourceCompile Include="Cloud2BatteryMonitorUI.rc">

Cloud2BatteryMonitorUI/MainForm.h

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,38 @@ namespace Cloud2BatteryMonitorUI {
410410
}
411411

412412
private: System::Void RefreshTrayIcon(System::String^ batteryLevel, System::String^ productName)
413+
{
414+
int battLvl = System::Int32::Parse(batteryLevel);
415+
String^ trayText = productName + "\n";
416+
SettingsHelper* settingsHelper = new SettingsHelper();
417+
418+
if (battLvl > 0 && battLvl <= 100)
419+
{
420+
if (settingsHelper->getBatIcon())
421+
{
422+
this->iconSystemTray->Icon = System::Drawing::Icon::FromHandle(RefreshTrayBattery(battLvl, settingsHelper));
423+
}
424+
else
425+
{
426+
this->iconSystemTray->Icon = System::Drawing::Icon::FromHandle(RefreshTrayNumber(batteryLevel, settingsHelper));
427+
}
428+
429+
trayText += BATTERY_LEVEL_STRING + batteryLevel + "%";
430+
this->iconSystemTray->Text = trayText;
431+
}
432+
else
433+
{
434+
trayText += BATTERY_LEVEL_STRING + "N/A";
435+
436+
this->iconSystemTray->Icon = System::Drawing::Icon::ExtractAssociatedIcon("icons\\headset_icon_light.ico");
437+
this->iconSystemTray->Text = trayText;
438+
}
439+
440+
delete trayText;
441+
delete settingsHelper;
442+
}
443+
444+
private: System::IntPtr RefreshTrayNumber(System::String^ batteryLevel, SettingsHelper* settingsHelper)
413445
{
414446
int battLvl = System::Int32::Parse(batteryLevel);
415447
System::Drawing::Font^ iconFont;
@@ -431,9 +463,6 @@ namespace Cloud2BatteryMonitorUI {
431463
System::Drawing::Graphics^ iconGraphics;
432464
iconGraphics = iconGraphics->FromImage(textBitmap);
433465

434-
System::IntPtr ptrIcon;
435-
SettingsHelper* settingsHelper = new SettingsHelper();
436-
437466
if (battLvl > 49)
438467
{
439468
iconBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorHighText());
@@ -453,30 +482,54 @@ namespace Cloud2BatteryMonitorUI {
453482
iconGraphics->TextRenderingHint = System::Drawing::Text::TextRenderingHint::SingleBitPerPixelGridFit;
454483
iconGraphics->DrawString(batteryLevel, iconFont, iconBrush, 0, 0);
455484

456-
ptrIcon = (textBitmap->GetHicon());
485+
delete iconBrush;
486+
delete iconFont;
487+
delete iconGraphics;
457488

458-
String^ TrayText = productName + "\n";
489+
return textBitmap->GetHicon();
490+
}
459491

460-
if (battLvl > 0 && battLvl <= 100)
461-
{
462-
TrayText += BATTERY_LEVEL_STRING + batteryLevel + "%";
492+
private: System::IntPtr RefreshTrayBattery(int battLvl, SettingsHelper* settingsHelper)
493+
{
494+
// For icon fill height, round the battery level to the nearest 10, then divide it by 10. One is added later as that is the minimum.
495+
int fillHeight = ((battLvl / 10) * 10) / 10;
496+
497+
System::Drawing::Brush^ batFillBrush = gcnew System::Drawing::SolidBrush(System::Drawing::Color::White);
498+
System::Drawing::Brush^ batOutlineBrush = gcnew System::Drawing::SolidBrush(System::Drawing::Color::Blue);
463499

464-
this->iconSystemTray->Icon = System::Drawing::Icon::FromHandle(ptrIcon);
465-
this->iconSystemTray->Text = TrayText;
500+
if (battLvl > 49)
501+
{
502+
batOutlineBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorHighText());
503+
batFillBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorHigh());
466504
}
467-
else
505+
else if (battLvl > 19)
468506
{
469-
TrayText += BATTERY_LEVEL_STRING + "N/A";
470-
471-
this->iconSystemTray->Icon = System::Drawing::Icon::ExtractAssociatedIcon("icons\\headset_icon_light.ico");
472-
this->iconSystemTray->Text = TrayText;
507+
batOutlineBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorMedText());
508+
batFillBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorMed());
509+
}
510+
else
511+
{
512+
batOutlineBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorLowText());
513+
batFillBrush = gcnew System::Drawing::SolidBrush(settingsHelper->getColorLow());
473514
}
474515

475-
delete iconBrush;
476-
delete textBitmap;
477-
delete iconFont;
478-
delete iconGraphics;
479-
delete settingsHelper;
516+
System::Drawing::Bitmap^ batBitmap = gcnew System::Drawing::Bitmap(16, 16);
517+
System::Drawing::Graphics^ batGraphics = System::Drawing::Graphics::FromImage(batBitmap);
518+
519+
// Draw outline of battery
520+
System::Drawing::Pen^ batPen = gcnew System::Drawing::Pen(batOutlineBrush);
521+
batGraphics->DrawRectangle(batPen, 2, 3, 11, 12);
522+
batGraphics->FillRectangle(batOutlineBrush, 6, 0, 4, 2);
523+
524+
// Fill in the level
525+
batGraphics->FillRectangle(batFillBrush, 3, 14 - fillHeight, 10, fillHeight + 1);
526+
527+
delete batFillBrush;
528+
delete batOutlineBrush;
529+
delete batGraphics;
530+
delete batPen;
531+
532+
return batBitmap->GetHicon();
480533
}
481534

482535
private: System::Void BtnRefresh_Click(System::Object^ sender, System::EventArgs^ e)

Cloud2BatteryMonitorUI/SettingsForm.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace Cloud2BatteryMonitorUI {
5151
private: System::Windows::Forms::Label^ lbText;
5252
private: System::Windows::Forms::Label^ lbRefresh;
5353
private: System::Windows::Forms::NumericUpDown^ numRefreshMinutes;
54+
private: System::Windows::Forms::CheckBox^ cbBatIcon;
5455

5556
private:
5657
/// <summary>
@@ -81,6 +82,7 @@ namespace Cloud2BatteryMonitorUI {
8182
this->lbText = (gcnew System::Windows::Forms::Label());
8283
this->lbRefresh = (gcnew System::Windows::Forms::Label());
8384
this->numRefreshMinutes = (gcnew System::Windows::Forms::NumericUpDown());
85+
this->cbBatIcon = (gcnew System::Windows::Forms::CheckBox());
8486
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numRefreshMinutes))->BeginInit();
8587
this->SuspendLayout();
8688
//
@@ -115,7 +117,7 @@ namespace Cloud2BatteryMonitorUI {
115117
//
116118
this->cbStart->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
117119
this->cbStart->AutoSize = true;
118-
this->cbStart->Location = System::Drawing::Point(12, 154);
120+
this->cbStart->Location = System::Drawing::Point(12, 180);
119121
this->cbStart->Name = L"cbStart";
120122
this->cbStart->Size = System::Drawing::Size(139, 17);
121123
this->cbStart->TabIndex = 6;
@@ -200,7 +202,7 @@ namespace Cloud2BatteryMonitorUI {
200202
this->btnSaveSettings->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
201203
this->btnSaveSettings->BackColor = System::Drawing::Color::LightGray;
202204
this->btnSaveSettings->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
203-
this->btnSaveSettings->Location = System::Drawing::Point(165, 150);
205+
this->btnSaveSettings->Location = System::Drawing::Point(165, 176);
204206
this->btnSaveSettings->Name = L"btnSaveSettings";
205207
this->btnSaveSettings->Size = System::Drawing::Size(138, 23);
206208
this->btnSaveSettings->TabIndex = 16;
@@ -249,12 +251,25 @@ namespace Cloud2BatteryMonitorUI {
249251
this->numRefreshMinutes->TabIndex = 20;
250252
this->numRefreshMinutes->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
251253
//
254+
// cbBatIcon
255+
//
256+
this->cbBatIcon->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
257+
this->cbBatIcon->AutoSize = true;
258+
this->cbBatIcon->Location = System::Drawing::Point(12, 150);
259+
this->cbBatIcon->Name = L"cbBatIcon";
260+
this->cbBatIcon->Size = System::Drawing::Size(120, 17);
261+
this->cbBatIcon->TabIndex = 21;
262+
this->cbBatIcon->Text = L"Use battery as icon.";
263+
this->cbBatIcon->UseVisualStyleBackColor = true;
264+
this->cbBatIcon->CheckStateChanged += gcnew System::EventHandler(this, &SettingsForm::CBBatIcon_CheckChange);
265+
//
252266
// SettingsForm
253267
//
254268
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
255269
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
256270
this->BackColor = System::Drawing::Color::Silver;
257-
this->ClientSize = System::Drawing::Size(316, 183);
271+
this->ClientSize = System::Drawing::Size(316, 209);
272+
this->Controls->Add(this->cbBatIcon);
258273
this->Controls->Add(this->numRefreshMinutes);
259274
this->Controls->Add(this->lbRefresh);
260275
this->Controls->Add(this->lbText);
@@ -272,8 +287,8 @@ namespace Cloud2BatteryMonitorUI {
272287
this->Controls->Add(this->lbSettingsHigh);
273288
this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
274289
this->MaximizeBox = false;
275-
this->MaximumSize = System::Drawing::Size(332, 222);
276-
this->MinimumSize = System::Drawing::Size(332, 222);
290+
this->MaximumSize = System::Drawing::Size(332, 248);
291+
this->MinimumSize = System::Drawing::Size(332, 248);
277292
this->Name = L"SettingsForm";
278293
this->Text = L"Settings";
279294
this->Load += gcnew System::EventHandler(this, &SettingsForm::SettingsForm_Load);
@@ -284,6 +299,11 @@ namespace Cloud2BatteryMonitorUI {
284299
}
285300
#pragma endregion
286301

302+
#define FILL_STRING "Fill"
303+
#define OUTLINE_STRING "Outline"
304+
#define BACKGROUND_STRING "Background"
305+
#define TEXT_STRING "Text"
306+
287307
private: System::Void SettingsForm_Load(System::Object^ sender, System::EventArgs^ e)
288308
{
289309

@@ -296,10 +316,32 @@ namespace Cloud2BatteryMonitorUI {
296316
this->btnLow->BackColor = settingsHelper->getColorLow();
297317
this->btnLowText->BackColor = settingsHelper->getColorLowText();
298318
this->cbStart->Checked = settingsHelper->getAutostart();
319+
this->cbBatIcon->Checked = settingsHelper->getBatIcon();
320+
321+
if (cbBatIcon->Checked)
322+
{
323+
this->lbBackground->Text = FILL_STRING;
324+
this->lbText->Text = OUTLINE_STRING;
325+
}
299326

300327
delete settingsHelper;
301328
}
302329

330+
private: System::Void CBBatIcon_CheckChange(System::Object^ sender, System::EventArgs^ e)
331+
{
332+
333+
if (cbBatIcon->Checked)
334+
{
335+
this->lbBackground->Text = FILL_STRING;
336+
this->lbText->Text = OUTLINE_STRING;
337+
}
338+
else
339+
{
340+
this->lbBackground->Text = BACKGROUND_STRING;
341+
this->lbText->Text = TEXT_STRING;
342+
}
343+
}
344+
303345
private: System::Void colorBtnClick(System::Object^ sender, System::EventArgs^ e)
304346
{
305347
ColorDialog^ colorDlg = gcnew ColorDialog();
@@ -326,6 +368,7 @@ namespace Cloud2BatteryMonitorUI {
326368
settingsHelper->setColorLow(this->btnLow->BackColor);
327369
settingsHelper->setColorLowText(this->btnLowText->BackColor);
328370
settingsHelper->setAutostart(this->cbStart->Checked);
371+
settingsHelper->setBatIcon(this->cbBatIcon->Checked);
329372

330373
settingsHelper->saveSettings();
331374

Cloud2BatteryMonitorUI/SettingsHelper.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,27 @@ bool SettingsHelper::getAutostart()
129129
void SettingsHelper::setAutostart(bool autostart)
130130
{
131131
settingsNode.find_child_by_attribute(XML_ELEM_SETTING, XML_ATTR_NAME, "autostart").text().set(autostart);
132+
}
133+
134+
bool SettingsHelper::getBatIcon()
135+
{
136+
pugi::xml_node batNode = settingsNode.find_child_by_attribute(XML_ELEM_SETTING, XML_ATTR_NAME, "batteryIcon");
137+
138+
return settingsNode.find_child_by_attribute(XML_ELEM_SETTING, XML_ATTR_NAME, "batteryIcon").text().as_bool();
139+
}
140+
141+
void SettingsHelper::setBatIcon(bool useBatIcon)
142+
{
143+
pugi::xml_node batNode = settingsNode.find_child_by_attribute(XML_ELEM_SETTING, XML_ATTR_NAME, "batteryIcon");
144+
if (!batNode)
145+
{
146+
pugi::xml_node newNode = settingsNode.append_child("setting");
147+
pugi::xml_attribute attribute = newNode.append_attribute("name");
148+
attribute.set_value("batteryIcon");
149+
newNode.text().set(useBatIcon);
150+
}
151+
else
152+
{
153+
batNode.text().set(useBatIcon);
154+
}
132155
}

Cloud2BatteryMonitorUI/SettingsHelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ class SettingsHelper
3636
void setColorLowText(Color);
3737
bool getAutostart();
3838
void setAutostart(bool);
39+
bool getBatIcon();
40+
void setBatIcon(bool);
3941

4042
};

0 commit comments

Comments
 (0)