在程序上设置计时器?
问题描述:
我正在用Twebbrowser组件创建一个应用程序,需要导航到只有一个页面,应用程序将在Windows自动启动,因此有可能在第一次导航时没有任何互联网连接,所以我想检查对于页面的标题,如果不是正确的,再次导航。就像这样:在程序上设置计时器?
procedure TForm1.titlechange(Sender: TObject; const Text: WideString);
begin
if Text = 'Untitled Document' then
begin
StaticText1.Visible := False;
Timer4.Enabled := False;
end
else
webbrowser1.Navigate('http://website.com');
end;
我想有该过程的一个5秒计时器,如果导航成功并且标题是“无标题文档”计时器应禁用。
我该怎么做?
谢谢!
我回我原来的要求,我实现了提出的解决方案,但这只是有时工作,如果没有连接网络,有时有一个“导航到该网页被取消”这一触发OnDocumentComplete我在想, Twebbrowser中的LocationName函数本身具有错误的功能描述。
但是我原来的代码确实有效,我只需要一个定时器就可以了!有人可以帮我解决这个问题吗?
答
尝试使用OnNavigatError
和OnDocumentComplete
事件,而不是,如:
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer4Timer(nil);
end;
procedure TForm1.Timer4Timer(Sender: TObject);
begin
Timer4.Enabled := False;
webbrowser1.Navigate('http://website.com');
end;
procedure TForm1.webbrowser1NavigateError(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant; var Frame: OleVariant; var StatusCode: OleVariant; var Cancel: WordBool);
begin
Timer4.Enabled := True;
end;
procedure TForm1.webbrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
if webbrowser1.LocationName = 'Untitled Document' then
begin
StaticText1.Visible := False;
end
else begin
Timer4.Enabled := True;
end;
end;
您好,感谢您的回答,我有点一个Delphi新手,我代替我的定时器4与您的代码,并插入Timer4Timer(零);在我的FormCreate过程中,我在webbrowser1NavigateError,webbrowser1DocumentComplete和webbrowser1.LocationName上收到了未声明的标识符错误,我该怎么办? – user990767
@ user990767您必须单击浏览器组件,然后在对象浏览器的事件选项卡上,然后双击“DocumentComplete”事件,并复制webbrowser1DocumentComplete过程的内容。并为其他人重复。这里的过程名称是对象名称和事件的组合。 – mj2008