没整理,将就看吧!!
unit web;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, ImgList, ActnList, StdCtrls, OleCtrls,MsHtml,
SHDocVw, ExtCtrls, Registry, ActiveX, Menus, ComObj,Types, jpeg, strUtils;
type
TBrowserFrm = class(TForm)
MainMenu1: TMainMenu;
ActionList1: TActionList;
ImageList1: TImageList;
IlHot: TImageList;
IlUnFocus: TImageList;
OpenDialog1: TOpenDialog;
ActOpen: TAction;
ActClose: TAction;
ActCopy: TAction;
ActGoto: TAction;
ActRefresh: TAction;
ActStop: TAction;
ActFont: TAction;
ActAddTo: TAction;
ActLintToNetEase: TAction;
ActLintToSina: TAction;
ActLintToSohu: TAction;
ActLinkHust: TAction;
ActFoward: TAction;
ActBackward: TAction;
ActFavoritClk: TAction;
ActManageFavorit: TAction;
ActSelAll: TAction;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
ActFont1: TMenuItem;
Largest: TMenuItem;
Larger: TMenuItem;
Middle: TMenuItem;
Small: TMenuItem;
Smallest: TMenuItem;
MenuFavorit: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
N15: TMenuItem;
N16: TMenuItem;
N17: TMenuItem;
N18: TMenuItem;
CoolBar1: TCoolBar;
TbMenu: TToolBar;
TbButton: TToolBar;
ToolButton2: TToolButton;
ToolButton1: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
TbLink: TToolBar;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
ComboAddr: TComboBoxEx;
TbGoto: TToolBar;
ToolButton10: TToolButton;
PageControl: TPageControl;
TabSheet1: TTabSheet;
WebBrowser: TWebBrowser;
actNewWin: TAction;
PopupMenu: TPopupMenu;
N12: TMenuItem;
N19: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ActFavoritClkExecute(Sender: TObject);
procedure ComboAddrKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComboAddrSelect(Sender: TObject);
procedure ActOpenExecute(Sender: TObject);
procedure ActBackwardExecute(Sender: TObject);
procedure ActFowardExecute(Sender: TObject);
procedure ActRefreshExecute(Sender: TObject);
procedure ActStopExecute(Sender: TObject);
procedure ActGotoExecute(Sender: TObject);
procedure ActLintToNetEaseExecute(Sender: TObject);
procedure ActLintToSinaExecute(Sender: TObject);
procedure ActAddToExecute(Sender: TObject);
procedure ActManageFavoritExecute(Sender: TObject);
procedure ActCloseExecute(Sender: TObject);
procedure WebBrowserNewWindow2(Sender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
procedure WebBrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure N12Click(Sender: TObject);
procedure PageControlChange(Sender: TObject);
// procedure procedure NewWin;
private
FavoritFolder: string; //用来记录用户收藏夹的路径
FolderCount: Integer; //用来记录收藏夹的文件夹个数,对其分类添加
UrlList: TStringList; //记录收藏夹中各项的Url地址
{======= 生成多页面 =======}
TS: TTabSheet;
WB: TWebBrowser;
WBDocumentComplete: TWebBrowserDocumentComplete; //为生成页面标题的过程
WBNewWindow2 : TWebBrowserNewWindow2;
{ Private declarations }
public
procedure ReadFavorit(ARootItem: TMenuItem; aPath: string);//从系统文件夹中读入收藏夹内容,并动态生成菜单
procedure ReadTypedUrl; //从注册表中读入历史地址
procedure HistoryAddItem(aUrl: string); //将历史地址添加到ComboAddr中
{ Public declarations }
end;
const
CLSId_ShellUiHelper: TGuId = '{64AB4BB7-111E-11D1-8F79-00C04FC2FBE1}';
//将在添加及管理收藏夹时用到
var
BrowserFrm: TBrowserFrm;
implementation
{$R *.dfm}
procedure TBrowserFrm.FormCreate(Sender: TObject);
var
MyReg: TRegistry; //封装了注册表操作的类
Url: OleVariant;
begin
inherited; //调用基类中的OnCreate事件响应过程
MyReg := TRegistry.Create;
try
MyReg.RootKey := HKey_Current_User; //设置myreg的根键
if MyReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', false) then
//打开子键
begin
FavoritFolder := MyReg.ReadString('Favorites'); //读入收藏夹的目录路径
MyReg.CloseKey;
end;
finally
MyReg.Free;
ReadTypedUrl; //调用过程,读入历史页面地址
FolderCount := 3; //设置初始值为3,使得动态菜单从第3项开始添加
UrlList := TStringList.Create;
ReadFavorit(MenuFavorit, FavoritFolder); //读入收藏夹的内容,生成菜单
Url := 'about:blank';
ComboAddr.Text := 'about:blank';
WebBrowser.Navigate2(Url); //初始化WebBrowser为打开空白页面
end;
{===========为完成多页浏览的初始化=========}
WBDocumentComplete := WebBrowser.OnDocumentComplete;
WBNewWindow2 := WebBrowser.OnNewWindow2;
end;
{ TODO : 在form.destroy中注销在Creat中创建的对象 }
procedure TBrowserFrm.FormDestroy(Sender: TObject);
begin
UrlList.Free;
end;
{ TODO : 从注册表和本地文件系统中读入历史Url和收藏夹内容 }
procedure TBrowserFrm.ReadTypedUrl;
var
MyReg: TRegistry;
Val: TStringList;
I: Integer;
begin
MyReg := TRegistry.Create;
Val := TStringList.Create;
try
MyReg.RootKey := HKey_Current_User;
if MyReg.OpenKey('/Software/Microsoft/InternetExplorer/TypedUrls', false) then
begin
MyReg.GetValueNames(Val); //得到所有子键的枚举名字
ComboAddr.ItemsEx.Clear;
for I := 0 to Val.Count - 1 do
HistoryAddItem(MyReg.ReadString(Val.Strings)); //添加历史地址到ComboAddr
MyReg.CloseKey;
end;
finally
MyReg.Free;
Val.Free;
end;
end;
{ TODO : 将Url地址加进ComboAddr中根据不同的协议设置图标。 }
procedure TBrowserFrm.HistoryAddItem(aUrl: string);
var
Item: TComboExItem;
begin
Item := ComboAddr.ItemsEx.Add;
Item.Caption := aUrl;
if pos('http:', aUrl) > 0 then //HTTP协议的Url
Item.ImageIndex := 1
else if (pos('ftp:', aUrl) > 0) or (pos('ftp.', aUrl) > 0) then //ftp协议的Url
Item.ImageIndex := 0
else
Item.ImageIndex := 1; //其他
end;
{ TODO : Internet快捷方式 的文件格式:[InternetShortcut]<br>Url=。与INI相同,故用WindowsAPI函数
GetProvateProfileString来读取.Url文件的内容}
procedure TBrowserFrm.ReadFavorit(aRootItem: TMenuItem; aPath: string);
var
NewItem: TMenuItem;
sr: TSearchRec;
FileAttrs: Integer;
aUrl: string;
Buffer: array[0..2047] of char;
begin
// FileAttrs := faArchive + faDirectory; //查找目录下的所有文件和目录
FileAttrs := 32 + 16;
if FindFirst(aPath + '/*', FileAttrs, sr) = 0 then //API函数FindFirst用于查找文件,结果在SR中
begin
repeat
//动态生成收藏夹部分菜单,FindFirst函数将父目录'..'当前目录'.'也返回,需去掉,对于目录则递归调用该方法
if ((sr.Attr and faDirectory) = sr.Attr) and (sr.Name <> '.') and (sr.Name <> '..') then
begin
NewItem := TMenuItem.Create(nil);
NewItem.Caption := sr.Name;
NewItem.Visible := true;
NewItem.ImageIndex := 2;
//用Insert方法而不是Add,可以将所有目录排列在一起
aRootItem.Insert(FolderCount, NewItem);
FolderCount := FolderCount + 1;
ReadFavorit(NewItem, aPath + '/' +sr.Name); //递归调用
end
else if (sr.Attr and 32) = sr.Attr then //处理快捷方式
begin
NewItem := TMenuItem.Create(nil);
//用API函数GetPrivateProfileString读取ini文件格式的内容
GetPrivateProfileString('InternetSortcut', Pchar('URL'),nil, Buffer, sizeOf(Buffer), Pchar(apath + '/' +sr.Name));
aUrl := String(Buffer);
Delete(sr.Name, pos('.url',sr.Name), 4); //删除文件后缀
NewItem.ImageIndex := 1;
NewItem.Visible := true;
NewItem.Tag :=UrlList.Count; //用tag属性记下在列表中的索引
UrlList.Add(aUrl); //将Url地址存入UrlList列表
NewItem.Hint := aUrl;
NewItem.OnClick := actFavoritClkExecute; //设置该菜单的响应过程
NewItem.Caption := sr.Name;
aRootItem.Add(NewItem);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
{ TODO : 在ComboAddr中输入url后按回车进行浏览 }
procedure TBrowserFrm.ComboAddrKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
aText: string;
begin
if key = VK_RETURN then //判断用户是否按下了回车键
begin
//将'空白'的TabSheet去掉
if Assigned(TabSheet1) then
TabSheet1.Destroy;
{========多页面部分=======}
TS := TTabSheet.Create(self);
TS.PageControl := PageControl;
WB := TWebBrowser.Create(self);
TS.InsertControl(WB);
WB.Align := alClient;
PageControl.ActivePage := TS;
aText := trim(ComboAddr.Text);
if ComboAddr.Items.IndexOf(aText) < 0 then //判断历史地址中有没有该项
HistoryAddItem(aText); //没有则将当前地址加入历史列表中
WB.Navigate(ComboAddr.Text);
WB.OnDocumentComplete := WBDocumentComplete; //得到下载文档完成后的执行过程
WB.OnNewWindow2 := WBNewWindow2;
{=========================}
end;
end;
{ TODO : 方便用户,在用户选择地址列表中的内容时,也应打开相应的url }
procedure TBrowserFrm.ComboAddrSelect(Sender: TObject);
begin
WebBrowser.Navigate(ComboAddr.Text);
end;
{================================================================}
//以下将集中处理action中的事件
{================================================================}
{ TODO : 允许用户打开本地html文件 }
procedure TBrowserFrm.ActOpenExecute(Sender: TObject);
begin
OpenDialog1.Filter := 'Html 文件|*.html;*.htm|所有文件|*.*';
if OpenDialog1.Execute then
begin
ComboAddr.Text := OpenDialog1.FileName;
WebBrowser.Navigate(ComboAddr.Text);
end;
end;
{ TODO : 以下代码实现了浏览器的前进,后退功能 }
procedure TBrowserFrm.ActBackwardExecute(Sender: TObject);
begin
try
WB.GoBack; //返回到上一页
finally
end;
end;
procedure TBrowserFrm.ActFowardExecute(Sender: TObject);
begin
try
WB.GoForward; //前进到下一页
finally
end;
end;
{ TODO : 刷新功能 }
procedure TBrowserFrm.ActRefreshExecute(Sender: TObject);
begin
try
WebBrowser.Refresh2;
finally
end;
end;
{ TODO : 停止功能 }
procedure TBrowserFrm.ActStopExecute(Sender: TObject);
begin
try
WebBrowser.Stop;
finally
end;
end;
{ TODO : 转到功能 }
procedure TBrowserFrm.ActGotoExecute(Sender: TObject);
var
aText: string;
begin
aText := trim(ComboAddr.Text);
if ComboAddr.Items.IndexOf(aText) < 0 then
HistoryAddItem(aText);
WebBrowser.Navigate(aText);
end;
{ TODO : 增加快捷网站按钮 }
procedure TBrowserFrm.ActLintToNetEaseExecute(Sender: TObject);
begin
//连到网易
ComboAddr.Text := 'http://www.163.com';
WebBrowser.Navigate('http://www.163.com');
end;
procedure TBrowserFrm.ActLintToSinaExecute(Sender: TObject);
begin
//新浪
ComboAddr.Text := 'http://www.sina.com.cn';
WebBrowser.Navigate('http://www.sina.com.cn');
end;
{ TODO : 允许用户新喜欢的网站添加到收藏夹中
该过程的实现用到了IShellUIHelper接口}
procedure TBrowserFrm.ActAddToExecute(Sender: TObject);
var
ShellUIHelper: IShellUIHelper;
Url, Title: OleVariant;
begin
{====== 多页面 =======}
Title := WB.LocationName;
Url := WB.LocationURL;
{ Title := WebBrowser.LocationName;
Url := WebBrowser.LocationURL;
}
if Url <> '' then
begin
ShellUIHelper := CreateComObject(CLSId_ShellUIHelper) as IShellUIHelper;
ShellUIHelper.AddFavorite(Url, Title);
end;
end;
{ TODO : 调用shdocvw.dll中的doOrganizeFavDlg过程,直接弹出系统的'管理收藏夹'对话框。 }
procedure TBrowserFrm.ActManageFavoritExecute(Sender: TObject);
var
proc: procedure(Handle:THandle; Path: Pchar); stdcall; //定义过程变量
WHwnd: HWnd; //定义窗口句柄变量
begin
//装载动态链接库
WHwnd := LoadLibrary(pchar('shdocvw.dll'));
if Whwnd <> 0 then
begin
//得到DoOrganazeFavDlg过程的执行地址
proc := GetProcAddress(WHwnd, Pchar('DoOrganizeFavDlg'));
if Assigned(proc) then
proc(Application.Handle, Pchar(FavoritFolder)); //执行过程
end;
FreeLibrary(WHwnd); //卸载动态链接库
end;
{ TODO : 单击收藏夹中的项目进行浏览 }
procedure TBrowserFrm.ActFavoritClkExecute(Sender: TObject);
begin
ComboAddr.Text := UrlList.Strings[TMenuItem(sender).tag];
WebBrowser.Navigate(ComboAddr.Text);
end;
{ TODO : 退出应用程序 }
procedure TBrowserFrm.ActCloseExecute(Sender: TObject);
begin
Self.Close;
end;
{==============处理WebBrowser的过程,并赋给WB以实现功能=======================}
{ TODO : 在浏览过程中打开新页面时,创建新的TabSheet&WebBrowser. }
procedure TBrowserFrm.WebBrowserNewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
{========多页面部分=======}
TS := TTabSheet.Create(self);
TS.PageControl := PageControl;
WB := TWebBrowser.Create(self);
WB.Align := alClient;
TS.InsertControl(WB);
Ts.Caption := WB.GetNamePath;
PageControl.ActivePage := TS;
ppDisp := WB.Application;
end;
{ TODO : 在浏览页面下载完成后显示标题 }
procedure TBrowserFrm.WebBrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if Assigned(WB) and assigned(WB.Document) then
begin
TS.Caption := IHtmlDocument2(WB.Document).title;
TS.ShowHint := true;
TS.Hint := IHtmlDocument2(WB.Document).url;
if TS.Caption = '' then
begin
TS.Caption := '空白页面';
TS.Hint := 'About: Black';
end;
end;
end;
{ TODO : TabSheet 右键菜单的执行 }
//关闭当前浏览页面
procedure TBrowserFrm.N12Click(Sender: TObject);
begin
if PageControl.PageCount > 1 then
PageControl.ActivePage.Destroy;
end;
procedure TBrowserFrm.PageControlChange(Sender: TObject);
begin
ComboAddr.Text := PageControl.ActivePage.Hint;
end;
initialization
{ TODO : 引进了TRegistry类,WebBrowser是OLE对象,需要对它初始化 }
OleInitialize(nil);
finalization
try
OleUninitialize;
except
end;
end.