?
Solved

Bring previous application instance to front

Posted on 2003-02-22
2
Medium Priority
?
843 Views
Last Modified: 2012-06-21
I hide my application to the system tray, then try to prevent another instance of the application to be run but are unable to bring the first instance to the front again.

Here is my code to hide the application to the system tray:
  ShowWindow(Application.Handle, SW_HIDE);
  ShowWindow(Application.MainForm.Handle, SW_HIDE);

to prevent I use:
  Mutex := CreateMutex(nil, False, AppTitle);
  if (GetLastError = ERROR_ALREADY_EXISTS) or
     (Mutex = 0) then
     Application.Terminate;

But how do I bring my previous instance of my application to the front?

Thanx in advance!
0
Comment
Question by:skynergy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 7998348
You have to use FindWindow() to find handle of main form of previous instance and then call ShowWindow()
ziolko.
0
 
LVL 6

Accepted Solution

by:
DaFox earned 200 total points
ID: 8159253
Hi.

// OnceOnly by Peter Below
Unit OnceOnly;

Interface

Function AlreadyRunning(const AppTitle: String): Boolean;

Implementation

Uses Messages, Windows, Sysutils;

var
  mutex: THandle = 0;

function AlreadyRunning(const AppTitle: String): Boolean;
var
  wnd: HWND;
begin
  mutex := CreateMutex( nil, false, PChar(StringReplace(AppTitle, ' ', '_', [rfReplaceAll] )));
  if mutex = 0 then Result := false else Result := GetLastError = ERROR_ALREADY_EXISTS;
  if Result then
  begin
    wnd := FindWindow('TApplication', PChar(AppTitle));
    if (wnd <> 0) then
    begin
      if IsIconic(wnd) then SendMessage(wnd, WM_SYSCOMMAND, SC_RESTORE, 0);
      SetForegroundWindow(wnd);
    end;
  end;
end;

initialization

finalization
  if (mutex <> 0) then CloseHandle(mutex);
end.


Markus
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question