Search code examples
windowsdelphidelphi-6

How to make other application hidden window visible from my application?


My application hides its window (and hence the task bar button) and stays only in the notification area, with the following code:

Form1.Hide;

When I click on the notification icon, the window shows itself with the following code:

Form1.Show;
Application.Restore;

But I want to show the window when the second instance is executed. I can pass any data from first instance to the second using "file mapping". So far I have only managed how to show the window if it is only minimized (the task bar button exists), with this code:

if IsIconic(FirstInstanceApplicationHandle) then ShowWindow(FirstInstanceApplicationHandle, SW_RESTORE);
SetForegroundWindow(FirstInstanceApplicationHandle);

So how I make visible first instance window from the second instance?


Solution

  • The cleanest way is to send a message to the first application to get it to restore itself. There's a whole load of code that executes when the application restores itself. That's what you get with the call you make to Application.Restore. That's what you need to happen.

    Now, it's hard to get that to happen from the outside. So get the second app to send a message to the first, and then let the first app restore itself. Do call SetForegroundWindow as well, as you currently do. Your second app can gift that, but the first app cannot otherwise take it.

    You'll need to allow the second app to discover a window handle in the first app to send the message to. Your file mapping approach will allow that. Do make sure that you use a window handle not subject to recreation. For example the Application handle or one made with AllocateHWnd.