?
Solved

Delphi/Windows 2000 inactive window problem.

Posted on 2001-06-21
10
Medium Priority
?
304 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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