Solved

How to popup/activate a main window of application?

Posted on 2010-08-31
3
1,609 Views
Last Modified: 2012-05-10
Hi.

It is Borland Delphi 5 forms application.

I need to make application running a single instance only.
If user trying to start a new instance of application it should send parameters to the already running instance and then exit, while the running instance of application should popup the main form.

Note: it is a typical MDI forms application written in Borland Delphi 5, built with Run-time packages=Vcl50;Vcl50x.

To achieve this I'm using some communication protocol based on a SendMessage(HWND_BROADCAST, ...). This part is working fine - it finds the running instance, check if can reuse it, sends parameters to running instance and then exit. So, this part is ok.
But the biggest problem I have at the moment - it cannot popup/activate the Window of running app instance.

I have tried different ways, like this:
1)
    Application.BringToFront;
    Self.BringToFront;

2)
    Application.BringToFront;
    Self.BringToFront; // Self is a class derived from TForm
    BringWindowToTop(Self.Handle);
    SetForegroundWindow(Self.Handle);
    SetActiveWindow(Self.Handle);

3)
    Application.BringToFront;
    ShowWindow(Self.Handle, SW_MINIMIZE);
    Sleep(50);
    ShowWindow(Self.Handle, SW_RESTORE);
    Sleep(50);
    Self.BringToFront;

But no success :-(((
Always - it is only blinking in the Widnows taskbar but not activate the main form of application! :-\

Please help - how I can activate application window? I mean - it should bring to front and make active (not just flashing in a Windows taskbar).

Test scenario is following:
1) run MyApp.exe, it started, show the main form, working.
2) switch to Explorer (as option - FAR/TotalCommaner), start the new instance of MyApp.exe
3) The newly started MyApp.exe should exit, the MyApp.exe started on step (1) should bring to front main form, so it is active and user can just work with it.

Problem - step (3) does not work - instead it only blinking in a taskbar. So, it activated correct application but unable to popup main form.

Regards,
Dmitry.

PS. I'm testing it on Windows 7 x64, also on Windows 2000/XP/2003/2008 - in all OSes it behaving the same way.
0
Comment
Question by:Dmitry_Bond
  • 2
3 Comments
 
LVL 13

Expert Comment

by:aflarin
ID: 33569411
try this function:

procedure ActivateWindow(Handle: THandle; RestoreIfMinimized: Boolean = True);
var
  CurThreadID, ThreadID: THandle;
begin
  if RestoreIfMinimized and IsIconic(Handle) then
    ShowWindow(Handle, SW_RESTORE);

  CurThreadID:= GetCurrentThreadID;
  ThreadID:= GetWindowThreadProcessId(GetForegroundWindow, nil);
  if CurThreadID = ThreadID then
    SetForegroundWindow(Handle)
  else begin

    if AttachThreadInput(GetCurrentThreadID, ThreadID, True) then begin
      ActivateWindowOld(Handle, RestoreIfMinimized);
      SetForegroundWindow(Handle);
      BringWindowToTop(Handle);
      AttachThreadInput(GetCurrentThreadID, ThreadID, False);
    end else
      ActivateWindowOld(Handle, RestoreIfMinimized);

  end;
end;
0
 
LVL 13

Accepted Solution

by:
aflarin earned 500 total points
ID: 33569431
you can delete ActivateWindowOld, it becomes obsolete :

procedure ActivateWindow(Handle: THandle; RestoreIfMinimized: Boolean = True);
var
  CurThreadID, ThreadID: THandle;
begin
  if RestoreIfMinimized and IsIconic(Handle) then
    ShowWindow(Handle, SW_RESTORE);

  CurThreadID:= GetCurrentThreadID;
  ThreadID:= GetWindowThreadProcessId(GetForegroundWindow, nil);
  if CurThreadID = ThreadID then
    SetForegroundWindow(Handle)
  else begin

    if AttachThreadInput(GetCurrentThreadID, ThreadID, True) then begin
      ActivateWindowOld(Handle, RestoreIfMinimized);
      SetForegroundWindow(Handle);
      BringWindowToTop(Handle);
      AttachThreadInput(GetCurrentThreadID, ThreadID, False);
    end;

  end;
end;
0
 

Author Closing Comment

by:Dmitry_Bond
ID: 33569831
Thank you very much! It works fine. :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now