Other questions here have not answered your part 2, so I will give this snapshot:
2. How do windows? (I'm more curious than anything, but if it's not difficult, maybe I could implement something like this in my own program?)
The idea is that even if you have several dozen windows open, each of which has many toolbars, each of which contains many elements, etc., every time you move the mouse, the windows do not have to check everything.
Windows is basically structured in two layers: there is HWND, how Windows itself manages the division of desktop space; and usually inside each HWND there is a control that manages its own space inside that HWND: a control list that manages its own list items, a tab control that manages its own tabs, an HTML control that manages its own layout of the HTML page, and so on . (Or, in your case, the code controls 50 or so rectangles.)
When the mouse moves, Windows first determines the correct HWND to send this WM_MOUSEMOVE to. And he does this by crossing the HWND. HWNDs are stored as a tree representing restraint and order among siblings representing Z-Order, so Windows can perform a simple descent into that tree to find the lowest HWND at any given point. If you run the Spy ++ application, you yourself will see what this HWND tree looks like. Please note that Windows does not perform a full exhaustive crawl: when passing through windows of top-level applications, for example, to find out which application the point is in, as soon as the windows detect the first top-level HWND that contains the point, this will work in this, directly ignoring all other applications that are below / after it, and all the controls inside them. This is the key, which means that only windows should go through relatively little HWND, even if there are many visible objects on the screen.
Once Windows determines the correct HWND, it will send the appropriate message (WM_NCHITTEST, WM_MOUSEMOVE, etc.), and then before that control will do the same for its own content. For a list containing items of a fixed size, defining an item at a specific point can be as simple as a division operation; or for an HTML control, the control can have its own equivalent of a "tree of layouts", which it can use to quickly cross the element at that point. In your case, scrolling through the list of rectangles can be great.
This is a slightly simplified version: it is a little more complicated than higher - for example. windows are not just direct checks, but also other checks that allow you to create odd and transparent windows (both invisible and disabled windows); but the basic idea of lowering the tree applies.
Another important issue to keep in mind is that it is all pretty fast: moving the mouse occurs in “human time”, and a modern processor can perform many operations in the time it takes for the mouse to move a couple of pixels on the screen. And finally, note that when you move the mouse from point A to point B on the screen, the mouse does not always cross each individual pixel between them - especially if you quickly move the mouse.