Solved

My program looses focus

Posted on 2002-06-03
7
465 Views
Last Modified: 2007-12-19
Hi Experts,

A question about one of my programs that's running on a Citrix server:

This program looses focus once it has started. Probably because Citrix starts some process just after my program has started. I can't find out what process, but a fact is that my program is not the active windows once it has started.
Because my program starts with a login window, the user must click on my program first before typing anything in the login window; rather annoying.

When I run my program locally or in the IDE there's no problem; once started my program is the active program. So, it's rather difficult to track and solve this problem.

What I like to know is: is there perhaps a way to force my program to become the active program. I could for instance progam a TTimer event, that runs some code 1 second after the program has been started. This code must force my program to become the active program.

Is there a way to achieve this?
Or is there perhaps another solution for this problem?

Thanks,
JDN
0
Comment
Question by:JDN
7 Comments
 
LVL 3

Expert Comment

by:tongalite
Comment Utility
Hi JDN

This is a long shot...
Try this code in your on create....

-----------------------------------
SendMessage(Application.Handle,WM_ACTIVATE,WA_CLICKACTIVE,0);

//Maybe switch the Application.Handle with the mainforms handle, usually Form1.Handle
-----------------------------------
Good luck
T.
0
 
LVL 1

Expert Comment

by:czechmate
Comment Utility
What about old      
 Application.BringToFront;

cj
0
 

Accepted Solution

by:
paulmoun earned 50 total points
Comment Utility
You used to be able to use the Windows API call 'SetForegroundWindow' but they changed it's behaviour in Windows98/2000 to stop you grabbing focus.
Here is a workaround(The fuction is fired by a timer..see bottom of code, also you need to change the name 'form1' to the name of your form):


function ForceForegroundWindow(hwnd: THandle): boolean;
const
  SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
  SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
  ForegroundThreadID: DWORD;
  ThisThreadID      : DWORD;
  timeout           : DWORD;
begin
  if IsIconic(hwnd) then ShowWindow(hwnd, SW_RESTORE);

  if GetForegroundWindow = hwnd then Result := true
  else begin

    // Windows 98/2000 doesn't want to foreground a window when some other
    // window has keyboard focus

    if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4))
or
      ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
      ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
(Win32MinorVersion > 0)))) then
    begin

      // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
      // Converted to Delphi by Ray Lischner
      // Published in The Delphi Magazine 55, page 16

      Result := false;
      ForegroundThreadID :=
GetWindowThreadProcessID(GetForegroundWindow,nil);
      ThisThreadID := GetWindowThreadPRocessId(hwnd,nil);
      if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
begin
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hwnd);
        AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
        Result := (GetForegroundWindow = hwnd);
      end;
      if not Result then begin

        // Code by Daniel P. Stasinski  

        SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
SPIF_SENDCHANGE);
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hWnd);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
TObject(timeout), SPIF_SENDCHANGE);
      end;
    end
    else begin
      BringWindowToTop(hwnd); // IE 5.5 related hack
      SetForegroundWindow(hwnd);
    end;

    Result := (GetForegroundWindow = hwnd);
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 if GetForegroundWindow<>Form1.handle then
   ForceForegroundWindow(Form1.handle);
end;

end.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:paulmoun
Comment Utility
Oh and I forgot, remove the minimize button from the form, otherwise the user will think windows has siezed up.
0
 
LVL 1

Author Comment

by:JDN
Comment Utility
Tongalite, Czechmate,

Both solutions you proposed don't work.

You can test it yourself:

Start a compiled Delphi application and, when this application is launched, click the Windows taskbar or desktop. Your application becomes inactive (the title bar of your application becomes gray).
Now this is in fact my problem. I like to execute some code to force my application (that is inactive now) become active. Once the user has logged on, this TTimer event can stop.
I've tried both your code (in a TTimer event) but my application stays inactive in Windows 2000.

Paulmon, I will try your solution tomorrow (it's sleeping time in the Netherlands now).

Thanks,
JDN
0
 
LVL 1

Author Comment

by:JDN
Comment Utility
This works.
Thanks.
0
 

Expert Comment

by:eant
Comment Utility
Hi, I have the same problem but I tried to use the code of Paulmon and it don't work..
How did you do?

thanks
luca
lcaruso@anacomp.it
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

15 Experts available now in Live!

Get 1:1 Help Now