在页面编辑环境下,如何定位所选中的表单项,在线等~~~

hubeicaolei 2003-02-25 10:40:40
在页面编辑时,当选中一个表单项,如text,select的时候,能够选中这个表单项的html代码,就像frontpage中,在普通模式中选中一个表单项,在原文件模式中会发现这个表单项的代码被选中一样。
如何用vc做到这一点?
...全文
9 点赞 收藏 2
写回复
2 条回复
蒋晟 2003年03月03日
In Microsoft Visual C++, this method call translates to IHTMLControlRange::execCommand. The following code illustrates how to implement the same technique in Visual C++ given an IDispatch pointer to the document that contains the image and the ID of the IMG element wrapped in a VARIANT structure (with a type VT_BSTR):

STDMETHODIMP CMyBrowser::CopyImage(LPDISPATCH pDispDoc, VARIANT vImageID)
{
HRESULT hr = E_FAIL;
IHTMLDocument2* pDoc = NULL;
IHTMLElement* pelmBody = NULL;
IHTMLElement2* pelmBodyTwo = NULL;
IDispatch* pdispImgElement = NULL;
IDispatch* pdispCtrlRange = NULL;
IHTMLElementCollection* pColl = NULL;
IHTMLControlElement* pCtrlElement = NULL;
IHTMLControlRange* pCtrlRange = NULL;
BSTR bstrCommand = SysAllocString(L"select");//can be replaced by copy,past,etc.
VARIANT_BOOL vbReturn;
VARIANT vEmpty;
VariantInit(&vEmpty);

if (pDispDoc == NULL)
goto Cleanup;

if (FAILED(pDispDoc->QueryInterface(IID_IHTMLDocument2, (void**) &pDoc)))
goto Cleanup;

if (FAILED(pDoc->get_all(&pColl)))
goto Cleanup;

if (FAILED(pColl->item(vImageID, vEmpty, &pdispImgElement))
|| pdispImgElement == NULL)
goto Cleanup;

if (FAILED(pDoc->get_body(&pelmBody)) || pelmBody == NULL)
goto Cleanup;

if (FAILED(pelmBody->QueryInterface(IID_IHTMLElement2, (void**) &pelmBodyTwo))
|| pelmBodyTwo == NULL)
goto Cleanup;

if (FAILED(pelmBodyTwo->createControlRange(&pdispCtrlRange))
|| pdispCtrlRange == NULL)
goto Cleanup;

if (FAILED(pdispCtrlRange->QueryInterface(IID_IHTMLControlRange, (void**) &pCtrlRange))
|| pCtrlRange == NULL)
goto Cleanup;

if (FAILED(pdispImgElement->QueryInterface(IID_IHTMLControlElement, (void**) &pCtrlElement))
|| pCtrlElement == NULL)
goto Cleanup;

hr = pCtrlRange->add(pCtrlElement);

if (SUCCEEDED(hr))
hr = pCtrlRange->execCommand(bstrCommand, VARIANT_FALSE, vEmpty, &vbReturn);

pCtrlElement->Release();
hr = S_OK;

Cleanup:

SysFreeString(bstrCommand);

if (pCtrlRange)
pCtrlRange->Release();

if (pdispCtrlRange)
pdispCtrlRange->Release();

if (pelmBodyTwo)
pelmBodyTwo->Release();

if (pelmBody)
pelmBody->Release();

if (pdispImgElement)
pdispImgElement->Release();

if (pColl)
pColl->Release();

if (pDispDoc)
pDispDoc->Release();

return hr;
}
回复 点赞
Tasehouny 2003年03月02日
我想这个不难吧,你在定位这个控件(edit,select等),就可以得到它的IHTMLElement指针,这样你就得得到它所对应的HTML代码,用outerHTML 方法得到,然后以这个得到HTML代码在源文件中查找,然后标记它。

回复 点赞
发动态
发帖子
HTML/XML
创建于2007-09-28

2513

社区成员

8070

社区内容

VC/MFC HTML/XML
社区公告
暂无公告