How to detect spawning and destruction of windows?
-
Hi. I am not quite sure if this is the correct place to put this, but I am using Qt for my project, so I believe it is fine.
I would like to write my own shell for windows 11. I thought I would first work on the taskbar, and so I began first with a QTimer which constantly kept checking for processes with a window title every 100ms. This would add a button to the taskbar for that window.It didnt seem like a good choice though. It would drive up the CPU usage and also I wanted to learn if there was a better way, more optimized to do it. I tried to use wmi as suggested by https://stackoverflow.com/questions/1986249/net-process-monitor/1986856#1986856 (for C#, not C++) and write my own implementation but it resulted in bugs.
The program would crash every so often when a new window was spawned, and some windows wouldn't even open. Eg. if i opened notepad, it would show the icon on the windows taskbar but the window would never appear. when my program crashes, it appears then.
My project uses LLVM, written in Qt framework for C++, any alternative methods and suggestions are appreciated 😊
-
I think you can do this with a so called shell hook library.
https://learn.microsoft.com/en-us/windows/win32/winmsg/about-hooks
See the part about monitoring messages. When a new window is created a WM_CREATE message is posted to the window's event queue.