Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Fenster im Vordergrund (https://www.delphipraxis.net/153028-fenster-im-vordergrund.html)

API 18. Jul 2010 13:05

Fenster im Vordergrund
 
Moin,

Verschiedene Fenster sollen abwechslungsweise im Vordergrund erscheinen.

Ich habe schon alles mögliche versucht - die Fenster erscheinen nicht immer im Vordergrund.

Testweise wurden 2 Notepads geöffnet, welche abwechslungsweise im Vordergrund erscheinen sollen.

Diese Funktionen habe ich schon ausprobiert (auch miteinander kombiniert)

Delphi-Quellcode:
    SetWindowPos(
      hwin,
      HWND_TOP, // HWND_TOPMOST möchte ich jedoch nicht
      0, // horizontal position
      0, // vertical position
      0, // width
      0, // height
      SWP_NOMOVE or SWP_NOSIZE or SWP_SHOWWINDOW);

Delphi-Quellcode:
SwitchToThisWindow
Delphi-Quellcode:
SetForeGroundWindow
Delphi-Quellcode:
ForceForeGroundWindow
Delphi-Quellcode:
BringWindowToTop
Bin ratlos und fast am Verzweifeln...

PS: Die Fenster Handles stimmen!

API

DeddyH 18. Jul 2010 17:13

AW: Fenster im Vordergrund
 
Versuch es doch einmal mit ForceForegroundWindow.

Berni68 18. Jul 2010 18:31

AW: Fenster im Vordergrund
 
Mit den Fenstern ist es so eine Sache, da war ich auch am verzweifeln.
Mit dieser Funktion aus der Delphi Praxis hab ich so manches Problem gelöst:

Delphi-Quellcode:
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, [url]www.mvps.org/vb/sample.htm[/url]
      // 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;

toms 18. Jul 2010 19:30

AW: Fenster im Vordergrund
 
Liste der Anhänge anzeigen (Anzahl: 1)
Geht bei mir auch nicht... Siehe Beispielprojekt.

Relevanter Test-Code:

Delphi-Quellcode:
function Notepad1: HWND;
begin
  Result := FindWindow('Notepad', 'test1.txt - Notepad');
end;

function Notepad2: HWND;
begin
  Result := FindWindow('Notepad', 'test2.txt - Notepad');
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  hNotepad: HWND;
begin
  Inc(i);

  if Odd(i) then
  begin
    hNotepad := Notepad1;
    Caption := 'test1.txt - Notepad sollte im Vordergrund sein.'
  end
  else
  begin
    hNotepad := Notepad2;
    Caption := 'test2.txt - Notepad sollte im Vordergrund sein.'
  end;

  if hNotepad <> 0 then
  begin
    ForceForegroundWindow(hNotepad);
  end;
end;

Nachtrag: So werden die Notepads in den Vordergrund geholt:

Delphi-Quellcode:
  if hNotepad <> 0 then
  begin
    ShowWindow(hNotepad,SW_RESTORE);
    ForceForegroundWindow(hNotepad);
  end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:47 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz