basically its like this,
there are variables that the os can send to the program like where the mouse cursor is, what keys are being pressed ect. the program catches these variables and does what it wants with them (like rendering the cursor or whatever).
its easier to explain this with some psudo-code:
while (WINDOW_HAS_FOCUS AND I_want_to_draw_something)
int draw_cursor (CURSOR_LOCATION, BUTTON_PRESSED)
-------do some stuff-----
I_want_to_draw_something = false;
so every cycle the os gives the program, the condition WINDOW_HAS_FOCUS is checked. of its true, and the program wants to draw the cursor, then the function is called and draws the cursor.
thats the basic idea of windows programming, what visual studio does is it turns all that into objects
so you would have something like this:
yeah I know that's not anywhere near valid C++ but whatever, its psudocode. anyway, when you write something in visual studio to do something on an event all it does is create a method that gets called when one of those variables the os gives you is true.