diff --git a/SporeBorderless/dllmain.cpp b/SporeBorderless/dllmain.cpp index 8160138..b414df4 100644 --- a/SporeBorderless/dllmain.cpp +++ b/SporeBorderless/dllmain.cpp @@ -1,42 +1,7 @@ // dllmain.cpp : Defines the entry point for the DLL application. #include "stdafx.h" -// -// Internal Variables -// - -static HWND l_SporeWindowHandle = nullptr; - -// -// Internal Functions -// - -static BOOL CALLBACK enum_windows_proc_callback(HWND hWnd, LPARAM lParam) -{ - if (l_SporeWindowHandle != nullptr) - { - return TRUE; - } - - DWORD processId; - HRESULT res = GetWindowThreadProcessId(hWnd, &processId); - - if (res != 0 && - processId == lParam) - { - // only match windows with SPORE in the name - char windowTitle[256+1] = { 0 }; - - GetWindowTextA(hWnd, windowTitle, 256); - - if (strstr(windowTitle, "SPORE") != nullptr) - { - l_SporeWindowHandle = hWnd; - } - } - - return TRUE; -} +#include // // Exported Functions @@ -44,14 +9,17 @@ static BOOL CALLBACK enum_windows_proc_callback(HWND hWnd, LPARAM lParam) void Initialize() { - EnumWindows(enum_windows_proc_callback, GetCurrentProcessId()); - if (l_SporeWindowHandle == nullptr) + App::Canvas* appCanvas = App::Canvas::Get(); + if (appCanvas == nullptr) { - MessageBoxA(nullptr, "couldn't find window handle!", "SporeBorderless", MB_OK | MB_ICONERROR); + MessageBoxA(nullptr, "couldn't retrieve app canvas!", "SporeBorderless", MB_OK | MB_ICONERROR); return; } - LONG windowFlags = GetWindowLongA(l_SporeWindowHandle, GWL_STYLE); + HWND windowHandle = appCanvas->GetWindow(); + HMONITOR monitorHandle = appCanvas->GetMonitor(); + + LONG windowFlags = GetWindowLongA(windowHandle, GWL_STYLE); if (windowFlags == 0) { MessageBoxA(nullptr, "couldn't retrieve window flags!", "SporeBorderless", MB_OK | MB_ICONERROR); @@ -61,30 +29,28 @@ void Initialize() windowFlags &= ~WS_BORDER; windowFlags &= ~WS_CAPTION; - LONG ret = SetWindowLongA(l_SporeWindowHandle, GWL_STYLE, windowFlags); + LONG ret = SetWindowLongA(windowHandle, GWL_STYLE, windowFlags); if (ret == 0) { MessageBoxA(nullptr, "couldn't apply new window flags!", "SporeBorderless", MB_OK | MB_ICONERROR); return; } - HMONITOR hMonitor = MonitorFromWindow(l_SporeWindowHandle, MONITOR_DEFAULTTOPRIMARY); - - MONITORINFO hMonitorInfo; - hMonitorInfo.cbSize = sizeof(MONITORINFO); - BOOL res = GetMonitorInfoA(hMonitor, &hMonitorInfo); + MONITORINFO monitorInfo; + monitorInfo.cbSize = sizeof(MONITORINFO); + BOOL res = GetMonitorInfoA(monitorHandle, &monitorInfo); if (res == FALSE) { MessageBoxA(nullptr, "couldn't retrieve monitor information!", "SporeBorderless", MB_OK | MB_ICONERROR); return; } - const int width = hMonitorInfo.rcMonitor.right - hMonitorInfo.rcMonitor.left; - const int height = hMonitorInfo.rcMonitor.bottom - hMonitorInfo.rcMonitor.top; - res = SetWindowPos(l_SporeWindowHandle, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED); + const int width = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; + const int height = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; + res = SetWindowPos(windowHandle, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED); if (res == FALSE) { - MessageBoxA(nullptr, "couldn't refresh window to force window flag change!", "SporeBorderless", MB_OK | MB_ICONERROR); + MessageBoxA(nullptr, "couldn't set window position and size!", "SporeBorderless", MB_OK | MB_ICONERROR); return; } }