我在IE窗口中加了一个按钮,我点击时,想得到IE当前的网页,怎么做?(226分)

  • 主题发起人 netwinds
  • 开始时间
N

netwinds

Unregistered / Unconfirmed
GUEST, unregistred user!
我在IE窗口中加了一个按钮,我点击时,想得到IE当前的网页,怎么做?
如果IE当前是空的,又要怎么处理呢?

如果是在 Explorer窗口上加一下按钮,我点击时如何知道当前所选择的文件呢?
如果我选择了多个文件(可能文件+文件,也可能文件+目录),我又如何得知呢?

就这些分了,全给了,高手快出来!
 
是Toolbar Button 还是 Context Menu?
 
判断浏览器当前浏览的地址
function URLInfo(sBrowerPrgFile, sServiceName: string; Netscape: boolean; var Title: string): pChar;
{
参数说明:
sBrowerPrgFile: 浏览器exe文件的完整路径名
sServiceName: 浏览器的DDE-Service名字
Netscape是'Netscape',IE是'iexplore'
Title: 返回当前网页的title
返回值: pChar的字符串
}
var
DDEClientConv: TDDEClientConv;
StartPtr, EndPtr: pchar;
browserWinName: string;
begin
result:= #0;
Title:= '';

if (sBrowerPrgFile = '') or (not FileExists(sBrowerPrgFile)) then
raise EBrowserNotFoundError.create('****!浏览器应用程序不存在!');

ddeClientConv:= TDDEClientConv.Create( nil );
try
with ddeClientConv do
begin
ServiceApplication := sBrowerPrgFile;
SetLink( sServiceName,'WWW_GetWindowInfo');
StartPtr:= RequestData('0xFFFFFFFF');
end;
finally
ddeClientConv.Free;
end;

if startPtr^ = #0 then
exit;
{skip leading "}
inc(StartPtr);
EndPtr:= StartPtr;
{proceed to next "}
while (EndPtr^ <> '"') do
inc(EndPtr);
{terminate URL string}
EndPtr^:= #0;
result:= StartPtr;
{skip ","}
StartPtr:= EndPtr+3;
if Netscape then
inc(StartPtr,12);

EndPtr:= StartPtr;
if Netscape then
while (EndPtr^ <> ']') do
inc(EndPtr)
else
while (EndPtr^ <> '"') do
inc(EndPtr);

EndPtr^:= #0;
Title:= strPas(StartPtr);
end;
 
是Context Menu
 
想得到网页的什么,Document、url还是html源码?
 
Document

比如我想把当前文档保存,或打印等操作。

在Explorer(我的电脑样式的文件夹)中,就是取得所选择的文件,(多个或文件夹与文件)
 
在脚本中可以得到:

<script language="VBScript">

Sub OnContextMenu()

On Error Resume Next

set objSaveMgr=CreateObject("QuickSave.SaveMgr")

Call objSaveMgr.SaveHtm(external.menuArguments.document.body.innerHtml,external.menuArguments.document.Url,external.menuArguments.document.title)

end Sub

call OnContextMenu()

</script>

----------------------------------------
另一个办法是这样:

uses MSHTML;
{$R *.dfm}


type
TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;

//This function detects the Window
function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
hInst: HWND;
lRes: Cardinal;
MSG: Integer;
pDoc: IHTMLDocument2;
ObjectFromLresult: TObjectFromLresult;
begin
hInst := LoadLibrary('Oleacc.dll');
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult <> nil then begin
try
MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
SendMessageTimeOut(WHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
if Result = S_OK then
(pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
finally
FreeLibrary(hInst);
end;
end;
end;

procedure TForm1.GetSource;
var
//All: IHtmlElementCollection;
//HtmlElement: IHtmlElement;
//I: Integer;
Document: IHtmlDocument2;
IE: IWebBrowser2;
Wnd: HWND;
WndChild: HWND;

begin
Wnd := FindWindow('IEFrame', nil);
if Wnd = 0 then
begin
MessageDlg ('No Running instance of Internet Explorer!',mtError, [mbOK], 0);
end;
// walk Shell DocObject View->Internet Explorer_Server
WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil);
if WndChild <> 0 then begin
WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);
if WndChild <> 0 then
begin
GetIEFromHWnd(WndChild, IE); //Get Iwebbrowser2 from Handle
Document := IE.Document as IHtmlDocument2;
if (assigned(Document)) then
begin
{All := Document.All;
for I := 0 to All.Length -1 do
begin
HtmlElement := All.item(i,0) as IhtmlElement;
if (assigned(Document)) then
SourceView.Lines.Add (HTmlElement.innerHTML);
end; }
HtmTitle:=Document.title;
Caption:=HtmTitle;
SourceView.Lines.clear;
{SourceView.Lines.Add('<html>');
SourceView.Lines.Add('<head>');
SourceView.Lines.Add('<title>'+HtmTitle+'</title>');
SourceView.Lines.Add('</head>'); }
SourceView.Lines.Add(IHtmlDocument2(Document).Body.innerHTML);
{SourceView.Lines.Add('</html>');}

PersistFile := IHtmlDocument2(Document) as IPersistFile;


end;
end;
end;
end;
 
to zw84611:
谢谢您的帮助。按照您的第二种方式之后,可以得到网页源码,我把它保存之后可以成
功,但我再次打开时则与原网页不一样。因为有一些元素没有保存下来。要怎么才能实现如
有IE中保存那样。
我的目的是想原样打印出来,如果不保存,直接调用打印行不?但我打印是想直接打印到指定的
已经存在的打印机,而不需要弹出选择打印机的窗口。
另,在explorer下呢?如何知道选择的文件名,或都没有选择,或者选择文件与文件夹了呢?
 
再请高手!!!!!
 
顶部