网页存在于本地硬盘上,这么用程序的方法实现修该它的title内容?

ghiewa 2003-06-08 03:52:55
如题
...全文
92 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghiewa 2003-06-11
  • 打赏
  • 举报
回复
好结账了!!
woainihaha 2003-06-10
  • 打赏
  • 举报
回复
不行呀,非对称在Title中一般比较少(几乎是不会出现的),如果要处理的话就得分析语法,太复杂了.你先用用,有什么问题再说吧.
woainihaha 2003-06-10
  • 打赏
  • 举报
回复
以上代码有些问题,在Title中有非对称标记时出错,我再改改.
woainihaha 2003-06-10
  • 打赏
  • 举报
回复
TStringList *htmfile = new TStringList;

AnsiString KeyWordBegin = "<TiTLE>";
AnsiString KeyWordEnd = "</TITLE>";

htmfile->LoadFromFile("1.htm");

AnsiString text = htmfile->Text;

delete htmfile;

int beginnum = 1;
int beginpos = 0;

int endnum = 1;
int endpos = 0;


for (int i=1; i<text.Length(); i++)
{
char tempchar = text[i];

if (beginnum == KeyWordBegin.Length())
{
beginpos = i + 1;
beginnum = 1;
}

int temp = tempchar - KeyWordBegin[beginnum];

if ((temp==0)||(temp==-32)||(temp==32)) beginnum++;
else beginnum=1;


if (endnum == KeyWordEnd.Length())
{
endpos = i-KeyWordEnd.Length()+1;
endnum = 1;
}

temp = tempchar - KeyWordEnd[endnum];

if ((temp==0)||(temp==-32)||(temp==32)) endnum++;
else endnum=1;

}

if ((endpos == 0) || (beginpos == 0))
{
ShowMessage("你所查找的文件没有你所要的关键字。");
return;
}

if (endpos < beginpos)
{
ShowMessage("你所查找的文件格式错误或已损坏。");
return;
}

if (endpos == beginpos)
{
ShowMessage("你所查找的文件关键字为空。");
return;
}

AnsiString theTitle = "";

for (int i=beginpos; i<endpos; i++)
theTitle = theTitle + text[i];

AnsiString lasttitle = "";

bool copyit = true;
bool tagBegin = false;

for (int i=1; i<=theTitle.Length(); i++)
{
char tempchar = theTitle[i];

if (tempchar == '<')
{
copyit = false;

}

if ((copyit)&&(!tagBegin)) lasttitle = lasttitle + tempchar;

if (tempchar == '>')
{
copyit = true;

tagBegin = ! tagBegin;

}

}
ghiewa 2003-06-10
  • 打赏
  • 举报
回复
不行不行,小弟太菜了  等待 Borland C++Builder版的
ghiewa 2003-06-10
  • 打赏
  • 举报
回复
我先试试,然后结账
叶子哟 2003-06-10
  • 打赏
  • 举报
回复
void CMyHtmlView::OnDesignMode()
{
// TODO: Add your command handler code here
HRESULT hr;
IHTMLDocument2* piDoc = 0;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
if(SUCCEEDED(hr))
{
if(m_MyClientSite.m_bDocDesignMode)
piDoc->put_designMode(L"Inherit");
else
piDoc->put_designMode(L"On");
m_MyClientSite.m_bDocDesignMode = !m_MyClientSite.m_bDocDesignMode;
piDoc->Release();
}
else
AfxMessageBox("QueryInterface doc");
piDisp->Release();
}
else
AfxMessageBox("get_Document");
}

void CMyHtmlView::OnSaveToFile()
{
// TODO: Add your command handler code here
CFileDialog dlg(FALSE, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
NULL, NULL);
if(dlg.DoModal()==IDCANCEL)
return;
_bstr_t path=dlg.GetPathName();

HRESULT hr;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IPersistFile* piPF = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IPersistFile, (void**)&piPF);
if(SUCCEEDED(hr))
{
piPF->Save(path, TRUE);
piPF->Release();
}
piDisp->Release();
}
}

void CMyHtmlView::OnModifyBody()
{
// TODO: Add your command handler code here
HRESULT hr;
IHTMLDocument2* piDoc = 0;
IWebBrowser2* piWeb = 0;
IDispatch* piDisp = 0;
IHTMLElement* piElem = 0;
IHTMLBodyElement* piBody = 0;
IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
if(FAILED(hr))
{
AfxMessageBox("QueryInterface for IWebBrowser2");
return;
}
hr = piWeb->get_Document(&piDisp);
if(SUCCEEDED(hr))
{
hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
if(SUCCEEDED(hr))
{
hr = piDoc->get_body(&piElem);
if(SUCCEEDED(hr))
{
hr = piElem->QueryInterface(IID_IHTMLBodyElement, (void**)&piBody);
if(SUCCEEDED(hr))
{
VARIANT v;

piBody->get_bgColor(&v);
_bstr_t bstr=v;
// AfxMessageBox((char*)bstr);

v.vt=VT_BSTR;
v.bstrVal = ::SysAllocString(OLESTR("#00FF00"));
piBody->put_bgColor(v);
::SysFreeString(v.bstrVal);
piBody->Release();
}
else
AfxMessageBox("QueryInterface body");
piElem->Release();
}
else
AfxMessageBox("get_body");
piDoc->Release();
}
else
AfxMessageBox("QueryInterface doc");
piDisp->Release();
}
else
AfxMessageBox("get_Document");
}

其中m_MyClientSite.m_bDocDesignMode是当前状态。
叶子哟 2003-06-10
  • 打赏
  • 举报
回复
你在显示时不用置设计模式呀!如果你要保存的话,才需要!
if(document->put_title((BSTR)L"sdhsjsj")!=S_OK )//加一个L就可了
实际你只要注意数据类型,vc的代码也可以用的
ghiewa 2003-06-10
  • 打赏
  • 举报
回复
matq2008(叶子.net) :帮我看看吧

brw->Navigate(WideString("C:\\Documents and Settings\\LiuJH\\桌面\\CSDN_近三年高级程序员试题知识点比较.htm"));
IHTMLDocument2 *document;
BSTR title,source,content;
IDispatch *WebDocument=static_cast<IDispatch*>(brw->Document);
document=static_cast<IHTMLDocument2*>(WebDocument);

BSTR pp;
document->put_designMode(BSTR("On"));
if(document->put_title((BSTR)("sdhsjsj"))!=S_OK )
ShowMessage("error");
//document->put_title(BSTR("sohagfhlashg"));
//document->get_title(&pp);
if(document) document->Release();

//Caption=AnsiString(pp);
叶子哟 2003-06-09
  • 打赏
  • 举报
回复
还有,如果你想保存修改后的网页,得首先修改designmode
叶子哟 2003-06-09
  • 打赏
  • 举报
回复
呵呵!
参照如下cookie的写法:
if(document->put_cookie((BSTR)L"mycati=1111;")!=S_OK )
主要是参数类型不对
pp616 2003-06-09
  • 打赏
  • 举报
回复
html本就是文本。你自己修改标记中间的字符不就好了吗。
ghiewa 2003-06-09
  • 打赏
  • 举报
回复
自己Up一下
ghiewa 2003-06-09
  • 打赏
  • 举报
回复
to netsys2(来电):
这个方法我已经实现了,我想有没有API来作这个事情呢,就象上面的那个
document->put_title(title);
netsys2 2003-06-09
  • 打赏
  • 举报
回复
自己编写文件读写函数啦,
打开源文件和目标文件,
读一段,写一段,

读到<title>和</title>之间的换成你自己的
ghiewa 2003-06-09
  • 打赏
  • 举报
回复
TCppWebBrowser* brw =new TCppWebBrowser();
brw->Visible=false;
brw->Navigate(WideString(path));
IHTMLDocument2 *document;
BSTR title,source,content;
IDispatch *WebDocument=static_cast<IDispatch*>(brw->Document);
document=static_cast<IHTMLDocument2*>(WebDocument);
BSTR pp;
document->put_title(title);

if(document) document->Release();
delete brw;

问什么里面的 document->put_title(title);
没有起作用啊?
ghiewa 2003-06-09
  • 打赏
  • 举报
回复
to woainihaha(笨笨):
你想要多少分
叶子哟 2003-06-09
  • 打赏
  • 举报
回复
搜一下vc的html版,有较详细的如何保存修改后的网页内容
woainihaha 2003-06-09
  • 打赏
  • 举报
回复
呵呵,我给你写,你给多少分?:)
ghiewa 2003-06-09
  • 打赏
  • 举报
回复
to alenwelkin(苍穹) :如果 <title></title>里面有javascrip脚本呢?
加载更多回复(5)

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧