Skip to content

Instantly share code, notes, and snippets.

@NSG650
Last active October 16, 2023 16:42
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>
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};
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;
if (gCurrentPos.left <= 0 || gCurrentPos.left >= (gUsableAreaCoords.right - WINDOW_X)) { gVelocityX *= -1; }
if (gCurrentPos.top <= 0 || gCurrentPos.top >= (gUsableAreaCoords.bottom - WINDOW_Y)) { gVelocityY *= -1; }
GetWindowRect(hwnd, &gCurrentPos);
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();
GetCursorPos(&gLastMousePos);
SystemParametersInfo(SPI_GETWORKAREA, 0, &gUsableAreaCoords, 0);
WNDCLASS wc = {0};
wc.hInstance = hInstance;
wc.lpszClassName = "Something";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpfnWndProc = WindowProcessMessages;
RegisterClass(&wc);
CreateWindowA("Something", "Fun", WS_SYSMENU | WS_VISIBLE, WINDOW_X, WINDOW_Y, WINDOW_X, WINDOW_Y, NULL, NULL, NULL, NULL);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment