Skip to content

Commit

Permalink
Version 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
CSaratakij committed Aug 21, 2018
2 parents f255ed0 + 49061c5 commit e43278b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 30 deletions.
127 changes: 98 additions & 29 deletions MyWinBar/MyWinBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
HWND currentFocusWindow;
UINT currentWorkspace;

SYSTEMTIME localTime;
APPBARDATA appbarData;

RECT rectLeft;
Expand All @@ -17,6 +16,16 @@ RECT rectRight;
int focusWindowTextBufferLength = MAX_LOADSTRING;
TCHAR focusWindowTextBuffer[MAX_LOADSTRING];

RECT rectBatteryBG;
RECT rectBatteryStatusBG;

HBRUSH brushBatteryHigh;
HBRUSH brushBatteryLow;
HBRUSH brushBatteryCritical;

SYSTEMTIME localTime;
SYSTEM_POWER_STATUS powerStatus;

HINSTANCE hInst;
WCHAR szTitle[MAX_LOADSTRING];
WCHAR szWindowClass[MAX_LOADSTRING];
Expand Down Expand Up @@ -148,7 +157,12 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
rectRight.right = (barPortion * 3);
rectRight.bottom = MAX_APPBAR_HEIGHT;

brushBatteryHigh = CreateSolidBrush(colorBatteryHigh);
brushBatteryLow = CreateSolidBrush(colorBatteryLow);
brushBatteryCritical = CreateSolidBrush(colorBatteryCritical);

GetLocalTime(&localTime);
GetSystemPowerStatus(&powerStatus);

SetTimer(hWnd, IDT_REDRAW_TIMER, TIMER_REDRAW_RATE, NULL);
SetTimer(hWnd, IDT_REDRAW_BAR_CENTER_TIMER, TIMER_REDRAW_BAR_CENTER_RATE, NULL);
Expand All @@ -168,6 +182,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDT_REDRAW_TIMER:
{
GetLocalTime(&localTime);
GetSystemPowerStatus(&powerStatus);
InvalidateRect(hWnd, &rectRight, TRUE);
break;
}
Expand All @@ -193,6 +208,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
PaintWorkspace(hdc);
PaintCurrentFocusWindow(hdc);

PaintBatteryInfo(hdc);

MoveToRightSideOfScreen(hdc);
PaintLocalTime(hdc);

Expand Down Expand Up @@ -221,6 +238,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
KillTimer(hWnd, IDT_REDRAW_TIMER);
KillTimer(hWnd, IDT_REDRAW_BAR_CENTER_TIMER);

DeleteObject(brushBatteryHigh);
DeleteObject(brushBatteryLow);
DeleteObject(brushBatteryCritical);

SHAppBarMessage(ABM_REMOVE, &appbarData);
PostQuitMessage(0);

Expand All @@ -234,17 +255,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return 0;
}

void MoveToRightSideOfScreen(HDC hdc)
{
SetTextAlign(hdc, TA_LEFT | TA_TOP | TA_UPDATECP);

SetBkColor(hdc, black);
SetTextColor(hdc, white);

int beginPos = GetSystemMetrics(SM_CXSCREEN) * 95 / 100;
MoveToEx(hdc, beginPos, 0, NULL);
}

void PaintWorkspace(HDC hdc)
{
TCHAR buffer[5];
Expand All @@ -265,6 +275,82 @@ void PaintWorkspace(HDC hdc)
}
}

void PaintCurrentFocusWindow(HDC hdc)
{
SetBkColor(hdc, black);
SetTextColor(hdc, goldYellow);

HWND hWnd = currentFocusWindow;

if (hWnd == NULL) {
DrawTextW(hdc, NULL, 0, &rectCenter, DT_CENTER | DT_VCENTER);
}
else {
int length = GetWindowText(hWnd, focusWindowTextBuffer, focusWindowTextBufferLength);
if (length > 0)
DrawTextW(hdc, focusWindowTextBuffer, length, &rectCenter, DT_CENTER | DT_VCENTER);
}
}

void PaintBatteryInfo(HDC hdc)
{
if (powerStatus.BatteryFlag == 128 || powerStatus.BatteryFlag == 255)
return;

rectBatteryBG;
rectBatteryStatusBG;

int offset = 2;

rectBatteryBG.left = GetSystemMetrics(SM_CXSCREEN) * 92 / 100;
rectBatteryBG.top = 2;
rectBatteryBG.right = GetSystemMetrics(SM_CXSCREEN) * 94 / 100;
rectBatteryBG.bottom = (MAX_APPBAR_HEIGHT - 6);

rectBatteryStatusBG.left = rectBatteryBG.left + offset;
rectBatteryStatusBG.top = rectBatteryBG.top + offset;
rectBatteryStatusBG.right = rectBatteryBG.right - offset;
rectBatteryStatusBG.bottom = rectBatteryBG.bottom - offset;

Rectangle(hdc, rectBatteryBG.left, rectBatteryBG.top, rectBatteryBG.right, rectBatteryBG.bottom);

int rectBatteryStatusBGSize = (rectBatteryStatusBG.right - rectBatteryStatusBG.left);
int rectBatteryStatusBGSizePortion = (rectBatteryStatusBGSize / 4);

if (powerStatus.BatteryLifePercent == 255)
return;

if (powerStatus.BatteryLifePercent >= 90) {
FillRect(hdc, &rectBatteryStatusBG, brushBatteryHigh);
}

else if (powerStatus.BatteryLifePercent >= 40 && powerStatus.BatteryLifePercent < 90) {
rectBatteryStatusBG.left = rectBatteryStatusBG.right - (rectBatteryStatusBGSizePortion * 3);
FillRect(hdc, &rectBatteryStatusBG, brushBatteryHigh);
}

else if (powerStatus.BatteryLifePercent >= 15 && powerStatus.BatteryLifePercent < 40) {
rectBatteryStatusBG.left = rectBatteryStatusBG.right - (rectBatteryStatusBGSizePortion * 2);
FillRect(hdc, &rectBatteryStatusBG, brushBatteryLow);
}

else if (powerStatus.BatteryLifePercent >= 0 && powerStatus.BatteryLifePercent < 15) {
rectBatteryStatusBG.left = rectBatteryStatusBG.right - (rectBatteryStatusBGSizePortion);
FillRect(hdc, &rectBatteryStatusBG, brushBatteryCritical);
}
}

void MoveToRightSideOfScreen(HDC hdc)
{
SetTextAlign(hdc, TA_LEFT | TA_TOP | TA_UPDATECP);

SetBkColor(hdc, black);
SetTextColor(hdc, white);

int beginPos = GetSystemMetrics(SM_CXSCREEN) * 95 / 100;
MoveToEx(hdc, beginPos, 0, NULL);
}

void PaintLocalTime(HDC hdc)
{
WORD resultHour;
Expand All @@ -285,20 +371,3 @@ void PaintLocalTime(HDC hdc)

TextOut(hdc, 0, 0, txtTime, txtTimeLength);
}

void PaintCurrentFocusWindow(HDC hdc)
{
SetBkColor(hdc, black);
SetTextColor(hdc, goldYellow);

HWND hWnd = currentFocusWindow;

if (hWnd == NULL) {
DrawTextW(hdc, NULL, 0, &rectCenter, DT_CENTER | DT_VCENTER);
}
else {
int length = GetWindowText(hWnd, focusWindowTextBuffer, focusWindowTextBufferLength);
if (length > 0)
DrawTextW(hdc, focusWindowTextBuffer, length, &rectCenter, DT_CENTER | DT_VCENTER);
}
}
7 changes: 6 additions & 1 deletion MyWinBar/MyWinBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
#define white RGB(255, 255, 255)
#define goldYellow RGB(255, 185, 0)

#define colorBatteryHigh RGB(164, 244, 66)
#define colorBatteryLow RGB(244, 223, 65)
#define colorBatteryCritical RGB(255, 28, 28)

void MoveToRightSideOfScreen(HDC);
void PaintWorkspace(HDC);
void PaintCurrentFocusWindow(HDC hdc);
void PaintCurrentFocusWindow(HDC);
void PaintBatteryInfo(HDC);
void PaintLocalTime(HDC);

0 comments on commit e43278b

Please sign in to comment.