比如使用 WebBrowser 时:var
lItem: IHTMLElement;
begin
lItem := (WebBrowser.Document as IHTMLDocument2).all.item(i, varEmpty) as IHTMLElement;
{ 如何将 lItem 存到树节点的 Data 中... }
end;取出时:
TreeView1.Selected.Data 怎么转换为 IHTMLElement;再一个问题:
取得一个网页元素 lItem 后,(IHTMLElement 类型),如何选中这个元素?
lItem: IHTMLElement;
begin
lItem := (WebBrowser.Document as IHTMLDocument2).all.item(i, varEmpty) as IHTMLElement;
{ 如何将 lItem 存到树节点的 Data 中... }
end;取出时:
TreeView1.Selected.Data 怎么转换为 IHTMLElement;再一个问题:
取得一个网页元素 lItem 后,(IHTMLElement 类型),如何选中这个元素?
data1: Pointer;
aintf: IHTMLDocument3;
aNode1: IHTMLElement;
aNode2: IHTMLElement;
begin
//
aintf := WebBrowser1.Document as IHTMLDocument3;
aNode1 := aintf.documentElement; data1 := Pointer(aNode1);//存
//..........
aNode2 := IHTMLElement(data1);//取
ShowMessage( aNode2.tagName);
这是我按你的方法修改的代码:
存:
procedure TAllYuanSu.FullTreeItem(ABody: IHTMLElement);
procedure FullChildItem(AParent: TTreeNode; AItem: IHTMLElement);
var
m: Integer;
lItemNode: TTreeNode;
lChilds: IHTMLElementCollection;
lItem: IHTMLElement;
begin
lChilds := AItem.children as IHTMLElementCollection;
for m := 0 to lChilds.Length - 1 do
begin
lItem := lChilds.Item(m, varEmpty) as IHTMLElement;
lItemNode := tvItem.Items.AddChildObject(AParent, lItem.tagName, Pointer(lItem));
lItemNode.ImageIndex := GetImgIndex(lItem.tagName);
if (lItem.children as IHTMLElementCollection).Length <> 0 then
FullChildItem(lItemNode, lItem);
end;
end;
var
lChildren: IHTMLElementCollection;
i: Integer;
Item: IHTMLElement;
ParentNode, ItemNode: TTreeNode;
begin
tvItem.Items.Clear;
ParentNode := tvItem.Items.AddChildObject(nil, ABody.tagName, nil);
ParentNode.ImageIndex := GetImgIndex(ABody.tagName);
lChildren := ABody.children as IHTMLElementCollection;
for i:=0 to lChildren.Length-1 do
begin
Item := lChildren.item(i, varEmpty) as IHTMLElement;
ItemNode := tvItem.Items.AddChildObject(ParentNode, Item.tagName, Pointer(Item));
ItemNode.ImageIndex := GetImgIndex(Item.tagName);
if (Item.children as IHTMLElementCollection).Length <> 0 then
FullChildItem(ItemNode, Item);
end;
end;取:
procedure TAllYuanSu.tvItemClick(Sender: TObject);
var
Item: IHTMLElement;
begin
if tvItem.Selected = nil then Exit;
Item := IHTMLElement(tvItem.Selected.Data);
Panel1.Caption := Item.tagName;
end;
改成
if (tvItem.Selected = nil) or (tvItem.Selected.Data = nil) then Exit;
另外,因为Data仅是单纯的指针,不会为对象增加引用计数,在用的时候,要保证
HTMLelement与属于的htmlDocument对象都存在.
http://msdn2.microsoft.com/en-us/library/aa753624.aspx
我是第一次作这种与网页相关的程序,这方面非常陌生,很多概念都搞不清。
英文也不好,看MSDN也挺困难的。
IHTMLDocument2* pHtmlDoc2;
lpDispatch = Explorer.get_Document();
ASSERT(lpDispatch);
HRESULT hr=lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2);
if(hr!=S_OK)
{
return FALSE;
} IHTMLElementCollection *all;
pHtmlDoc2->get_all(&all);
IDispatch* distable;
all->item(COleVariant(strTableName.c_str()),COleVariant(short(0)),&distable);
IHTMLTable *pITable=NULL;
if(distable != NULL)
{
hr = distable->QueryInterface(IID_IHTMLTable, (void**)&pITable);//get table dispatch
ASSERT(hr==S_OK);
} IHTMLElementCollection *pIRows;
hr = pITable->get_rows(&pIRows);
ASSERT(hr==S_OK); IDispatch *disrow;
hr=pIRows->item(COleVariant(long(nRow)),COleVariant(short(0)),&disrow);
if(hr!=S_OK||disrow==NULL)
return FALSE; IHTMLTableRow *pIRow = NULL;
if(disrow != NULL)
{
hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
ASSERT(hr==S_OK);
} IHTMLElementCollection* rowcells = NULL;
if(pIRow != NULL)
{
pIRow->get_cells(&rowcells);
} IDispatch* discell = NULL;
if(rowcells != NULL)
{
hr=rowcells->item(COleVariant(long(nColumn)),COleVariant(short(0)),&discell);//iCol col
if(hr!=S_OK||discell==NULL)
return FALSE;
} IHTMLElement* cell = NULL;
if(discell != NULL)
{
hr=discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
ASSERT(hr==S_OK);
} CString strHtmlValue;
strHtmlValue = strValue.c_str();
BSTR bsStr = strHtmlValue.AllocSysString();
cell->put_innerHTML(bsStr);
SysFreeString(bsStr); cell->Release();
pIRow->Release();
pITable->Release();