Separate DISPLAY_MODE struct and associated functions into another file.

This commit is contained in:
2025-08-15 18:34:38 -07:00
parent 863471e08e
commit 0f66725951
5 changed files with 134 additions and 108 deletions

106
WinPowerDMS/DisplayMode.c Normal file
View File

@ -0,0 +1,106 @@
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#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(&params->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);
}
}

19
WinPowerDMS/DisplayMode.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
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);

View File

@ -8,6 +8,7 @@
#include <commctrl.h>
#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(&params->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;

View File

@ -144,9 +144,11 @@
</Manifest>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DisplayMode.c" />
<ClCompile Include="WinPowerDMS.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DisplayMode.h" />
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>

View File

@ -18,11 +18,17 @@
<ClCompile Include="WinPowerDMS.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DisplayMode.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DisplayMode.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="WinPowerDMS.rc">