Games like that are called screenmates or desktop pets. They are a type of digital pet that interacts with desktop windows.
Positioning
Desktop pets query the host operating system's windowing system for window positions. The windowing system is typically DWM on Windows and X11 on Linux.
On Windows, you can include Windows.h
and use EnumWindows
to get a list of windows, then GetWindowRect
to find its size and position.
On Linux (using X11), the equivalent calls are XQueryTree
and XGetWindowAttributes
.
Rendering
Most screenmates are rendered with each little character in its own desktop window. The windowing system's API is used to give that window a transparent background and to remove its borders (like this on Windows and this on Linux), to move those windows around, to keep them on top of other windows, and to receive pointer events.
Since using windowing system APIs directly is often very complex, you might want to look into using a UI toolkit such as GTK+ or Qt, which are wrappers for the core windowing functionality. Some are even cross-platform.
Some helpful links
For Linux, the open source Linux desktop pets XPenguins and oneko may be helpful. Xsnow demonstrates window position queries more minimally.
For Windows, here's a series of articles on replicating xsnow in C++ and C#.