Last Modification: August 12, 2000

How do I subclass the Desktop Window?

You cannot subclass the real desktop window, as it is a hidden window, and you'll get strange results should you try it. 

However, the window most people call the desktop is in reality a ListView control, which is a child of the real desktop and it's owned by explorer.exe. You can find out this window using Spy++, which is included with Visual C++. Be aware, however, that it's not guaranteed that this will be so in all Windows versions, and it might change in the future.

To subclass the window you need to inject your code into explorer's process, because you can't directly subclass windows across process boundaries. The easiest way to do this is to put your subclassing code into a dll and using SetWindowsHookEx() to hook the window before subclassing it. That will cause Windows to load your dll into explorer's address space, and you can then proceed from there.

 

Finally, note that you should have a good reason for doing this, because messing up explorer can cause severe problems, specially in Win9x.

References and samples: