Thema: Delphi Auswahlliste TWebbrowser

Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#4

Re: Auswahlliste TWebbrowser

  Alt 26. Dez 2008, 10:44
Update: Das OnChange Ereignis des DropDowns wird nicht ausgelöst, wenn ein Item per Code ausgewählt wird.

Delphi-Quellcode:
uses
  mshtml_tlb, ActiveX;

function TriggerEvent(WB: TWebbrowser; Element: OleVariant; EventName: WideString): Boolean;
var
  Doc4: IHTMLDocument4;
  Doc2: IHTMLDocument2;
  EventObj: IHTMLEventObj;
  varEventObj, pEventObject: OleVariant;
begin
  Result := False;
  if (Succeeded(WB.Document.QueryInterface(IID_IHTMLDocument4, Doc4))) then
  begin
    // TVariantArg(ret).vt := VT_UINT;
    // TVariantArg(ret).lVal := 0;
    varEventObj := Unassigned;
    EventObj := Doc4.CreateEventObject(varEventObj);
    if (Succeeded(WB.Document.QueryInterface(IID_IHTMLDocument2, Doc2))) then
    begin
      pEventObject := EventObj;
      try
        Result := Element.fireEvent(EventName, pEventObject);
      except
        Result := False;
      end;
    end;
  end;
end;

function WB_select_SelectItem(WB: TWebbrowser; SelectName, ItemName: string; TriggerOnChangeEvent: Boolean): boolean;
var
  iForms, iFormItems, iSelectItems: Word;
  FormItem: OleVariant;
begin
  Result := False;
  for iForms := 0 to WB.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WB.OleObject.Document.forms.Item(iForms);
    for iFormItems := 0 to FormItem.Length - 1 do
    begin
      if (FormItem.Item(iFormItems).type = 'select-one') and
        (FormItem.Item(iFormItems).Name = SelectName) then
      begin
        for iSelectItems := 0 to FormItem.Item(iFormItems).Options.Length - 1 do
        begin
          if SameText(FormItem.Item(iFormItems).Options.Item(iSelectItems).Text, ItemName) then
          begin
            FormItem.Item(iFormItems).SelectedIndex := iSelectItems;
            // The onchange event does not fire when the selected option of
            // the select object is changed programatically
            // http://msdn.microsoft.com/en-us/library/ms536912(VS.85).aspx
            if TriggerOnChangeEvent then
              // Trigger OnChange event:
              TriggerEvent(WB, FormItem.Item(iFormItems), 'onchange');
            Result := True;
            Break;
          end;
        end;
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WB_select_SelectItem(Webbrowser1,'url','Germany', True);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Webbrowser1.Navigate('http://www.mathworks.com/company/worldwide/');
end;
Thomas
  Mit Zitat antworten Zitat