terça-feira, 5 de junho de 2012

Delphi - Utilizando o FindWindow para localizar janela do IE


procedure Tform1.SetUrlFromIE(Handle: THandle); stdcall;
var
  hWndIE, hWndIEChild : HWND;
  Buffer : array[0..255] of Char;
  Url: String;
  tamanho: integer;
begin

  // Pega o Caption da Janela
  SendMessage(Handle, WM_GETTEXT, 255, lParam(@Buffer[0]));

  // Procura pela janela do Internet Explorer com o Buffer do Caption
  hWndIE := FindWindow('IEFrame', Buffer);

  If hWndIE > 0 Then
    Begin
      // Pega o Handle do container do Internet Explorer
      hWndIEChild:= FindWindowEx(hWndIE, 0, 'WorkerW', nil);

      If hWndIEChild > 0 Then
        Begin
          // Pega o Handle da Barra de Endereço do Internet Explorer
          hWndIEChild := FindWindowEx(hWndIEChild, 0, 'ReBarWindow32', nil);

          If hWndIEChild > 0 Then
            Begin
              // Pega o Handle do ComboBoxEx32 do Internet Explorer
              hWndIEChild := FindWindowEx(hWndIEChild, 0, 'Address Band Root', nil);

              If hWndIEChild > 0 Then
                Begin

                  If hWndIEChild > 0 Then
                    Begin
                      // Pega o Handle do Edit do Internet Explorer
                      hWndIEChild := FindWindowEx(hWndIEChild, 0, 'Edit', nil);

                      If hWndIEChild > 0 Then
                        Begin
                        tamanho := SendMessage(hWndIEChild, WM_GETTEXTLENGTH, 0, 0);
                        SetLength(Url, tamanho);
                        SendMessage(hWndIEChild, WM_GETTEXT, tamanho + 1, lParam(@Url[1]));
                        Edit1.Text := Url;
                        Webbrowser1.Navigate(Url);
                        end;
                    end;
                end;
            end;
        end;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Janela : THandle;
begin
Janela := FindWindow(nil, 'CONSISTeSCA - Menu - Windows Internet Explorer');
SetUrlFromIE(Janela);
end;

Nenhum comentário:

Postar um comentário