Solved

Delphi/Windows 2000 inactive window problem.

Posted on 2001-06-21
10
302 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
[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
  • 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
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!

 
LVL 13

Expert Comment

by:Epsylon
ID: 6216299
Did you try

FormX.BringToFront;
0
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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