Skip to content

Commit

Permalink
initial work on gui
Browse files Browse the repository at this point in the history
  • Loading branch information
rounk-ctrl committed Feb 8, 2022
1 parent 5f76f4f commit 5721e26
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/MicaForEveryone/x64/Release
/x64/Release
/MicaForEveryone/MicaForEveryone.vcxproj.user
/MicaForEveryone/MicaForEveryone.aps
126 changes: 80 additions & 46 deletions MicaForEveryone/MicaForEveryone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,6 @@
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
NOTIFYICONDATA nidApp;
BOOL Extend = FALSE;
BOOL Dark = FALSE;
BOOL Light = FALSE;
BOOL Mica = FALSE;
BOOL DefaultBack = TRUE;
BOOL Acrylic = FALSE;
BOOL Tabbed = FALSE;
BOOL DefaultCol = TRUE;
DWORD nice = TRUE;
HWINEVENTHOOK hEvent;
std::vector<HWND> hwndlist;
MARGINS margins = { -1 };
MENUITEMINFO mi = { 0 };
OSVERSIONINFOEX os;
UINT menuItemId = 0;
HICON hMainIcon;
POINT ok;
fnSetWindowCompositionAttribute SetWindowCompositionAttribute;
fnSetPreferredAppMode SetPreferredAppMode;

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
Expand Down Expand Up @@ -137,12 +117,26 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MICAFOREVERYONE));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MICAFOREVERYONE);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_MICAFOREVERYONE));

return RegisterClassExW(&wcex);
}
BOOL TrayIcon(HWND hWnd)
{
hMainIcon = LoadIcon(hInst, (LPCTSTR)MAKEINTRESOURCE(IDI_MICAFOREVERYONE));
nidApp.cbSize = sizeof(NOTIFYICONDATA);
nidApp.hWnd = (HWND)hWnd;
nidApp.uID = IDI_MICAFOREVERYONE;
nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nidApp.hIcon = hMainIcon;
nidApp.uCallbackMessage = WM_USER_SHELLICON;
nidApp.uVersion = NOTIFYICON_VERSION_4;
LoadString(hInst, IDS_APPTOOLTIP, nidApp.szTip, MAX_LOADSTRING);
Shell_NotifyIcon(NIM_ADD, &nidApp);
return TRUE;
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
Expand All @@ -157,26 +151,27 @@ HMODULE hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_S
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
// SetWindowCompositionAttribute = (fnSetWindowCompositionAttribute)GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetWindowCompositionAttribute");

SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(ForceDark);
RefreshImmersiveColorPolicyState = reinterpret_cast<fnRefreshImmersiveColorPolicyState>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(104)));
AllowDarkModeForWindow = reinterpret_cast<fnAllowDarkModeForWindow>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(133)));
SetPreferredAppMode(PreferredAppMode::ForceDark);
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
hMainIcon = LoadIcon(hInstance, (LPCTSTR)MAKEINTRESOURCE(IDI_MICAFOREVERYONE));
nidApp.cbSize = sizeof(NOTIFYICONDATA);
nidApp.hWnd = (HWND)hWnd;
nidApp.uID = IDI_MICAFOREVERYONE;
nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nidApp.hIcon = hMainIcon;
nidApp.uCallbackMessage = WM_USER_SHELLICON;
nidApp.uVersion = NOTIFYICON_VERSION_4;
LoadString(hInstance, IDS_APPTOOLTIP, nidApp.szTip, MAX_LOADSTRING);
Shell_NotifyIcon(NIM_ADD, &nidApp);
return TRUE;
TrayIcon(hWnd);

MSG msg;
// Main message loop:
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}
BOOL CALLBACK hwndcallback(HWND hwnd, LPARAM lParam) {
std::vector<HWND>& hwnds =
Expand Down Expand Up @@ -352,6 +347,7 @@ void ShowContextMenu(HWND hwnd, POINT pt)
{
DwmSetWindowAttribute(hwnds, DWMWA_USE_IMMERSIVE_DARK_MODE, &nice, sizeof nice);
}

}
if (IDM_LIGHT == menuItemId)
{
Expand Down Expand Up @@ -441,6 +437,10 @@ void ShowContextMenu(HWND hwnd, POINT pt)
DwmSetWindowAttribute(hwnds, DWMWA_SYSTEMBACKDROP_TYPE, &value, sizeof value);
}
}
if (IDM_GUI == menuItemId)
{
ShowWindow(hwnd, SW_SHOW);
}

DestroyMenu(hMenu);
}
Expand Down Expand Up @@ -516,15 +516,24 @@ VOID CALLBACK WinEventProcCallback(HWINEVENTHOOK hWinEventHook, DWORD dwEvent, H
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HBRUSH brush = CreateSolidBrush(darkBkColor);
RtlGetVersion(&os);
static UINT s_uTaskbarRestart;
switch (message)
{
case WM_CREATE:
AllowDarkModeForWindow(hWnd, true);
nice = TRUE;
DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &nice, sizeof nice);
hEvent = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL,
WinEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT);
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
break;
case WM_CLOSE:
ShowWindow(hWnd, SW_HIDE);
break;
case WM_ERASEBKGND:
SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG_PTR)brush);
case WM_USER_SHELLICON:
// systray msg callback
switch (LOWORD(lParam))
Expand All @@ -534,6 +543,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
EnumWindows(hwndcallback, reinterpret_cast<LPARAM>(&hwndlist));
ShowContextMenu(hWnd, ok);
return TRUE;
case WM_LBUTTONDOWN:
ShowWindow(hWnd, SW_SHOW);
return TRUE;
}
case WM_COMMAND:
{
Expand All @@ -554,22 +566,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
}
break;
case WM_DESTROY:
if (hEvent) UnhookWinEvent(hEvent);
UnhookWinEvent(hEvent);
PostQuitMessage(0);
break;
default:
if (message == s_uTaskbarRestart) {

hMainIcon = LoadIcon(hInst, (LPCTSTR)MAKEINTRESOURCE(IDI_MICAFOREVERYONE));
nidApp.cbSize = sizeof(NOTIFYICONDATA);
nidApp.hWnd = (HWND)hWnd;
nidApp.uID = IDI_MICAFOREVERYONE;
nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nidApp.hIcon = hMainIcon;
nidApp.uCallbackMessage = WM_USER_SHELLICON;
nidApp.uVersion = NOTIFYICON_VERSION_4;
LoadString(hInst, IDS_APPTOOLTIP, nidApp.szTip, MAX_LOADSTRING);
Shell_NotifyIcon(NIM_ADD, &nidApp);
TrayIcon(hWnd);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
Expand All @@ -580,10 +582,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL ok = TRUE;
static HBRUSH hbrBkgnd = nullptr;
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
SetWindowTheme(GetDlgItem(hDlg, IDOK), L"Explorer", nullptr);
SendMessageW(hDlg, WM_THEMECHANGED, 0, 0);
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
Expand All @@ -592,6 +597,35 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
return (INT_PTR)TRUE;
}
break;
case WM_CTLCOLORDLG:
return (INT_PTR)CreateSolidBrush(darkBkColor);
case WM_CTLCOLORSTATIC:
{
HDC hdc = reinterpret_cast<HDC>(wParam);
SetTextColor(hdc, darkTextColor);
SetBkColor(hdc, darkBkColor);
if (!hbrBkgnd)
hbrBkgnd = CreateSolidBrush(darkBkColor);
return reinterpret_cast<INT_PTR>(hbrBkgnd);
}
break;
case WM_DESTROY:
if (hbrBkgnd)
{
DeleteObject(hbrBkgnd);
hbrBkgnd = nullptr;
}
break;
case WM_THEMECHANGED:
{
nice = TRUE;
AllowDarkModeForWindow(hDlg, true);
DwmSetWindowAttribute(hDlg, DWMWA_USE_IMMERSIVE_DARK_MODE, &nice, sizeof nice);
HWND hButton = GetDlgItem(hDlg, IDOK);
AllowDarkModeForWindow(hButton, true);
SendMessageW(hButton, WM_THEMECHANGED, 0, 0);
UpdateWindow(hDlg);
}
}
return (INT_PTR)FALSE;
}
93 changes: 51 additions & 42 deletions MicaForEveryone/MicaForEveryone.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,18 @@ enum IMMERSIVE_HC_CACHE_MODE
IHCM_USE_CACHED_VALUE,
IHCM_REFRESH
};
enum PreferredAppMode
enum class PreferredAppMode
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};
enum WINDOWCOMPOSITIONATTRIB
{
WCA_UNDEFINED = 0,
WCA_NCRENDERING_ENABLED = 1,
WCA_NCRENDERING_POLICY = 2,
WCA_TRANSITIONS_FORCEDISABLED = 3,
WCA_ALLOW_NCPAINT = 4,
WCA_CAPTION_BUTTON_BOUNDS = 5,
WCA_NONCLIENT_RTL_LAYOUT = 6,
WCA_FORCE_ICONIC_REPRESENTATION = 7,
WCA_EXTENDED_FRAME_BOUNDS = 8,
WCA_HAS_ICONIC_BITMAP = 9,
WCA_THEME_ATTRIBUTES = 10,
WCA_NCRENDERING_EXILED = 11,
WCA_NCADORNMENTINFO = 12,
WCA_EXCLUDED_FROM_LIVEPREVIEW = 13,
WCA_VIDEO_OVERLAY_ACTIVE = 14,
WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15,
WCA_DISALLOW_PEEK = 16,
WCA_CLOAK = 17,
WCA_CLOAKED = 18,
WCA_ACCENT_POLICY = 19,
WCA_FREEZE_REPRESENTATION = 20,
WCA_EVER_UNCLOAKED = 21,
WCA_VISUAL_OWNER = 22,
WCA_HOLOGRAPHIC = 23,
WCA_EXCLUDED_FROM_DDA = 24,
WCA_PASSIVEUPDATEMODE = 25,
WCA_USEDARKMODECOLORS = 26,
WCA_LAST = 27
};

struct WINDOWCOMPOSITIONATTRIBDATA
{
WINDOWCOMPOSITIONATTRIB Attrib;
PVOID pvData;
SIZE_T cbData;
};

using fnRtlGetNtVersionNumbers = void (WINAPI*)(LPDWORD major, LPDWORD minor, LPDWORD build);
using fnSetWindowCompositionAttribute = BOOL(WINAPI*)(HWND hWnd, WINDOWCOMPOSITIONATTRIBDATA*);
// 1809 17763
using fnShouldAppsUseDarkMode = bool (WINAPI*)(); // ordinal 132
using fnAllowDarkModeForWindow = bool (WINAPI*)(HWND hWnd, bool allow); // ordinal 133
using fnAllowDarkModeForApp = bool (WINAPI*)(bool allow); // ordinal 135, in 1809
using fnFlushMenuThemes = void (WINAPI*)(); // ordinal 136
using fnRefreshImmersiveColorPolicyState = void (WINAPI*)(); // ordinal 104
using fnIsDarkModeAllowedForWindow = bool (WINAPI*)(HWND hWnd); // ordinal 137
Expand All @@ -72,3 +31,53 @@ using fnOpenNcThemeData = HTHEME(WINAPI*)(HWND hWnd, LPCWSTR pszClassList); // o
using fnShouldSystemUseDarkMode = bool (WINAPI*)(); // ordinal 138
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode); // ordinal 135, in 1903
using fnIsDarkModeAllowedForApp = bool (WINAPI*)(); // ordinal

/// Variables
// Notification Icon
NOTIFYICONDATA nidApp;

// Menu items values
BOOL Extend = FALSE;
BOOL Dark = FALSE;
BOOL Light = FALSE;
BOOL Mica = FALSE;
BOOL DefaultBack = TRUE;
BOOL Acrylic = FALSE;
BOOL Tabbed = FALSE;
BOOL DefaultCol = TRUE;

// value for DwmSetWindowAttribute
DWORD nice = TRUE;

// WinEventHook
HWINEVENTHOOK hEvent;

// list for all top level windows
std::vector<HWND> hwndlist;

// DwmExtendFrameIntoClientArea margins
MARGINS margins = { -1 };

// Menu item info
MENUITEMINFO mi = { 0 };

// osversionex struct
OSVERSIONINFOEX os;

// Menu Item id int used to check return values from menu
UINT menuItemId = 0;

// Dark mode text and background color
constexpr COLORREF darkBkColor = 0x202020;
constexpr COLORREF darkTextColor = 0xFFFFFF;

// icon
HICON hMainIcon;

// point used to spawn the menu where your cursor is
POINT ok;

// some functions used for dark mode
fnSetPreferredAppMode SetPreferredAppMode;
fnRefreshImmersiveColorPolicyState RefreshImmersiveColorPolicyState;
fnAllowDarkModeForWindow AllowDarkModeForWindow;
Binary file modified MicaForEveryone/MicaForEveryone.rc
Binary file not shown.
4 changes: 3 additions & 1 deletion MicaForEveryone/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define IDM_TEST 32811
#define IDM_TEST6 32812
#define ID_TITLEBARCOLORMODE_DEFAULT 32813
#define ID_MENU_OPENGUI 32814
#define IDM_GUI 32815
#define IDC_STATIC -1

// Next default values for new objects
Expand All @@ -40,7 +42,7 @@
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_COMMAND_VALUE 32814
#define _APS_NEXT_COMMAND_VALUE 32816
#define _APS_NEXT_CONTROL_VALUE 1002
#define _APS_NEXT_SYMED_VALUE 110
#endif
Expand Down

0 comments on commit 5721e26

Please sign in to comment.