Solved

How to popup/activate a main window of application?

Posted on 2010-08-31
3
1,670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Progress

691 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