From 0f6672595104b835b668157963dd9776c2c3e402 Mon Sep 17 00:00:00 2001 From: Mina Brown Date: Fri, 15 Aug 2025 18:34:38 -0700 Subject: [PATCH] Separate DISPLAY_MODE struct and associated functions into another file. --- WinPowerDMS/DisplayMode.c | 106 +++++++++++++++++++++++ WinPowerDMS/DisplayMode.h | 19 +++++ WinPowerDMS/WinPowerDMS.c | 109 +----------------------- WinPowerDMS/WinPowerDMS.vcxproj | 2 + WinPowerDMS/WinPowerDMS.vcxproj.filters | 6 ++ 5 files changed, 134 insertions(+), 108 deletions(-) create mode 100644 WinPowerDMS/DisplayMode.c create mode 100644 WinPowerDMS/DisplayMode.h diff --git a/WinPowerDMS/DisplayMode.c b/WinPowerDMS/DisplayMode.c new file mode 100644 index 0000000..926561d --- /dev/null +++ b/WinPowerDMS/DisplayMode.c @@ -0,0 +1,106 @@ +#include + +#define WIN32_LEAN_AND_MEAN +#include + +#include "DisplayMode.h" + +BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b) { + return a->width == b->width && a->height == b->height && a->refresh == b->refresh; +} + +// I hate parsing the string here, but the alternative requires extra memory management. +DISPLAY_MODE GetModeFromCB(HWND hComboBox) { + DISPLAY_MODE mode = { 0 }; + LRESULT selectedIndex = SendMessage(hComboBox, CB_GETCURSEL, 0, 0); + if (selectedIndex != CB_ERR) { + LRESULT len = SendMessage(hComboBox, CB_GETLBTEXTLEN, selectedIndex, 0); + if (len != CB_ERR) { + LPWSTR text = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*text)); + if (text) { + SendMessage(hComboBox, CB_GETLBTEXT, selectedIndex, (LPARAM)text); + swscanf_s(text, L"%dx%d @ %d Hz", &mode.width, &mode.height, &mode.refresh); + HeapFree(GetProcessHeap(), 0, text); + } + } + } + + return mode; +} + +// returns the result of the ChangeDisplaySettings call that this results in. +LONG ChangeDisplayMode(const DISPLAY_MODE* mode, DWORD dwFlags) { + DEVMODE devMode = { + .dmSize = sizeof(devMode), + .dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY, + .dmPelsWidth = mode->width, + .dmPelsHeight = mode->height, + .dmDisplayFrequency = mode->refresh + }; + + return ChangeDisplaySettings(&devMode, dwFlags); +} + +struct MessageBoxParams { + HWND hWnd; + LPCWSTR lpText; + LPCWSTR lpCaption; + UINT uType; +}; + +static DWORD WINAPI MessageBoxAsync(LPVOID lpParam) { + struct MessageBoxParams* params = lpParam; + return MessageBox(params->hWnd, params->lpText, params->lpCaption, params->uType); +} + +struct TestDisplayModeParams { + HWND hDlg; + DISPLAY_MODE mode; +}; + +static DWORD WINAPI TestDisplayModeThread(LPVOID lpParam) { + struct TestDisplayModeParams* params = lpParam; + DEVMODE originalMode = { .dmSize = sizeof(originalMode) }; + EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &originalMode); + ChangeDisplayMode(¶ms->mode, CDS_FULLSCREEN); + + // Create the message box about the resolution change + WCHAR msgText[96]; + swprintf_s(msgText, sizeof(msgText) / sizeof(msgText[0]), + L"Testing %dx%d @ %d Hz\nThe display mode will reset back in 10 seconds.", + params->mode.width, params->mode.height, params->mode.refresh); + struct MessageBoxParams mbParams = { + .hWnd = params->hDlg, + .lpText = msgText, + .lpCaption = L"Resolution Test", + .uType = MB_OK | MB_ICONINFORMATION + }; + HANDLE mbThread = CreateThread(NULL, 0, MessageBoxAsync, &mbParams, 0, NULL); + if (mbThread) { + CloseHandle(mbThread); + Sleep(10000); + ChangeDisplaySettings(&originalMode, 0); + } + else { + MessageBox(params->hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); + } + HeapFree(GetProcessHeap(), 0, params); + return 0; +} + +void TestDisplayMode(HWND hDlg, DISPLAY_MODE* mode) { + struct TestDisplayModeParams* tdmParams = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdmParams)); + if (tdmParams) { + tdmParams->hDlg = hDlg; + tdmParams->mode = *mode; + HANDLE tdmThread = CreateThread(NULL, 0, TestDisplayModeThread, tdmParams, 0, NULL); + if (tdmThread) CloseHandle(tdmThread); + else { + HeapFree(GetProcessHeap(), 0, tdmParams); + MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); + } + } + else { + MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); + } +} \ No newline at end of file diff --git a/WinPowerDMS/DisplayMode.h b/WinPowerDMS/DisplayMode.h new file mode 100644 index 0000000..c084509 --- /dev/null +++ b/WinPowerDMS/DisplayMode.h @@ -0,0 +1,19 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN +#include + +typedef struct { + DWORD width; + DWORD height; + DWORD refresh; +} DISPLAY_MODE; + +BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b); + +DISPLAY_MODE GetModeFromCB(HWND hComboBox); + +// returns the result of the ChangeDisplaySettings call that this results in. +LONG ChangeDisplayMode(const DISPLAY_MODE* mode, DWORD dwFlags); + +void TestDisplayMode(HWND hDlg, DISPLAY_MODE* mode); \ No newline at end of file diff --git a/WinPowerDMS/WinPowerDMS.c b/WinPowerDMS/WinPowerDMS.c index f493d9a..34e80c2 100644 --- a/WinPowerDMS/WinPowerDMS.c +++ b/WinPowerDMS/WinPowerDMS.c @@ -8,6 +8,7 @@ #include #include "resource.h" +#include "DisplayMode.h" // Link Common Controls v6 for visual styling #pragma comment(linker,"\"/manifestdependency:type='win32' \ @@ -17,114 +18,6 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") #define WM_TRAYICON (WM_USER + 1) #define TRAY_ICON_ID 1001 -typedef struct { - DWORD width; - DWORD height; - DWORD refresh; -} DISPLAY_MODE; - -BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b) { - return a->width == b->width && a->height == b->height && a->refresh == b->refresh; -} - -#define MODE_SELECTED(mode) (!DisplayModeEquals(&mode, &(DISPLAY_MODE) { 0 })) - -// I hate parsing the string here, but the alternative requires extra memory management. -static DISPLAY_MODE GetModeFromCB(HWND hComboBox) { - DISPLAY_MODE mode = { 0 }; - LRESULT selectedIndex = SendMessage(hComboBox, CB_GETCURSEL, 0, 0); - if (selectedIndex != CB_ERR) { - LRESULT len = SendMessage(hComboBox, CB_GETLBTEXTLEN, selectedIndex, 0); - if (len != CB_ERR) { - LPWSTR text = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*text)); - if (text) { - SendMessage(hComboBox, CB_GETLBTEXT, selectedIndex, (LPARAM) text); - swscanf_s(text, L"%dx%d @ %d Hz", &mode.width, &mode.height, &mode.refresh); - HeapFree(GetProcessHeap(), 0, text); - } - } - } - - return mode; -} - -// returns the result of the ChangeDisplaySettings call that this results in. -static LONG ChangeDisplayMode(const DISPLAY_MODE* mode, DWORD dwFlags) { - DEVMODE devMode = { - .dmSize = sizeof(devMode), - .dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY, - .dmPelsWidth = mode->width, - .dmPelsHeight = mode->height, - .dmDisplayFrequency = mode->refresh - }; - - return ChangeDisplaySettings(&devMode, dwFlags); -} - -struct MessageBoxParams { - HWND hWnd; - LPCWSTR lpText; - LPCWSTR lpCaption; - UINT uType; -}; - -static DWORD WINAPI MessageBoxAsync(LPVOID lpParam) { - struct MessageBoxParams* params = lpParam; - return MessageBox(params->hWnd, params->lpText, params->lpCaption, params->uType); -} - -struct TestDisplayModeParams { - HWND hDlg; - DISPLAY_MODE mode; -}; - -static DWORD WINAPI TestDisplayModeThread(LPVOID lpParam) { - struct TestDisplayModeParams* params = lpParam; - DEVMODE originalMode = { .dmSize = sizeof(originalMode) }; - EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &originalMode); - ChangeDisplayMode(¶ms->mode, CDS_FULLSCREEN); - - // Create the message box about the resolution change - WCHAR msgText[96]; - swprintf_s(msgText, sizeof(msgText) / sizeof(msgText[0]), - L"Testing %dx%d @ %d Hz\nThe display mode will reset back in 10 seconds.", - params->mode.width, params->mode.height, params->mode.refresh); - struct MessageBoxParams mbParams = { - .hWnd = params->hDlg, - .lpText = msgText, - .lpCaption = L"Resolution Test", - .uType = MB_OK | MB_ICONINFORMATION - }; - HANDLE mbThread = CreateThread(NULL, 0, MessageBoxAsync, &mbParams, 0, NULL); - if (mbThread) { - CloseHandle(mbThread); - Sleep(10000); - ChangeDisplaySettings(&originalMode, 0); - } - else { - MessageBox(params->hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); - } - HeapFree(GetProcessHeap(), 0, params); - return 0; -} - -static void TestDisplayMode(HWND hDlg, DISPLAY_MODE* mode) { - struct TestDisplayModeParams* tdmParams = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdmParams)); - if (tdmParams) { - tdmParams->hDlg = hDlg; - tdmParams->mode = *mode; - HANDLE tdmThread = CreateThread(NULL, 0, TestDisplayModeThread, tdmParams, 0, NULL); - if (tdmThread) CloseHandle(tdmThread); - else { - HeapFree(GetProcessHeap(), 0, tdmParams); - MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); - } - } - else { - MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR); - } -} - typedef struct { DISPLAY_MODE modeBatt; DISPLAY_MODE modeAC; diff --git a/WinPowerDMS/WinPowerDMS.vcxproj b/WinPowerDMS/WinPowerDMS.vcxproj index 1238c70..2b0748b 100644 --- a/WinPowerDMS/WinPowerDMS.vcxproj +++ b/WinPowerDMS/WinPowerDMS.vcxproj @@ -144,9 +144,11 @@ + + diff --git a/WinPowerDMS/WinPowerDMS.vcxproj.filters b/WinPowerDMS/WinPowerDMS.vcxproj.filters index 9235a1c..30a033d 100644 --- a/WinPowerDMS/WinPowerDMS.vcxproj.filters +++ b/WinPowerDMS/WinPowerDMS.vcxproj.filters @@ -18,11 +18,17 @@ Source Files + + Source Files + Header Files + + Header Files +