Skip to content

Commit

Permalink
Possible final fix for secondary display taskbars.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgjackson committed Feb 12, 2024
1 parent 4c8e43f commit 703ce18
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
81 changes: 67 additions & 14 deletions toggle.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,55 @@ int IsLight(void)
return -1; // error
}

// Data for sending messages to specific windows
typedef struct {
UINT msg;
WPARAM wParam;
LPARAM lParam;
TCHAR *className;
TCHAR *classNameAlt;
UINT count;
} send_message_t;

BOOL CALLBACK SendEnumWindowsProc(HWND hWnd, LPARAM lParam)
{
send_message_t *msgData = (send_message_t *)lParam;
if (!hWnd) return TRUE;

// Filter by class name
if (msgData->className != NULL || msgData->classNameAlt != NULL)
{
TCHAR szClassName[256];
GetClassName(hWnd, szClassName, sizeof(szClassName) / sizeof(szClassName[0]));

BOOL match = false;
if (msgData->className != NULL && _tcscmp(szClassName, msgData->className) == 0) match = true;
if (msgData->classNameAlt != NULL && _tcscmp(szClassName, msgData->classNameAlt) == 0) match = true;

if (!match) return TRUE;
}

SendMessage(hWnd, msgData->msg, msgData->wParam, msgData->lParam);
//PostMessage(hWnd, msgData->msg, msgData->wParam, msgData->lParam);
msgData->count++;
return TRUE;
}

// Send a message just to non-primary display taskbars (which seem a bit troublesome...)
UINT SendToTaskbar(UINT msg, WPARAM wParam, LPARAM lParam)
{
send_message_t msgData = {0};
msgData.msg = msg;
msgData.wParam = wParam;
msgData.lParam = lParam;
msgData.className = (TCHAR *)&TEXT("Shell_TrayWnd");
msgData.classNameAlt = (TCHAR *)&TEXT("Shell_SecondaryTrayWnd");
msgData.count = 0;
BOOL result = EnumWindows(SendEnumWindowsProc, (LPARAM)&msgData);
if (!result) return 0;
return (UINT)msgData.count;
}

BOOL SetLightDark(DWORD light)
{
HKEY hKeyMain = HKEY_CURRENT_USER;
Expand All @@ -429,7 +478,7 @@ BOOL SetLightDark(DWORD light)
{
_tprintf(TEXT("ERROR: RegOpenKeyEx() failed (%d): %ls\n"), lErrorCode, subKey);
return FALSE;
}
}

// Set the system key
lErrorCode = RegSetValueEx(hKey, valueSystem, 0, REG_DWORD, (const BYTE *)&light, sizeof(light));
Expand Down Expand Up @@ -459,13 +508,17 @@ BOOL SetLightDark(DWORD light)
RegCloseKey(hKey);
return FALSE;
}

RegCloseKey(hKey);

// Delay -- this may to help update taskbars on other displays?
Sleep(500);
//Sleep(500);

// Broadcast WM_SETTINGSCHANGE using SendMessageTimeout with lParam set to "ImmersiveColorSet" -- required by some apps such as explorer.exe (File Explorer)
// Send WM_SETTINGCHANGE message directly to primary and secondary taskbars
UINT countSC = SendToTaskbar(WM_SETTINGCHANGE, 0, (LPARAM)TEXT("ImmersiveColorSet"));
_tprintf(TEXT("SendToTaskbar(WM_SETTINGCHANGE): %d\n"), countSC);

// Broadcast WM_SETTINGCHANGE using SendMessageTimeout with lParam set to "ImmersiveColorSet" -- required by some apps such as explorer.exe (File Explorer)
{
HWND hWnd = HWND_BROADCAST;
UINT msg = WM_SETTINGCHANGE;
Expand All @@ -474,16 +527,7 @@ BOOL SetLightDark(DWORD light)
SendMessageTimeout(hWnd, msg, wParam, lParam, SMTO_ABORTIFHUNG, 5000, NULL);
}

// Broadcast WM_THEMECHANGED -- this may to help update taskbars on other displays?
{
HWND hWnd = HWND_BROADCAST;
UINT msg = WM_THEMECHANGED;
WPARAM wParam = 0;
LPARAM lParam = 0;
SendMessageTimeout(hWnd, msg, wParam, lParam, SMTO_ABORTIFHUNG, 5000, NULL);
}

// Broadcast a second WM_SETTINGSCHANGE using SendMessageTimeout with lParam set to "ImmersiveColorSet" -- this second broadcast fixes Task Manager
// Broadcast a second WM_SETTINGCHANGE using SendMessageTimeout with lParam set to "ImmersiveColorSet" -- this second broadcast fixes Task Manager
{
HWND hWnd = HWND_BROADCAST;
UINT msg = WM_SETTINGCHANGE;
Expand All @@ -492,6 +536,15 @@ BOOL SetLightDark(DWORD light)
SendMessageTimeout(hWnd, msg, wParam, lParam, SMTO_ABORTIFHUNG, 5000, NULL);
}

// // Broadcast WM_THEMECHANGED
// {
// HWND hWnd = HWND_BROADCAST;
// UINT msg = WM_THEMECHANGED;
// WPARAM wParam = 0;
// LPARAM lParam = 0;
// SendMessageTimeout(hWnd, msg, wParam, lParam, SMTO_ABORTIFHUNG, 5000, NULL);
// }

// // Broadcast WM_SYSCOLORCHANGE
// {
// HWND hWnd = HWND_BROADCAST;
Expand Down
2 changes: 1 addition & 1 deletion toggle.rc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ MAINICON ICON "icon.ico"
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define VER_MAJOR 1
#define VER_MINOR 0
#define VER_BUILD 12 // Patch ('build' in MS version order)
#define VER_BUILD 13 // Patch ('build' in MS version order)
#define VER_REVISION 0 // Build ('revision' in MS version order)
#define VER_STRING STRINGIZE(VER_MAJOR) "." STRINGIZE(VER_MINOR) "." STRINGIZE(VER_BUILD) "." STRINGIZE(VER_REVISION)

Expand Down

0 comments on commit 703ce18

Please sign in to comment.