当焦点在Form1上并且浏览器窗口失去焦点时,Form1将消失

时间:2014-11-08 00:22:06

标签: delphi delphi-xe5

当我的Form1出现在浏览器窗口的主体上时出现问题,当我将焦点放在我的表单中并且浏览器窗口失去焦点时,他就会消失。怎么解决了?任何建议都会受到欢迎。

见下图:

enter image description here

这是我的完整代码:

用于枚举窗口的单元(EnumWindowUtil_.pas):

unit EnumWindowUtil_;

interface
uses
  Winapi.Windows,
  System.SysUtils,
  System.Classes;
type
  TWindowList = class(TStringList)
  private
    FAddClassname: Boolean;
  public
    procedure EnumChildWindows(handle: HWND);
    property AddClassname: Boolean read FAddClassname write FAddClassname;
  end;

  var
  wlistChilds: TWindowList;

implementation

  function GetWindowClassName(hwnd: HWND): string;
  begin
    SetLength(Result, 1024);
    GetClassName(hwnd, PChar(Result), Length(Result));
    Result := PChar(Result);
  end;

  procedure EnumWindowCallback(hwnd: HWND; lParam: TWindowList); stdcall;
  var
    buffer: array[0..255] of char;
    texto: string;
  begin
    if (not IsWindowVisible(hwnd)) then
      Exit;

    SendMessage(hwnd, $000D, 256, Integer(@buffer));
    texto := StrPas(buffer);
    texto := texto + ':' + GetWindowClassName(hwnd) + ' - ' + Format('%6.6x', [hwnd]) + '/' + IntToStr(hwnd);
    lParam.AddObject(texto, TObject(hwnd));
  end;

  procedure TWindowList.EnumChildWindows(handle: HWND);
  begin
    Clear;
    if Winapi.Windows.EnumChildWindows(handle, @EnumWindowCallback, Integer(Self)) then;
  end;
end.

这是主要单位:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, EnumWindowUtil_,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  linha: string;

implementation

{$R *.dfm}

function GetNavigatorHandle(BASE: HWND): HWND;
var
  I: integer;
begin
  linha:= '';
  Result := 0;
  wlistChilds := TWindowList.Create;
  wlistChilds.AddClassname := True;
  wlistChilds.EnumChildWindows(BASE);

  for I := 0 to wlistChilds.Count - 1 do
  begin
    linha := wlistChilds.Strings[I];

    if
       (Pos('Chrome_Render',linha)>0)then
    begin
      Result := StrToInt(copy(linha, pos('/', linha) + 1, Length(linha)));
      Break;
    end;
  end;
  FreeAndNil(wlistChilds);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
   janela, janelaContainer: HWND;
begin
   janela := GetForegroundWindow;

   janelaContainer := GetNavigatorHandle(Janela);
   if janelaContainer = 0 then
   begin
      Exit;
   end;
   Winapi.Windows.SetParent(form1.handle,janelaContainer);
end;

end.

0 个答案:

没有答案