Solved

How to popup/activate a main window of application?

Posted on 2010-08-31
3
1,628 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 27
Output in PHP throwing alignment of data off issue 12 44
Fatal Error 11 40
Is there any way to treat Lock record in table with clientdataset? 3 16
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

830 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