Solved

How to popup/activate a main window of application?

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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.

746 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

14 Experts available now in Live!

Get 1:1 Help Now