Skip to content

Commit

Permalink
simplify implementation by using App::Canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Sep 21, 2023
1 parent ac519fe commit 33e9f50
Showing 1 changed file with 16 additions and 50 deletions.
66 changes: 16 additions & 50 deletions SporeBorderless/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
// 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 <Spore/App/Canvas.h>

//
// Exported Functions
//

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);
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 33e9f50

Please sign in to comment.