troubleshooting Question

Focus problems when using multiple forms and message boxes in Delphi

Avatar of Deliciosa
Deliciosa asked on
Delphi
6 Comments1 Solution1109 ViewsLast Modified:
I'm having focus problems with my application in Delphi.  It's complicated and uses Word_TLB, which is where the problem form is launched from.  It's mainly a problem on PC's running Office 2003 for some reason and barely affects PC's running Office 2000.

I try to set focus on modal forms, as Delphi doesn't always set focus to them correctly for some reason.  Sometimes it just keeps focus on the main application, which won't respond as it's waiting for the modal form to close.  The only way to get focus onto the modal window is to alt+tab round a few times until the child window gains focus.

I have tried various solutions including one which tries various methods depending on which file is in the root, it tries this every 1.5 seconds:

procedure TSenWPFileNameFrm.Timer1Timer(Sender: TObject);
begin
     If ((Application.Active)or(m_shown<2)) then
      begin
       Inc(m_shown);
       If FileExists('c:\focus.a') then
        begin
         SetForegroundWindow(handle);
         SetActiveWindow(handle);
         Invalidate;
        end
       Else
        begin
         If FileExists('c:\focus.b') then
          begin
           Application.BringToFront;
          end
         Else
          begin
           If FileExists('c:\focus.c') then
            Self.SetFocusOnForm
           Else
            if GetForegroundWindow<>Self.handle then
             ForceForegroundWindow(Self.handle);
          end;
        end;
      end;
end;



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 }


Any help would be greatly appreciated. Thanks.
ASKER CERTIFIED SOLUTION
modulo

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros