2008. július 23., szerda

Get the screen coordinates of the rectangle in which web pages are rendered in IE


Problem/Question/Abstract:

I am writing a Delphi application that needs to know the screen coordinates (top, left) of where the IE 'browser section' starts. What I mean by 'browser section' is the rectangle where web pages are rendered - not where the IE window is. I can find out where the IE window is, and where the client coordinates start, but not the where the 'browser section' starts. I want to lay my Delphi window precisely on top of the where the browser section is. But depending on how many toolbars the users is displaying where this browser section may start is a mystery.

Answer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    Label1: TLabel;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure FindIEBrowserWindowHandle;
  end;

var
  Form1: TForm1;
  IEBrowserWindowHandle: THandle;

implementation

{$R *.DFM}

function EnumIEChildProc(AHandle: hWnd; AnObject: TObject): BOOL; stdcall;
var
  tmpS: string;
  theClassName: string;
  theWinText: string;
begin
  Result := True;
  SetLength(theClassName, 256);
  GetClassName(AHandle, PChar(theClassName), 255);
  SetLength(theWinText, 256);
  GetWindowText(AHandle, PChar(theWinText), 255);
  tmpS := StrPas(PChar(theClassName));
  if theWinText <> EmptyStr then
    tmpS := tmpS + ' "' + StrPas(PChar(theWinText)) + '"'
  else
    tmpS := tmpS + '""';
  if Pos('Explorer_Server', tmpS) > 0 then
  begin
    IEBrowserWindowHandle := AHandle;
  end;
end;

function IEWindowEnumProc(AHandle: hWnd; AnObject: TObject): BOOL; stdcall;
{callback for EnumWindows}
var
  theClassName: string;
  theWinText: string;
  tmpS: string;
begin
  Result := True;
  SetLength(theClassName, 256);
  GetClassName(AHandle, PChar(theClassName), 255);
  SetLength(theWinText, 256);
  GetWindowText(AHandle, PChar(theWinText), 255);
  tmpS := StrPas(PChar(theClassName));
  if theWinText <> EmptyStr then
    tmpS := tmpS + ' "' + StrPas(PChar(theWinText)) + '"'
  else
    tmpS := tmpS + '""';
  if Pos('IEFrame', tmpS) > 0 then
  begin
    EnumChildWindows(AHandle, @EnumIEChildProc, longInt(0));
  end;
end;

procedure TForm1.FindIEBrowserWindowHandle;
begin
  Screen.Cursor := crHourGlass;
  try
    EnumWindows(@IEWindowEnumProc, LongInt(0));
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  caption: array[0..127] of Char;
  s: string;
  R: TRect;
begin
  FindIEBrowserWindowHandle;
  if IEBrowserWindowHandle > 0 then
  begin
    GetWindowRect(IEBrowserWindowHandle, R);
    s := 'IE Browser Window located at: ' + IntToStr(R.Top) + ', ' + IntToStr(R.Left)
      + ', ' + IntToStr(R.Bottom) + ', ' + IntTOStr(R.Right);
    Label1.Caption := s;
  end
  else
    label1.Caption := 'Not Found';
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése