Skip to content

Commit

Permalink
Classic Conhost 1.0.0 (#350)
Browse files Browse the repository at this point in the history
* Classic Conhost 1.0.0

* Classic Conhost: SetThemeAppProperties(STAP_ALLOW_NONCLIENT) if classic
This will replicate XP behavior for properties window

* Revert destructive changes of previous commit

* Classic Conhost: Add `author` and `github` fields
  • Loading branch information
aubymori authored Oct 21, 2023
1 parent 5da8dd0 commit ed112ac
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions mods/classic-conhost.wh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// ==WindhawkMod==
// @id classic-conhost
// @name Classic Conhost
// @description Forces classic theme and optionally client edge on console windows
// @version 1.0.0
// @author aubymori
// @github https://github.com/aubymori
// @include *
// @compilerOptions -luser32 -ldwmapi -luxtheme
// ==/WindhawkMod==

// ==WindhawkModReadme==
/*
# Classic Conhost
This mod will apply classic theme and optionally client edge to console windows.
**Before**:
![Before](https://raw.githubusercontent.com/aubymori/images/main/classic-conhost-before.png)
**After**:
![After](https://raw.githubusercontent.com/aubymori/images/main/classic-conhost-after.png)
*/
// ==/WindhawkModReadme==

// ==WindhawkModSettings==
/*
- classic: true
$name: Classic theme
$description: Apply classic theme to console windows. Disable this if you use classic theme system-wide.
- clientedge: true
$name: Client edge (Classic theme only)
$description: Apply a client edge to console windows
*/
// ==/WindhawkModSettings==

#include <windows.h>
#include <processthreadsapi.h>
#include <uxtheme.h>
#include <dwmapi.h>

HANDLE g_hObserverThread;

struct {
bool classic;
bool clientedge;
} settings;

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
WCHAR szClass[MAX_PATH];
int nStatus = GetClassNameW(hWnd, szClass, MAX_PATH);
BOOL bIsValidString = (((ULONG_PTR)szClass & ~(ULONG_PTR)0xffff) != 0);

if (nStatus && bIsValidString && !wcscmp(szClass, L"ConsoleWindowClass"))
{
if (settings.classic)
{
/* Set window theme */
SetWindowTheme(hWnd, L" ", L" ");
SendMessage(hWnd, WM_THEMECHANGED, NULL, NULL);

/* Disable DWM NC frames */
DWORD dwPol = DWMNCRP_DISABLED;
DwmSetWindowAttribute(
hWnd,
DWMWA_NCRENDERING_POLICY,
&dwPol,
sizeof(DWORD)
);
}

if (settings.clientedge)
{
DWORD dwExStyle = GetWindowLongPtrW(hWnd, GWL_EXSTYLE);
RECT rcWnd;
GetWindowRect(hWnd, &rcWnd);

if ((dwExStyle & WS_EX_CLIENTEDGE) != WS_EX_CLIENTEDGE)
{
SetWindowLongPtrW(
hWnd,
GWL_EXSTYLE,
dwExStyle | WS_EX_CLIENTEDGE
);

/* Resize for client edge */
SetWindowPos(
hWnd,
NULL,
0, 0,
rcWnd.right - rcWnd.left + 4,
rcWnd.bottom - rcWnd.top + 4,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE
);
}
}
}

return TRUE;
}

DWORD CALLBACK ObserverThreadProc(HANDLE handle)
{
HDESK curDesktop = GetThreadDesktop(GetCurrentThreadId());

for (int i = 0; i < 2; i++)
{
EnumDesktopWindows(curDesktop, EnumWindowsProc, NULL);
Sleep(1);
}

return 0;
}

void LoadSettings(void)
{
settings.classic = Wh_GetIntSetting(L"classic");
settings.clientedge = Wh_GetIntSetting(L"clientedge");
}

BOOL Wh_ModInit(void)
{
LoadSettings();

if (settings.classic)
{
WCHAR szPath[MAX_PATH], *pBackslash = NULL;
if (GetModuleFileNameW(NULL, szPath, MAX_PATH))
{
LPWSTR szPathL = wcslwr(szPath);
pBackslash = wcsrchr(szPathL, L'\\');
if (pBackslash && !wcscmp(pBackslash, L"\\conhost.exe"))
{
SetThemeAppProperties(STAP_ALLOW_NONCLIENT);
}
}
}

g_hObserverThread = CreateThread(NULL, NULL, ObserverThreadProc, NULL, NULL, NULL);
SetPriorityClass(g_hObserverThread, REALTIME_PRIORITY_CLASS);
SetThreadPriority(g_hObserverThread, THREAD_PRIORITY_TIME_CRITICAL);

return TRUE;
}

void Wh_ModSettingsChanged(void)
{
LoadSettings();
}

0 comments on commit ed112ac

Please sign in to comment.