A windows machine has at least one
WindowStation[
^], containing various
Desktops[
^].
There is at least one interactive desktop (that correspond to the user working space, containing the top-level windows) and other desktops, like the logon one (occupied by the GINA interface) and the screen saver one.
When the computer is unlocked the OS switches from the GINA to the USER desktop.
This event causes the desktop window itself to be activated and hence repainted.
Now, if your program is started in the user space, yo ucan chek periodically if the current active desktop is the one you belong or another and act when it changes.
Alternatively, you can subclass the desktop window your program runs in, and check for WM_NCACTIVATE or WM_ACTIVATE.
This is what I would try.