5

I would like to override the close button on a third-party application so that it causes the application to be minimized instead. I do not have source code for the target application.

  • 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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • 2
    -1 for underspecified/misleading question. The OP has clarified through comments that it's about modifying the behavior of an application that he does not (presumably) have source code for. – Cheers and hth. - Alf Oct 16 '10 at 00:46
  • 1
    Why would one need source code if I'm willing to write a hook? – Tamara Wijsman Oct 16 '10 at 00:48
  • 2
    Alf's comment is just noise, please ignore it. – Tamara Wijsman Oct 16 '10 at 00:56
  • 1
    I agree with Alf. The OP never stated that he wanted to modify the behavior of an existing, compiled application. I, myself, assumed he wanted code to override the close button I was going to suggest handling the FormClosing event. "Hooking" does not imply he does not have the code to the application he's trying to modify. – BeemerGuy Oct 16 '10 at 02:41
  • 1
    @Beemer: If I was writing the application myself I wouldn't be hooking, and I also said "specific" application and "an" application. Rather than adding up yet another noise comment, if you don't like the question in it's current form you are free to improve it... Even when you still interpret it as both then one could at least give a general answer instead of intentional noise mocking, I did never state it was about my own application. – Tamara Wijsman Oct 16 '10 at 11:55
  • I do agree with @Alf. I like that he does not shy away to downvote like that. – Johannes Schaub - litb Oct 18 '10 at 15:49
  • @TomWij, I respect an OP's writing to the extent that I think I have no privilege to change it; all we're saying is that we were unclear on what you were asking. You're saying "I did never state it was about my own application," well nor did you state it was someone else's. Just a miscommunication, that's all; we're only human. – BeemerGuy Oct 18 '10 at 21:43
  • 8
    Yo, guys. He is asking could he use C# or C++... If he had his own application, he wouldn't be asking which language to use, now would he? Also, he's asking if he needs to write a dll or separate process... Come on, it's clear that it's not his application. Even if you think it's unclear, ask the OP to edit his post, don't down-vote - that's just rude imho. – data May 15 '11 at 17:42
  • @HarryJohnston, your edit totally changes the question. You should ask a new question (assuming this one doesn't answer if). – Howli May 05 '14 at 21:55
  • @Howlin: no, my edit clarifies the OPs meaning. Did you read the comments where this was discussed? – Harry Johnston May 05 '14 at 23:18
  • @Howlin: in particular note the comment (from the deleted answer) which starts "I'm not talking about my own application but a third-party application" ... – Harry Johnston May 05 '14 at 23:20

3 Answers3

12

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...

  • 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? – Tamara Wijsman Oct 16 '10 at 00:52
  • 2
    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 Oct 16 '10 at 00:56
4

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)

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
-4

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

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 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. – Tamara Wijsman Oct 16 '10 at 00:27
  • 1
    @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"). – Cheers and hth. - Alf Oct 16 '10 at 00:35
  • 2
    "For a specific application" and "hook" are already in the question. No need to guess... It's just you. – Tamara Wijsman Oct 16 '10 at 00:39
  • 1
    @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. – Cheers and hth. - Alf Oct 16 '10 at 00:44
  • I'm talking about a kind of hook, not "something like" a hook... It's just you. – Tamara Wijsman Oct 16 '10 at 00:46
  • @TomVij: I was *quoting* you. Your question still says "something like". – Cheers and hth. - Alf Oct 16 '10 at 00:47
  • 1
    If you don't understand the sentences above it, then why quote it? It's just you. – Tamara Wijsman Oct 16 '10 at 00:54
  • 4
    I disagree, this can be very convenient for programs you usually want to keep in the background (uTorrent, Thunderbird, etc) – Ohad Schneider Oct 16 '10 at 12:43
  • @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 Oct 18 '10 at 15:18
  • @Johannes Schaub - Oh, in the case we are not talking about minimization to the tray, you're absolutely right - that would be annoying – Ohad Schneider Oct 18 '10 at 15:45