How can I override the close button in a third-party application so that it minimizes instead?

I would like to redefine the close button in a third-party application so that the application is minimized instead. I do not have source code for the target application.

  • Can I write this in C #? Or do I need to use C ++?
  • How to write such a hook? Do I need a process or is there enough driver / dll / service?

As far as I researched, I think I need to do something like this, but I don’t know exactly how:

Capture WH_GETMESSAGE to override WM_CLOSE to set Windows state to WS_MINIMIZE.

+4
source share
3 answers

You can do this in both C ++ and C #. To do this, you have to connect to the application message loop and redefine the WM_CLOSE message to WM_MINIMIZE. To connect to any running process, you can use:

  • Microsoft Detours (commercial and not free, if I remember correctly) (http://research.microsoft.com/en-us/projects/detours/)

  • EasyHook (Open source for LGPL) (http://easyhook.codeplex.com/)

I used EasyHook and I was very pleased with the results. This gives you really nice features, such as starting a process with hooks attached OR attaching hooks to already running processes. In addition, it provides you with both managed (C #) and proprietary connection libraries. I would recommend you take a look at it ...

+11
source

For C #, this can be done very simply:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (!realClose) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; } } 

Where realClose is a boolean, you set it to true when you want to close the application (for example, if the user does not click the close button and does not use file -> exit or some such)

+5
source

Do not do this. Disable the close button. Provide the Minimum button.

-4
source

All Articles