views:

102

answers:

3

I would like to override the close button to minimize an application instead.

  • Can I write such thing in C#? Or do I need to use C++?
  • How do I write this kind of hook? Do I need a process running or would a driver/dll/service suffice?

As far as I got researching I think I have to do something like this but I don't know how exactly:

A WH_GETMESSAGE hook to override WM_CLOSE to set the Windows status to WS_MINIMIZE.

A: 

Don't do that. Disable the close button. Provide a minimize button.

Alf P. Steinbach
I'm not talking about my own application but a third-party application which behaves differently than other applications, which gives a different user experience for that particular application... It's easier to hit "close or alt+f4" than "minimize or win+m", it would be nice to override that close behavior to minimize instead.
TomWij
@TomWij: Edit your question to provide those requirements, please. Neither nor others who potentially could help you are telepaths, we can't guess what you don't say. And it does make a huge difference in the tehcnical solution (although the Right(TM) solution is as I wrote and as presumably you downvoted: "do not do that").
Alf P. Steinbach
"For a specific application" and "hook" are already in the question. No need to guess... It's just you.
TomWij
@TomWij: Re hints already in the question: neither I nor others who could help you are telepaths. Wehen you say you don't know exactly but perhaps a solution could be "something like" a hook, we don't know whether you're guessing wildly about a possible solution, or what. That you were trying to hint something about the problem comes as a surprise. It's much easier to just say it right out.
Alf P. Steinbach
I'm talking about a kind of hook, not "something like" a hook... It's just you.
TomWij
@TomVij: I was *quoting* you. Your question still says "something like".
Alf P. Steinbach
If you don't understand the sentences above it, then why quote it? It's just you.
TomWij
I disagree, this can be very convenient for programs you usually want to keep in the background (uTorrent, Thunderbird, etc)
ohadsc
@ohadsc, that's not what the questioner wants to do. He wants to minimize it instead. I think that's an awful thing to do for a close button. For a close button, I see two options: 1) Close the window, but keep the application alive (in the systray) 2) Close the application. For something like thunderbird, I think option 1 fits well.
Johannes Schaub - litb
@Johannes Schaub - Oh, in the case we are not talking about minimization to the tray, you're absolutely right - that would be annoying
ohadsc
+1  A: 

You can do it in both C++ and C#. To do this, you would have to hook into the applications message loop and override the WM_CLOSE message to WM_MINIMIZE. To hook into any process that's running you can use:

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

  2. EasyHook (Open source under LGPL) (http://easyhook.codeplex.com/)

I've used EasyHook and I was very satisfied with the results. It gives you really nice features like starting up a process with the hooks attached OR attaching hooks to already running processes. Also, it provides you with both managed(C#) and native hooking libraries. I'd recommend you take a look at it...

Achintya Sharma
Now that is helpful, amazing to see that there are libraries written for .NET to allow one to do that. So, if I understand correctly: I would have to write a loader which starts the application with a DLL hook attached?
TomWij
Yes. You can write an exe which would inject your hooking dll (managed or unmanaged) into the target app and start it. EasyHook has fully working examples to get you started :)
Achintya Sharma
Thank you. :-)` `
TomWij
+1  A: 

For C# this can be done in a very simple manner:

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

Where realClose is a boolean you set to true when you DO want the application to close (e.g. not when the user presses the close button, rather when he uses file -> exit or some such)

ohadsc