Solved

How to popup/activate a main window of application?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

13 Experts available now in Live!

Get 1:1 Help Now