How do I repurpose a window's close button wit

2019-05-23 00:55发布

问题:

I am making a program with native C++ and the WinAPI.

This program has a main application window that I want to be somewhat persistent. It seems kind of natural to close a window via the red X button, and I often accidentally press it when I don't actually want to exit the program (this happens to me with Teamspeak). So, I'm trying to repurpose the red X button into a minimize button to avoid accidental exiting.

However, my application should have some way to exit the program with standard system controls. I want to allow ALT+F4 and right clicking on the taskbar icon to close. I tried intercepting WM_CLOSE and SYSCOMMAND (SC_CLOSE) but this seems to disable all standard system methods of exiting the program (short of killing it in Process Explorer - not really a standard method).

Is there a way to only intercept the window's red X button? I noticed that Windows Live Messenger does this but I have no idea how.

回答1:

Handle WM_NCLBUTTONDOWN message and use WM_NCHITTEST to check it the click is about to be delivered to HTCLOSE control (this is what your red [X] resolves to). Or alternatively, handle WM_NCHITTEST itself and prevent from discovering HTCLOSE part of your window GUI by replacing return value with something else.