Skip to content

Instantly share code, notes, and snippets.

@NSG650
Last active September 26, 2025 21:08
Show Gist options
  • Save NSG650/74143df2a3eaea089e68cea8f551ba1d to your computer and use it in GitHub Desktop.
Save NSG650/74143df2a3eaea089e68cea8f551ba1d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h>
#include <vector>
RECT gUsableAreaCoords = {0};
RECT gCurrentPos = {0};
INT gVelocityX = 5;
INT gVelocityY = 5;
DWORD gLast = 0;
#define WINDOW_X 320
#define WINDOW_Y 200
POINT gLastMousePos = {0};
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
std::vector<RECT>* windows = reinterpret_cast<std::vector<RECT>*>(lParam);
RECT rc = {0};
if (!GetWindowRect(hwnd, &rc)) {
return TRUE;
}
if (!IsWindowVisible(hwnd) || IsZoomed(hwnd)) {
return TRUE;
}
char string[128] = {0};
GetWindowTextA(hwnd, string, 128);
if (strlen(string) == 0 || !strcmp(string, "Start") || !strcmp(string, "Program Manager")) {
return TRUE;
}
DWORD processId = 0;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == GetCurrentProcessId()) {
return TRUE;
}
windows->push_back(rc);
return TRUE;
}
VOID CollisionCheck(VOID) {
std::vector<RECT> windows;
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows));
// I wrote this late at night can be better
for (int i = 0; i < windows.size(); i++) {
if (gCurrentPos.left < windows[i].right && gCurrentPos.right > windows[i].left) {
if (gCurrentPos.top < windows[i].bottom && gCurrentPos.bottom > windows[i].top) {
if (gVelocityX > 0 && gCurrentPos.right > windows[i].left && gCurrentPos.left < windows[i].left) {
gVelocityX *= -1;
}
else if (gVelocityX < 0 && gCurrentPos.left < windows[i].right && gCurrentPos.right > windows[i].right) {
gVelocityX *= -1;
}
if (gVelocityY > 0 && gCurrentPos.bottom > windows[i].top && gCurrentPos.top < windows[i].top) {
gVelocityY *= -1;
}
else if (gVelocityY < 0 && gCurrentPos.top < windows[i].bottom && gCurrentPos.bottom > windows[i].bottom) {
gVelocityY *= -1;
}
}
}
}
if (gCurrentPos.left < gUsableAreaCoords.left || gCurrentPos.right > gUsableAreaCoords.right) {
gVelocityX *= -1;
}
if (gCurrentPos.top < gUsableAreaCoords.top || gCurrentPos.bottom > gUsableAreaCoords.bottom) {
gVelocityY *= -1;
}
}
LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam) {
DWORD now = GetTickCount();
POINT currMousePos = {0};
GetCursorPos(&currMousePos);
if (currMousePos.x != gLastMousePos.x || currMousePos.y != gLastMousePos.y) { PostQuitMessage(0); }
if ((now - gLast) > 10) {
gLast = now;
GetWindowRect(hwnd, &gCurrentPos);
CollisionCheck();
gCurrentPos.left += gVelocityX;
gCurrentPos.top += gVelocityY;
SetWindowPos(hwnd, 0, gCurrentPos.left, gCurrentPos.top, WINDOW_X, WINDOW_Y, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
RECT rc = {0};
GetClientRect(hwnd, &rc);
InvalidateRect(hwnd, &rc, TRUE);
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, param, lparam);
}
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
gLast = GetTickCount();
srand(gLast);
GetCursorPos(&gLastMousePos);
SystemParametersInfo(SPI_GETWORKAREA, 0, &gUsableAreaCoords, 0);
WNDCLASS wc = {0};
wc.hInstance = hInstance;
wc.lpszClassName = L"Something";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpfnWndProc = WindowProcessMessages;
RegisterClass(&wc);
int posX = rand() % (gUsableAreaCoords.right - WINDOW_X);
int posY = rand() % (gUsableAreaCoords.bottom - WINDOW_Y);
CreateWindowA("Something", "Fun", WS_SYSMENU | WS_VISIBLE, posX, posY, WINDOW_X, WINDOW_Y, NULL, NULL, NULL, NULL);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
@default-writer
Copy link

default-writer commented Sep 21, 2025

Hi, you are missing SS specific code for registering SS on Windows list of SS since it is available for Windows 95/2000/XP/7/8/10, so it will be available only as .EXE and not as actual SS with configurable parameters visible on a Desktop tab of SS properties.

@NSG650
Copy link
Author

NSG650 commented Sep 22, 2025

Hi, you are missing SS specific code for registering SS on Windows list of SS since it is available for Windows 95/2000/XP/7/8/10, so it will be available only as .EXE and not as actual SS with configurable parameters visible on a Desktop tab of SS properties.

Interesting I'll take a look at this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment