Einzelnen Beitrag anzeigen

Fuchtel

Registriert seit: 9. Nov 2005
Ort: Bamberg
53 Beiträge
 
Delphi 2005 Personal
 
#7

Re: Fenster Form immer aktiv????

  Alt 10. Apr 2006, 12:36
Hallo,

also ich habe mir schon länger mal folgendes zusammengebastelt:


Delphi-Quellcode:
{*************************************************************************************************}
{*  ForceForegroundWindow
{*************************************************************************************************}

{*  Manchmal funktioniert die SetForeGroundWindow Funktion nicht so, wie sie sollte; besonders un-
{*  ter Windows 98/ME/2000/XP, schraenkt das Betriebssystem ein, welcher Prozess in den Fordergrund
{*  kommen darf, wenn ein anderes Fenster den Fokus hat.
{*  ForceForegroundWindow ist eine "verbesserte" Version von der SetForeGroundWindow API-Funktion,
{*  um ein Fenster in den Vordergrund zu bringen.
{*************************************************************************************************}

{*  Das ganze funzt nur unter Windows NT4/98/ME/2000/XP bei Start aus dem Explorer heraus.
{*  Sobald das Programm von einer BatchDatei aus erneut gestartet wird funzt es nur noch unter
{*  Win NT4!!
{*************************************************************************************************}

//# V2.84.8
function ForceForegroundWindow(hwnd: THandle): Boolean;
var
  ForegroundThreadID : DWORD;
  ThisThreadID : DWORD;
  timeout : DWORD;
begin
  if IsIconic(hwnd) then ShowWindow(hwnd, SW_RESTORE);
  if GetForegroundWindow = hwnd then
      Result := True // Hurra, es klappte sofort!
    else
      begin

        // Windows Version ermitteln
        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
              //-----------------------------------------------------------
              // Erster Versuch (-> Delphi Magazine 55, page 16)
              //-----------------------------------------------------------
              Result := False;
              ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow, nil);
              ThisThreadID := GetWindowThreadPRocessId(hwnd, nil);
              if AttachThreadInput(ThisThreadID, ForegroundThreadID, True) then
                begin
                  //-----------------------------------------------------------------------------
                  // Siehe auch in der MSDN Library "BringWindowToTop":
                  //
                  // The BringWindowToTop function brings the specified window to the top of the Z order.
                  // If the window is a top-level window, it is activated. If the window is a child window,
                  // the top-level parent window associated with the child window is activated.
                  // Use the BringWindowToTop function to uncover any window that is partially or completely
                  // obscured by other windows.
                  // Calling this function is similar to calling the SetWindowPos function to change a wind-
                  // ow's position in the Z order. BringWindowToTop does not make a window a top-level window.
                  //-----------------------------------------------------------------------------
                  BringWindowToTop(hwnd); // IE 5.5 related hack
                  SetForegroundWindow(hwnd);
                  AttachThreadInput(ThisThreadID, ForegroundThreadID, False);
                  Result := (GetForegroundWindow = hwnd); // Testen ob Erfolg
                end;

              //-----------------------------------------------------------------------------
              // Zweiter Versuch (Code von Daniel P. Stasinski)
              //-----------------------------------------------------------------------------
              if not Result then
                begin
                  //-----------------------------------------------------------------------------
                  // Siehe auch in der MSDN Library "SetForegroundWindow":
                  //
                  // To have SetForegroundWindow behave the same as it did on Windows 95 and MS
                  // Windows NT 4.0, change the foreground lock timeout value when the applica-
                  // tion is installed. This can be done from the setup or installation applica-
                  // tion with the following function call:
                  //
                  // SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0,
                  // SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
                  //
                  // This method allows SetForegroundWindow on Windows 98/Windows Me and Windows
                  // 2000/Windows XP to behave the same as Windows 95 and Windows NT 4.0, respec-
                  // tively, for all applications. The setup application should warn the user
                  // that this is being done so that the user isn't surprised by the changed be-
                  // havior. On Windows Windows 2000 and Windows XP, the call fails unless the
                  // calling thread can change the foreground window, so this must be called
                  // from a setup or patch application. For more information, see Foreground
                  // and Background Windows.
                  //-----------------------------------------------------------------------------
                  // Siehe auch in der MSDN Library "SystemParametersInfo":
                  //
                  // SPI_SETFOREGROUNDLOCKTIMEOUT:
                  // Sets the amount of time following user input, in milliseconds, during which
                  // the system does not allow applications to force themselves into the fore-
                  // ground. Set pvParam to the new timeout value.
                  // The calling thread must be able to change the foreground window, otherwise
                  // the call fails.
                  // Windows NT and Windows 95: This value is not supported.
                  // SPIF_SENDCHANGE:
                  // Broadcasts the WM_SETTINGCHANGE message after updating the user profile.
                  //-----------------------------------------------------------------------------
                  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
              // Win 95 / Win NT 4.0
              BringWindowToTop(hwnd); // IE 5.5 related hack
              SetForegroundWindow(hwnd);
            end;

         Result := (GetForegroundWindow = hwnd);
       end;
end; { ForceForegroundWindow }
So bleibt Dein Fenster immer an oberster Stelle und es kann den Focus behalten

Fuchtel
  Mit Zitat antworten Zitat