Solved

Delphi/Windows 2000 inactive window problem.

Posted on 2001-06-21
10
294 Views
Last Modified: 2010-04-06
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
Comment
Question by:akajmf
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6215943
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6216083
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
 

Author Comment

by:akajmf
ID: 6216260
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 6216299
Did you try

FormX.BringToFront;
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6217201
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6217540
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
 

Author Comment

by:akajmf
ID: 6219153
Thanks you all for your help. I am testing it and will soon let you know if any of your suggestions work.

0
 
LVL 2

Expert Comment

by:egono
ID: 6219270
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8685158
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

17 Experts available now in Live!

Get 1:1 Help Now