Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:58:38+00:00 2026-06-17T22:58:38+00:00

See also: How can I tell if another instance of my program is already

  • 0

See also:
How can I tell if another instance of my program is already running?

i use the following code before starting my application, to check if another instance
of it is already started:

var _PreviousHandle : THandle;
begin
  _PreviousHandle := FindWindow('TfrmMainForm',nil);
  if _PreviousHandle <> 0 then
  begin
    ShowMessage('Application "" is already running!');
    SetForegroundWindow(_PreviousHandle);
    ShowWindow(_PreviousHandle, SW_SHOW);
    Application.Terminate;
    Exit;
  end;
...

However, if it has started, i need to show that application. The problem is after it is shown in this way the minimize button no longer works, and when i click the icon in the taskbar, it “unminimizes” and the animation that is shown is as if it was minimized. Am i missing something? is there a proper way to activate and show external application while it’s minimized?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:58:39+00:00Added an answer on June 17, 2026 at 10:58 pm

    Here is a complete project, which keeps running only one instance of the application, and which should bring already running instance window to front.

    You can download a testing project or try the code, which follows:

    Project1.dpr

    program Project1;
    
    uses
      Forms,
      Windows,
      Unit1 in 'Unit1.pas' {Form1};
    
    {$R *.res}
    
    var
      Mutex: THandle;
    const
      AppID = '{0AEEDBAF-2643-4576-83B1-8C9422726E98}';
    begin
      MessageID := RegisterWindowMessage(AppID);
    
      Mutex := CreateMutex(nil, False, AppID);
      if (Mutex <> 0) and (GetLastError = ERROR_ALREADY_EXISTS) then
      begin
        PostMessage(HWND_BROADCAST, MessageID, 0, 0);
        Exit;
      end;
    
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    

    Unit1.pas

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StrUtils, StdCtrls;
    
    type
      TForm1 = class(TForm)
      private
        function ForceForegroundWindow(WndHandle: HWND): Boolean;
        function ForceRestoreWindow(WndHandle: HWND; Immediate: Boolean): Boolean;
      protected
        procedure WndProc(var AMessage: TMessage); override;
      end;
    
    var
      Form1: TForm1;
      MessageID: UINT;
    
    implementation
    
    {$R *.dfm}
    
    { TForm1 }
    
    function TForm1.ForceForegroundWindow(WndHandle: HWND): Boolean;
    var
      CurrThreadID: DWORD;
      ForeThreadID: DWORD;
    begin
      Result := True;
      if (GetForegroundWindow <> WndHandle) then
      begin
        CurrThreadID := GetWindowThreadProcessId(WndHandle, nil);
        ForeThreadID := GetWindowThreadProcessId(GetForegroundWindow, nil);
        if (ForeThreadID <> CurrThreadID) then
        begin
          AttachThreadInput(ForeThreadID, CurrThreadID, True);
          Result := SetForegroundWindow(WndHandle);
          AttachThreadInput(ForeThreadID, CurrThreadID, False);
          if Result then
            Result := SetForegroundWindow(WndHandle);
        end
        else
          Result := SetForegroundWindow(WndHandle);
      end;
    end;
    
    function TForm1.ForceRestoreWindow(WndHandle: HWND;
      Immediate: Boolean): Boolean;
    var
      WindowPlacement: TWindowPlacement;
    begin
      Result := False;
      if Immediate then
      begin
        WindowPlacement.length := SizeOf(WindowPlacement);
        if GetWindowPlacement(WndHandle, @WindowPlacement) then
        begin
          if (WindowPlacement.flags and WPF_RESTORETOMAXIMIZED) <> 0 then
            WindowPlacement.showCmd := SW_MAXIMIZE
          else
            WindowPlacement.showCmd := SW_RESTORE;
          Result := SetWindowPlacement(WndHandle, @WindowPlacement);
        end;
      end
      else
        Result := SendMessage(WndHandle, WM_SYSCOMMAND, SC_RESTORE, 0) = 0;
    end;
    
    procedure TForm1.WndProc(var AMessage: TMessage);
    begin
      inherited;
      if AMessage.Msg = MessageID then
      begin
        if IsIconic(Handle) then
          ForceRestoreWindow(Handle, True);
        ForceForegroundWindow(Application.Handle);
      end;
    end;
    
    end.
    

    Tested on OS versions:

    • Windows 8.1 64-bit
    • Windows 7 SP1 64-bit Home Premium
    • Windows XP SP 3 32-bit Professional

    Known issues and limitations:

    • The MainFormOnTaskbar is not taken into account at all; it must be set to True at this time
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can print with printf() and puts() . I can also see
See also: Emacs behind HTTP proxy Is it possible to tell emacs to automatically
When I resize my window I want to tell another part of my program
Hi I have some simple code here that as far as I can tell
See also SO 1664677 I get this error 2: Syntax error: ( unexpected when
see also Differences between LINQ to Objects and LINQ to SQL queries We are
What are "iterable", "iterator", and "iteration" in Python? How are they defined? See also:
I have 3 main elements in my XML layout (also see the image): The
st = What kind of speCialist would we see for this?He also seems to
I'm working on a mobile phone application and I see a potential opportunity for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.