Delphi知识中心
www.delphi.ee
提供Delphi技术知识
与Delphi程序员共同进步


在线服务QQ:99923144 随时恭候您的光临
首页 基础知识 WIN系统 组件使用 组件开发 数据库 ACTIVEX 多媒体技术 网络技术 关于
文章类别:网络技术    你尚未登陆,会员功能无法使用,请从 网站首页 登陆。
  TWebBrowser控件编程技巧  
 
给大家介绍一下应用TwebBrowser控件的一些编程技巧。 

1.显示一个GIF动画

如果你找不到显示GIF动画的控件,下面的一个函数可以解决你的燃眉之急

procedure ShowGIF( GIFFileName : String );

var

TargetFrameName,PostData,Heads,Flags : OleVariant;

URL : widestring;

begin

TargetFrameName := '';{指定Frame的空字符串时,

则在当前Frame中打开动画文件}

PostData := false;{不发送数据}

Heads := '';{Header信息为空}

Flags := 0;{Flags设为0}

URL := GIFFileName;

with formGIF.webbrowser do{在指定窗口中的

IE浏览器控件中显示动画}

begin

navigate(URL,Flags,TargetFrameName

,PostData,Heads);{显示动画文件}

end;

end;

2.删除IE的历史记录

很想知道如何实现删除IE的历史记录的吧,下面一段程序事例将介绍如何编写

uses wininet;

procedure TForm1.Button1Click(Sender: TObject);

var

cache_info:PInternetCacheEntryInfo;

cbSizeOf_Cache_info:dword;

hw:hwnd;

buf:tstrings;

begin

new(cache_info);

hw:=FindFirstUrlCacheEntryEx(nil,0,NORMAL_CACHE_ENTRY or URLHISTORY_CACHE_ENTRY,0,cache_info,@cbSizeOf_Cache_info,nil,nil,nil);

if hw<>0 then

begin

DeleteUrlCacheEntry(cache_info.lpszSourceUrlName);

while FindNextUrlCacheEntryEx(hw,cache_info,@cbSizeOf_Cache_info,nil,nil,nil) do

DeleteUrlCacheEntry(cache_info.lpszSourceUrlName);

end;

FindCloseUrlCache(hw);

dispose(cache_info);

end;

//方法二

type

TSTATURL = record

cbSize: DWORD;

pwcsUrl: DWORD;

pwcsTitle: DWORD;

ftLastVisited: FILETIME;

ftLastUpdated: FILETIME;

ftExpires: FILETIME;

dwFlags: DWORD;

end;

type

IEnumSTATURL = interface(IUnknown)

['{3C374A42-BAE4-11CF-BF7D-00AA006946EE}']

function Next(celt: Integer; out elt; pceltFetched: PLongint): HRESULT; stdcall;

function Skip(celt: Longint): HRESULT; stdcall;

function Reset: HResult; stdcall;

function Clone(out ppenum: IEnumSTATURL): HResult; stdcall;

function SetFilter(poszFilter: PWideChar; dwFlags: DWORD): HResult; stdcall;

end;

type

IUrlHistoryStg = interface(IUnknown)

['{3C374A41-BAE4-11CF-BF7D-00AA006946EE}']

function AddUrl(pocsUrl: PWideChar; pocsTitle: PWideChar; dwFlags: Integer): HResult; stdcall;

function DeleteUrl(pocsUrl: PWideChar; dwFlags: Integer): HResult; stdcall;

function QueryUrl(pocsUrl: PWideChar; dwFlags: Integer; var lpSTATURL: TSTATURL): HResult; stdcall;

function BindToObject(pocsUrl: PWideChar; var riid: TGUID; out ppvOut: Pointer): HResult; stdcall;

function EnumUrls(out ppenum: IEnumSTATURL): HResult; stdcall;

end;

type

IUrlHistoryStg2 = interface(IUrlHistoryStg)

['{AFA0DC11-C313-11D0-831A-00C04FD5AE38}']

function AddUrlAndNotify(pocsUrl: PWideChar; pocsTitle: PWideChar; dwFlags: Integer;

fWriteHistory: Integer; var poctNotify: Pointer;

const punkISFolder: IUnknown): HResult; stdcall;

function ClearHistory: HResult; stdcall;

end;

function ClearIEHistory:integer;

const

CLSID_CUrlHistory: TGUID = '{3C374A40-BAE4-11CF-BF7D-00AA006946EE}';

var

IEHistory:IUrlHistoryStg2;

begin

IEHistory:=CreateComObject(CLSID_CUrlHistory) as IUrlHistoryStg2;

IEHistory.ClearHistory;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

ClearIEHistory;

end;

3.添加到收藏夹和整理收藏夹

const

CLSID_ShellUIHelper: TGUID = '{64AB4BB7-111E-11D1-8F79-00C04FC2FBE1}';

 var

p:procedure(Handle: THandle; Path: PChar); stdcall;

procedure TForm1.OrganizeFavorite(Sender: Tobject);

    var

     H: HWnd;

    begin

     H := LoadLibrary(PChar('shdocvw.dll'));

     if H <> 0 then

     begin

    p := GetProcAddress(H, PChar('DoOrganizeFavDlg'));

      if Assigned(p) then p(Application.Handle, PChar(FavFolder));

     end;

     FreeLibrary(h);

    end;

    

    procedure TForm1.AddFavorite(Sender: TObject);

    var

     ShellUIHelper: ISHellUIHelper;

     url, title: Olevariant;

    begin

     Title := Webbrowser1.LocationName;

     Url := Webbrowser1.LocationUrl;

     if Url <> '' then

     begin

      ShellUIHelper := CreateComObject(CLSID_SHELLUIHELPER) as IShellUIHelper;

      ShellUIHelper.AddFavorite(url, title);

     end;

    end;

4.直接从TWebBrowser得到网页源码及Html

Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterHtml);

如果,用TWebBrowser浏览HTML文件的时候要将其保存为文本文件就很简单了,


 Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterText);

5.获取IE地址栏的handle

var

Form1: TForm1;

implementation

{$R *.DFM}

Function GetURL(H:hwnd;lparam:longint):boolean;stdcall;

var str,url:array [0..254] of char;

begin

getclassname(h,@str,255);

if strpas(@str)='ComboBoxEx32' then // ComboBoxEx32可以改成Edit.

begin

SendMessage(h,WM_GETTEXT,255,LongInt(@url));

SendMessage(h,WM_SETTEXT,255,longint(pchar('http://www.sina.com.cn')));

Sendmessage(h,WM_KEYDOWN,VK_Return,1);

form1.ListBox1.Items.Add(strpas(@url));

end;

result:=true;

end;

function callbackproc(H:HWnd;lparam:longint):Boolean;stdcall;

var str:array [1..255] of char;

begin

getclassname(h,@str,255);

if ((strpas(@str)='CabinetWClass') or (strpas(@str)='IEFrame')) then

begin

Enumchildwindows(h,@GetURL,0);

end;

result:=true;

end;

procedure TForm1.findbuttonClick(Sender: TObject);

begin

listbox1.Clear;

Enumwindows(@callbackproc,0);

end;

end.

 

在线服务QQ:99923144 Delphi程序员QQ群(139442)
Delphi知识中心 www.delphi.ee 最专业的Delphi技术资料网站