Skip to content

Instantly share code, notes, and snippets.

@bplaat
Created February 2, 2021 08:30
Show Gist options
  • Save bplaat/5496a5fdbd99b992ff94ce1b75567e87 to your computer and use it in GitHub Desktop.
Save bplaat/5496a5fdbd99b992ff94ce1b75567e87 to your computer and use it in GitHub Desktop.
Win32 macOS like systembar experiment
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define UPDATE_TIMER 1
int barHeight = 32;
COLORREF backgroundColor = RGB(142, 68, 173);
COLORREF fontColor = RGB(255, 255, 255);
char *fontName = "Segoe UI";
int fontSize = 19;
int padding = 12;
HFONT font;
HFONT fontBold;
HWND activeWindow = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_CREATE) {
font = CreateFont(fontSize, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, CLEARTYPE_QUALITY, 0, fontName);
fontBold = CreateFont(fontSize, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, CLEARTYPE_QUALITY, 0, fontName);
SetTimer(hwnd, UPDATE_TIMER, 100, NULL);
return 0;
}
if (msg == WM_TIMER) {
if (wParam == UPDATE_TIMER) {
activeWindow = GetForegroundWindow();
InvalidateRect(hwnd, NULL, TRUE);
}
}
if (msg == WM_PAINT) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SetTextColor(hdc, fontColor);
SetBkMode(hdc, TRANSPARENT);
RECT windowRect;
GetClientRect(hwnd, &windowRect);
if (activeWindow != NULL) {
char title[MAX_PATH];
/*DWORD dwProcId = 0;
GetWindowThreadProcessId(activeWindow, &dwProcId);
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ , FALSE, dwProcId);
GetModuleFileName((HMODULE)hProc, title, MAX_PATH);
CloseHandle(hProc);*/
GetWindowText(activeWindow, &title, sizeof(title));
SelectObject(hdc, fontBold);
SIZE titleSize;
GetTextExtentPoint32(hdc, title, strlen(title), &titleSize);
if (titleSize.cx > 256) titleSize.cx = 256;
RECT titleRect = {
padding,
0,
padding + titleSize.cx,
windowRect.bottom
};
DrawText(hdc, title, -1, &titleRect, DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS);
HMENU menu = GetMenu(activeWindow);
int x = padding + titleSize.cx + padding;
SelectObject(hdc, font);
for (int i = 0; i < GetMenuItemCount(menu); i++) {
char menuItem[200];
GetMenuStringA(menu, i, &menuItem, sizeof(menuItem), MF_BYPOSITION);
SIZE size;
GetTextExtentPoint32(hdc, menuItem, strlen(menuItem), &size);
RECT menuItemRect = {
x,
0,
x + size.cx,
windowRect.bottom
};
DrawText(hdc, menuItem, -1, &menuItemRect, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
x += size.cx + padding / 2;
}
}
SelectObject(hdc, font);
char buf[100];
SYSTEMTIME st;
GetLocalTime(&st);
wsprintf(buf, "%02u:%02u:%02u", st.wHour, st.wMinute, st.wSecond);
RECT timeRect = {
0,
0,
windowRect.right - 8,
windowRect.bottom
};
DrawText(hdc, buf, -1, &timeRect, DT_RIGHT | DT_SINGLELINE | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
}
if (msg == WM_DESTROY) {
DeleteObject(fontBold);
DeleteObject(font);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(backgroundColor);
wc.lpszMenuName = NULL;
wc.lpszClassName = "systembar";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_NOACTIVATE, wc.lpszClassName, "Systembar", WS_POPUP,
0, 0, GetSystemMetrics(SM_CXSCREEN), barHeight, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment