• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

Delphi/Windows 2000 inactive window problem.

HI,

I currently have a program that has been built with Delphi 5, and when I run this program on Windows Me or 2000 it has a problem with inactive windows. Simply starting up the program will cause the vary first window to go inactive. This has not been a problem with 95, 98, or NT. It would be great if someone could tell me what causes it and how to fix it
0
akajmf
Asked:
akajmf
  • 2
  • 2
  • 2
  • +3
1 Solution
 
EpsylonCommented:
I'm not sure what you are after but my guess is that you need to call this function in the OnCreate event of your main 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; { ForceForegroundWindow }


procedure TForm1.FormCreate(Sender: TObject);
begin
  ForceForegroundWindow(Handle);
end;
0
 
MadshiCommented:
Normally the first window should be active. Do you have a default project (*.dpr) file or did you make some changes to it? Or do you show anything while the program shows up (= before and while the first window is shown)? There must be something, because my projects all work fine in all OSs...

Regards, Madshi.
0
 
akajmfAuthor Commented:
When the program is started a splash screen comes up as it connects to a server, when this is done a log in screen appears and that is what goes inactive. Various other forms go inactive throughout the whole program also, but it happens on the firs form the most.
0
Industry Leaders: 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!

 
EpsylonCommented:
Did you try

FormX.BringToFront;
0
 
FrodoBegginsCommented:
You may try some of these
  SetForegroundWindow(Handle);
  BringWindowToTop(WindowHandle);
  SendMessage(Handle,WM_SETFOCUS,0,0);
But somewhere in the MSDN I red (about a year ago) that there is a new policy in windows 2000 about bringing windows to front.  E.g. if your program has no focus, it can't retreive it because the user may enter a password in that very moment expecting other app to catch it... I'd better search for that article again. It was about "What's new in Windows 2000"
0
 
MadshiCommented:
It's right that you are not allowed to steal focus from the foreground window/application in ME&2000 in some situations, but this sounds like another situation. I go with Epsylon, I guess simply using InactiveForm.BringToFront after the splash screen should fix the problem.

Regards, Madshi.
0
 
akajmfAuthor Commented:
Thanks you all for your help. I am testing it and will soon let you know if any of your suggestions work.

0
 
egonoCommented:
if epsylons ForceForegroundWindow() doesn't work (it doesn't on my system - W2K SP1) take a look at this thread:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20089680

the provided answer works for me. if you don't want to pay the 10 points I can repost the answer.

good luck
0
 
Russell LibbySoftware Engineer, Advisory Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Epsylon's comment as answer

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now