我用一个WebBrowser1来显示页面,希望打开任意一个页面时焦点和光标都停在页面中的第一个可以接受它们的窗口中(如输入框中),有朋友告我用WebBrowser1.OleObject.Document.ParentWindow.Focus(); 但实现不了,我在打开sohu.com网站首页时,焦点和光标都不在页面里,我希望能停在用户栏中。 请各位大虾指点!
解决方案 »
- Identifier expected but 'TYPE' found错误是何原因?是关于记录类型方面的错误
- DBGridEh如何根据不同数值设置单元格不同颜色?
- 在c/s客户端取服务器上音频文件播放的问题,请有这方面设计经验的兄弟指点一下!!(项目中!)
- 请问如何锁定注册表的一个键,使它不能被其它应用程序删除?
- 为什么ADOQuery删不掉记录?
- 管理经验求助!!
- 100求算法高手写一个高难度的函数!满意的话再送500分![这是一个挑战]
- delphi中如何把word中的数据导入到数据库中
- 高手们:如何实现“类似 Microsoft OutLook 式样的卷动按钮盒”?
- 怎样用API播放MIDI文件?
- 怎么用formatfloat函数
- 当程序全屏时如何解决程序上的东西按比例的移动?
我走到哪里都能看到你那魅力无限的名字,呵呵。
var IDisp:IDispatch;
pElement:IHTMLElement;
i:integer;
sID:string;
hasID:boolean;
begin
hasID:=false;
for I := 0 to Document.all.length - 1 do // 遍历所有HTML元素
begin
{得到当前的IDispatch接口}
IDisp:=Document.all.item(i,i);
IDisp.QueryInterface(IID_IHTMLElement,pElement);
if CompareText(pElement.tagName,'INPUT')=0 then
begin
hasID:=Length(pElement.id )>0;//如果该元素没有ID
if not hasID then pElement.id :='AZXUEJFLAIER34';//设置一个新的ID
sID:=pElement.id ;
Document.parentWindow.execScript(Format('%s.focus();',[sID]),'JScript');
{执行JavaScript,将焦点移动到该元素上}
if not hasID then pElement.id :='';//恢复原来的ID.
exit;
end;
end; // for
end;
举个例子:procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('about:<INPUT%20TYPE="TEXT">');end;procedure TForm1.Button1Click(Sender: TObject);
begin
Focus(WebBrowser1.Document as IHTMLDocument2);
end;你试试看,是不是可以用?呵呵....................................
你的例子能行,但当我把WebBrowser1.Navigate('about:<INPUT%20TYPE="TEXT">');改成
WebBrowser1.Navigate('http:\\www.sohu.com');后运行就出错了,说'AZXUEJFLAIER34'
没有定义。 另外,如果有多个INPUT,该如何识别呢?
procedure Focus(Document:IHTMLDocument2);
var IDisp:IDispatch;
pElement:IHTMLElement;
i:integer;
begin
for I := 0 to Document.all.length - 1 do // 遍历所有HTML元素
begin
{得到当前的IDispatch接口}
IDisp:=Document.all.item(i,i);
IDisp.QueryInterface(IID_IHTMLElement,pElement);
if CompareText(pElement.tagName,'INPUT')=0 then
begin
Document.parentWindow.execScript(Format('document.all(%d).focus();',[i]),'JScript');
{执行JavaScript,将焦点移动到该元素上}
exit;
end;
end; // for
end;:)