delphi中给supermap的地图页面添加点

环境:

Delphi7中用WebBrowser打开地图页面,需要在其中添加一个起始点的显示。

supermap的地图显示用ajax实现。

解决办法:

1)在地图页面上添加显示点的javascript函数ShowPOI2(x,y,CallerID),可以添加在js文件中,比如说page.js或者customation.js等同地图页面一起载入的文件。

function ShowPOI2(x,y,callerid)
{
       var InnerHtmlName="<img src=’images/car2.gif’/><font face=’宋体’ size=’2′ color=’#800080′ style=’text-decoration: blink’>"+callerid+"</font>";
       map.CustomLayer.InsertMark("POIInfo1", x, y, 0, 0, InnerHtmlName,10);
       map.SetCenterAndZoom(x,y,8);
}

其中,

CustomLayer.InsertMark(id, x, y, w, h, innerHtml, className, zIndex)
添加自定义注记。如果地图中已经存在与该注记ID相同的对象,则替换存在的注记。
id:自定义注记的ID值。

x:对象的x坐标(地理坐标)

y:对象的y坐标(地理坐标)

w:对象的宽度(显示宽度),以象素(px)为单位

h: 对象的高度(显示高度),以象素(px)为单位

innerHtml:呈现时的HTML

className:呈现时的样式单CSS的ID

zIndex:自定义注记层的Index值。

摘自is.net的开发手册。

如果有多个点要同时显示的,并且都是用InsertMark添加的,那么第一个参数id,就必须不同;或者也可以考虑用addMark添加,addmark允许id相同。

2)   在Delphi中,用webbrowser载入地图页面,利用IHTMLWindow2的executeScript进行调用,传递x,y,callerid的值到ShowPOI函数。如下:

jsfunc := Format('ShowPOI2(%f,%f,%s)', [x, y, 
QuotedStr(ACallerID)]); frame_doc.parentWindow.execScript(jsfunc, 'JavaScript');


注意要引用到地图的frame,不然,会找不到相应的script。


3)  注意,如果点的值位于地图的显示范围之外,可能引起地图白屏,就是白茫茫一片,什么也看不到。

IE7,Delphi使用TWebBrowser提示psapi.dll出错

系统升级到IE7后,打开以前包含TWebBrowser的项目后,出错,显示

找不到过程入口点 GetProcessImageFileNameW PSAPI.DLL 动态链接库中。

后来,每次启动delphi,都会显示这个错误。

 

原因:

版本不符。IE7的WebBrowser控件为新的版本,依赖新的版本的psapi.dll,位于system32下,版本号为:5.2.3790.3959

查看delphi的bin路径,也发现了一个psapi.dll,版本为5.00.1641.1,而Delphi启动后,默认导入的是本目录下的psapi.dll,所以可能缺少了该函数而报错。

 

解决办法:

把Delphi的bin下的psapi.dll改名或删除。

Reading and Writing form elements


This page provides notes on how to read from and write to forms that are displayed in a web page in a TWebBrowser.

Contents:

The examples on this page assume that the page has been loaded into a TWebBrowser called WebBrowser.


Get the number of forms on a page

function NumberOfForms(document: IHTMLDocument2): integer;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Length;
end;

and to call it:

procedure TMyForm.Button1Click(Sender: TObject);
var
  nForms: integer;
begin
  nForms := NumberOfForms(WebBrowser.Document as IHTMLDocument2);
  ShowMessage('Form count: ' + IntToStr(nForms));
end;

Get a form by number

Pages can have more than one form on them. The following function will return an instance of a form by number – where the first form is numbered 0, the next 1 and so on:

function GetFormByNumber(document: IHTMLDocument2;
    formNumber: integer): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  if formNumber < forms.Length then
    result := forms.Item(formNumber,'') as IHTMLFormElement
  else
    result := nil;
end;

For an example of this function in use see “Get the name of a form” (below).


Get the name of a form

It should be appreciated that HTML does not require forms to be named.

var
  firstForm: IHTMLFormElement;
  document: IHTMLDocument2;
begin
  document := WebBrowser.Document as IHTMLDocument2;
  firstForm := GetFormByNumber(document,0);
  if Assigned(firstForm) then
    ShowMessage('Name of first form is ' + firstForm.Name)
  else
    ShowMessage('This page does not contain any forms');

Get a form by name

Since forms can be named, if you know the name of a form it may be preferable to access the form by name rather than by number.

function GetFormByName(document: IHTMLDocument2;
    const formName: string): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Item(formName,'') as IHTMLFormElement
end;

This function will return nil if there is no form with the required name.


List the names of all the fields on a form

function GetFormFieldNames(fromForm: IHTMLFormElement): TStringList;
var
  index: integer;
  field: IHTMLElement;
  input: IHTMLInputElement;
  select: IHTMLSelectElement;
  text: IHTMLTextAreaElement;
begin
  result := TStringList.Create;
  for index := 0 to fromForm.length do
  begin
    field := fromForm.Item(index,'') as IHTMLElement;
    if Assigned(field) then
    begin
      if field.tagName = 'INPUT' then
      begin
        // Input field.
        input := field as IHTMLInputElement;
        result.Add(input.name);
      end
      else if field.tagName = 'SELECT' then
      begin
        // Select field.
        select := field as IHTMLSelectElement;
        result.Add(select.name);
      end
      else if field.tagName = 'TEXTAREA' then
      begin
        // TextArea field.
        text := field as IHTMLTextAreaElement;
        result.Add(text.name);
      end;
    end;
  end;
end;

and to call it:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);
  fields := GetFormFieldNames(theForm);

  for index := 0 to fields.count-1 do
    ShowMessage('Field ' + IntToStr(index) + ' called ' + fields[index]);
end;

Get the value of a named field

To get the value of a named field from a form:

function GetFieldValue(fromForm: IHTMLFormElement;
    const fieldName: string): string;
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field := fromForm.Item(fieldName,'') as IHTMLElement;
  if not Assigned(field) then
    result := ''
  else
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      result := inputField.value
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      result := selectField.value
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      result := textField.value;
    end;
  end
end;

and to call it:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);
  ShowMessage('Field "name" has value ' + GetFieldValue(theForm,'name'));

Set the value of a named field

To set the value of a field:

procedure SetFieldValue(theForm: IHTMLFormElement;
    const fieldName: string; const newValue: string);
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field := theForm.Item(fieldName,'') as IHTMLElement;
  if Assigned(field) then
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      inputField.value := newValue;
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.value := newValue;
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      textField.value := newValue;
    end;
  end;
end;

and to call it:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);
  SetFieldValue(theForm,'name','Brian Cryer');

Submit the form

The above functions demonstrate how to read values from a form and how to set values in the form. The only thing remaining is how to submit a form. This si simply:

theForm.submit;

for example:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(document,0);
  SetFieldValue(theForm,'name','Brian Cryer');
  theForm.submit;

Note: Be aware that there are cases where this will not achieve the expected effect. This is because some forms are not actually submitted, but the action is either performed in response to JavaScript on a button or by the “onsubmit” handler of the form.

Reading and Writing form elements

How to call JavaScript functions in a TWebBrowser from Delphi

Introduction

On some occasions when using a TWebBrowser I’ve needed to use Delphi to call JavaScript functions contained in the current document.

This is quite easy to do. We’ll first examine the techniques then we’ll look at a case study that changes the font in an HTML document.

Overview of solution

The window object associated with an HTML document exposes a method – execScript – that enables JavaScript to be called. The first parameter of this method takes a string containing the required function call (complete with actual parameters). The method’s second parameter specifies the script language being used – in our case 'JavaScript'.

So, how do we get hold of the window object that exposes the required method? We simply use the parentWindow property of the web browser control’s document object. We need to ensure that a document object is available by first loading the required document into the web browser and then wait for it to finish loading.

Implementing the solution

Let us assume we have a TWebBrowser control on a form into which an HTML document has been loaded. Assume also that the HTML document defines a JavaScript function named Foo() that takes a string and an integer parameter. Assuming the document is fully loaded, we can call Foo() with a method similar to this:

uses
  MSHTML;
  
procedure TForm1.CallFoo(S: string; I: Integer);
  { Calls JavaScript Foo() function }
var
  Doc: IHTMLDocument2;      // current HTML document
  HTMLWindow: IHTMLWindow2; // parent window of current HTML document
  JSFn: string;             // stores JavaScipt function call
begin
  // Get reference to current document
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get parent window of current document
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  // Run JavaScript
  try
    JSFn := Format('Foo("%s",%d)', [S, I]);  // build function call
    HTMLWindow.execScript(JSFn, 'JavaScript'); // execute function
  except
    // handle exception in case JavaScript fails to run
  end;
end;

Listing 1

Let’s look at what’s going on here. We first check that a document is available and store a reference to in Doc. We then attempt to get a reference to the document’s parent window object and store it in HTMLWindow. Once we have a valid window object we call its execScript method, passing the call to Foo(). The parameters to Foo() must be literal values – we can’t pass variables. String literals must be enclosed by double or single quotes.

Escaping quotes in string literal parameters

When passing string literal parameters to JavaScript functions you need to be careful to escape any quote characters contained in the string, otherwise the quote will terminate the string prematurely. Since JavaScript can use either single or double quotes to delimit literal strings you may need to escape either of these types of quote by preceeding it with a backslash.

As an example, suppose we need to pass the string He didn't say "hello" to a JavaScript function. If we delimit the string with double quotes we pass the it as "He didn't say "hello"".

Alternatively we may delimit the string with single quotes and pass it as 'He didn't say "hello"'.

If the JavaScript function contains errors or doesn’t exist an exception will be raised. We may wish to handle such exceptions before returning.

Note that execScript always returns a varEmpty value so we can’t use it to return a value from a JavaScript function. This means we can only use execScript to call JavaScript functions that return no value or where we don’t need the return value.

Case study

In this case study we will develop a small application that displays an HTML document in a browser window. The document will contain a Javascript function – SetFont() – that can change document’s default font. The application will display a combo box containing a list of all installed screen fonts. When the user selects a font from the combo box the font used in the web browser will be changed accordingly. We do this by calling SetFont() from Delphi.

Firstly, create an HTML document with any content you choose and include the following JavaScript function in the document’s <head> section:

<script type="text/javascript">
<!--
  function SetFont(fontname) {
    document.body.style.fontFamily = fontname;
  }
// -->
</script>

Listing 2

This is the code that changes the font.

Now create a new delphi application and drop a TComboBox and a TWebBrowser on the main form. We will load the HTML document when the form is first shown. We will also load the screen fonts into the combo box at the same time. To do this create an OnShow event handler for the form with the following code:

procedure TForm1.FormShow(Sender: TObject);
  { Setup combo box and load document }
begin
  // Store screen fonts in combo box and disabled it
  ComboBox1.Items.Assign(Screen.Fonts);
  ComboBox1.Enabled := False;
  // Load the HTML page
  WebBrowser1.Navigate(ExtractFilePath(ParamStr(0)) + 'Test.html');
end;

Listing 3

Note that we disabled the combo box to prevent a font from being selected. We do this because to set a font we need to access the browser’s Document object – and this object is only available when the HTML document is fully loaded. TWebBrowser triggers an OnDocumentComplete event when the document has finished loading. Therefore we handle that event and enable the combo box:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
  { Document loaded: enable combo box }
begin
  ComboBox1.Enabled := True;
end;

Listing 4

We now come to the code that actually calls the JavaScript function. When the user selects a new font the combo box triggers its OnChange event. We handle this event by calling the JavaScript SetFont() function with the name of the selected font, as follows:

procedure TForm1.ComboBox1Change(Sender: TObject);
  { Make browser use selected font }
var
  Doc: IHTMLDocument2;      // current HTML document
  HTMLWindow: IHTMLWindow2; // parent window of current HTML document
  JSFn: string;             // stores JavaScipt function call
begin 
  // Get reference to current document
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get parent window of current document
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  // Run JavaScript
  try
    JSFn := 'SetFont(''' + ComboBox1.Text + ''')';
    HTMLWindow.execScript(JSFn, 'JavaScript');
  except
    // handle exception in case JavaScript fails to run
    ShowMessage('Error running JavaScript');
  end;
end;

Listing 5

This method is very similar to that described in the previous section. SetFont() is called with the name of the font selected in the combo box. If an exception is raised when the JavaScript is called we display a message.

We have now developed all the code. All that remains is to add the following uses clause to the implementation section of the unit to enable the program to compile:

uses
  SysUtils, MSHTML, Dialogs;

Listing 6

Demo code

The source code on the code study is available for download. The code was developed using Delphi 7 Professional.

This article is copyright © Peter D Johnson 2002-2007

Licensed under a Creative Commons LicenseOffsite icon.

 

How to call JavaScript functions in a TWebBrowser from Delphi

How to Execute a JavaScript Code on a TWebBrowser Document

The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi applications.

Here’s how to execute a custom script (JavaScript or VBScript) function on a HTML document loaded in the TWebBrowser control:

uses MSHTML_TLB, SHDocVw;
procedure ExecuteScript(doc: IHTMLDocument2; script: string; language: string) ;
begin
if doc <> nil then
begin
     if doc.parentWindow <> nil then
       doc.parentWindow.ExecScript(script, Olevariant(language)) ;
end;
end;

Usage (in some Button OnClick event handler, for example):

var
   script : string;
begin
//locate the first element with ID attribute = "main" and show its tag
   script := ‘var elemMain = document.getElementById("main") ; if (elemMain != null) { alert(elemMain.tagName) ; }’;
   ExecuteScript(EmbeddedWB1.Document as IHTMLDocument2, script, ‘javascript’)
end;

Note: More TWebBrowser Tips’n’Tricks!

几年前写的《作弊器编程》的东西(续)

《模拟器编程》-(针对IE控件WebBrowser编程-自动登录操作)
利用Delphi的Olevariant类型

单个frames的输入

var
  o : Olevariant;
begin
  o := WebBrowser.OleObject.document.all.item(‘LoginUserID’,0);   //找到登录用户名的输入框
  o.value := ‘TEST’;
  o := WebBrowser.oleobject.document.all.item(‘LoginPassword’,0); //找到登录密码的输入框
  o.value := ‘TEST’
  WebBrowser.oleobject.document.Forms.Item(0, 0).submit;          //第一个表单提交

{

  o :=WebBrowser.oleobject.document.all.item(‘Login’,0);          //或者用指定表单名称提交
  o.Click;  //点击操作,对其它对象也可同样操作
}
end;

多个frames的输入,FrameIndex为Frame的序号

var
  o : Olevariant;
begin

//找到登录用户名的输入框
  o := WebBrowser.oleobject.document.documentelement.document.frames.item(FrameIndex).document.all.item(‘LoginUserID’,0);  
  o.value := ‘TEST’;

//找到登录密码的输入框
  o := WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.all.item(‘LoginPassword’,0);
  o.value := ‘TEST’

//第一个表单提交
  WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.Forms.Item(0, 0).submit;         

{

//或者用指定表单名称提交

  o :=WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex)..document.all.item(‘Login’,0);         
  o.Click;   //点击操作,对其它对象也可同样操作
}
end;

《模拟器编程》-(针对IE控件WebBrowser编程-找出所有LINK对象对应的Href)
在模拟器编程中,我们常要找出所有要操作的LINK对象。如果是我们需要的,就对它进行操作。在一个网页中作为判断的依据或其它用途

单个frames的LINK对象

var
   Count:Integer; 

  LinkList:TStringList;
begin

  LinkList:=TStringList.Create;

  Try

    for Count :=0 to WebBrowser.OleObject.Document.Links.Count -1 do

      LinkList.Add(WebBrowser.OleObject.Document.Links.Item(Count).Href);

    {以下对取得的LINK对象内容HREF做相应操作,如判断、Navigate等}

  finally

    LinkList.Free;

  end;

end;

多个frames的LINK对象,FrameIndex为Frame的序号

var
   Count:Integer;  

  LinkList:TStringList;
begin

  LinkList:=TStringList.Create;

  Try

    for Count =0 to WebBrowser.OleObject.Document.documentelement.document.frames.item(FrameIndex).Document.Links.Count -1 do

      LinkList.Add(WebBrowser.OleObject.Document.documentelement.document.frames.item(FrameIndex).Document.Links.Item(Count).Href);

    {以下对取得的LINK对象内容HREF做相应操作,如判断、Navigate等}

  finally

    LinkList.Free;

  end;

end;

《模拟器编程》-针对IE控件WebBrowser编程-TAGNAME属性和SOURCEINDEX属性
  如何取得HTML页面中某个位置的内容,如我的登录姓名,现在我取得的$数据,我还有多少SPIN可用等等;下面我们就来谈谈这个东西。

  HTML中每一个保留字都由开始和结束组成就像《和》一样,是成对出现的。在HTML语法中常见的如<HTML></HTML>,<TD></TD>等,我们不需自己去计算这些内容,因为使用了WebBrowser后,它已经帮我们做到了。

  同时,在WebBrowser中,有一个极有用的属性,即SourceIndex属性,它将所有上述成对出现的对象能够唯一在页面中标识(不信的话你试试看,查找TD属性的对象就会出一大堆),能使我们找出唯一我们所需的内容。

在单个frames中查找相同属性的对象

var
   Count:Integer; 

begin

  for Count :=0 to WebBrowser.OleObject.Document.All.Length -1 do  //当前页面中所有对象数量

  Begin

    if WebBrowser.OleObject.Document.All.Item(Count).TagName =’TD’ then  //找出所有属性为TD的对象

    Begin

       ShowMessage(WebBrowser.OleObject.Document.All.Item(Count).InnerText);  //显示找到的对象的文本信息

       ShowMessage(WebBrowser.OleObject.Document.All.Item(Count).InnerHtml);  //显示找到的对象的HTML源码信息

    end;

  end;

end;

OK,现在找出相同属性的内容了,唯一值呢?

  if WebBrowser.OleObject.Document.All.Item(Count).SourceIndex = XXX then

  …

当然,你需首先得到XXX的值,怎样得到它?难到你不知道?

  自己做个工具吧;如果你比我还懒,算了,我这里做了一个工具,它可取得当前所有对象的唯一ID,就是SourceIndex了,当然有其它几个功能,不过用来完成刚才所讲内容,足够了,如果你认为我做的不行,好了,你多加几个功能吧;记得做好后给我Email一份。

  下载地点?当然在我的源码荟萃里了;没有?哦,我还没上传呢,等等吧!

多个frames的LINK对象,FrameIndex为Frame的序号

  我就不想多写了,所有多个Frames和前面所讲操作一样,如果你连这个都颔会不了的话,我劝你还是不要学了,别人做好后你用这行了。

另外,取得HTML页面中有几个Frames就这样做

  WebBrowser.OleObject.Document.documentelement.document.frames.Length

明白了吗?

现在我们做一个通用的函数,可按TagName和SourceIndex取得相应的文本内容:

// 取得TAGNAME指定的元素文本内容
function GetWebBrowserDocumentInnerText(Web:TWebBrowser;Const FramIndex,SourceIndex:Integer;Const TagName:String):String;
Var
  Count,Index :Integer;
  Tmp:String;
begin
  Result :=”;

//如果FramIndex为-1的话,表示只有一个Frame,否则,就是想操作的Frame序号
  if FramIndex = -1 then
  Begin
    for Count := 0 to web.oleobject.document.All.Length -1 do
    Begin
      tmp :=web.oleobject.document.All.Item(Count).TagName;
      Index :=web.oleobject.document.All.Item(Count).SourceIndex;
      if (tmp = TagName) and (SourceIndex =Index) then
      Begin
        Result :=web.oleobject.document.All.Item(Count).InnerText;
        Exit;
      end;
    end;
    Exit;
  end;
  for Count := 0 to web.oleobject.document.documentelement.document.frames.item(FramIndex).document.All.Length -1 do
  Begin
    tmp :=web.oleobject.document.documentelement.document.frames.item(FramIndex).document.All.Item(Count).TagName;
    Index :=web.oleobject.document.documentelement.document.frames.item(FramIndex).document.All.Item(Count).SourceIndex;
    if (tmp = TagName) and (SourceIndex =Index) then
    Begin
      Result :=web.oleobject.document.documentelement.document.frames.item(FramIndex).document.All.Item(Count).InnerText;
      Exit;
    end;
  end;
end;

《作弊器编程》-WinAPI-Mouse控制
  作弊器主要利用程序完成人对计算机的某些操作,如Mouse、键盘等外设的操作,让其它程序认为是人在计算机旁操作一样。今天谈谈Mouse的模拟操作。这些内容就需要用到WinAPI中的函数了。

  WinAPI中对Mouse的操作函数有:

  1、移动Mouse位置:SetCursorPos

    SetCursorPos(
      X: Integer; {X coordinate of the cursor}
      Y: Integer {Y coordinate of the cursor}
    ): BOOL; {returns TRUE or FALSE}
    Description
      The SetCursorPos function relocates the mouse cursor to the location specified by the X and Y parameters, in screen     coordinates. If the cursor is confined to a rectangular region by calling the ClipCursor function, the system translates the coordinates to the appropriate coordinates within the rectangular region.
    Parameters
      X: Specifies the new x-coordinate for the cursor.
      Y: Specifies the new y-coordinate for the cursor.
    Return Value
      If the function succeeds, it returns TRUE; otherwise it returns FALSE. To get extended error information, call the     GetLastError function.

   2、取得Mouse位置:GetCursorPos

     GetCursorPos(

       var lpPoint: TPoint {receives coordinates of cursor}
      ): BOOL; {returns TRUE or FALSE}
     Description
       The GetCursorPos function retrieves the mouse cursor position relative to the screen.
     Parameters
       lpPoint: Points to TPoint structure which receives the current mouse cursor抯 position in screen coordinates. This   structure must be allocated by the caller.
     Return Value
If the function succeeds, it returns TRUE; otherwise it returns FALSE. To get extended error information, call the GetLastError function.

   3、模拟Mouse点击:Mouse_Event
     mouse_event(
       dwFlags: DWORD; {mouse activity codes}
       dx: DWORD; {horizontal location or change}
       dy: DWORD; {vertical location or change}
       dwData: DWORD; {wheel movement amount}
       dwExtraInfo: DWORD {application defined data}
     ); {this procedure does not return a value}
     Description
     The mouse_event function simulates mouse activity. The system generates mouse messages as if the mouse was actually moved or a mouse button was actually pressed.
     Parameters
       dwFlags: Specifies which kind of mouse activity to simulate.
       dx: Specifies the horizontal location or change in location. If the dwFlags parameter contains the MOUSEEVENTF_ABSOLUTE flag, this parameter specifies a location. Otherwise, this parameter specifies the amount of mickeys (a measurement of mouse distance) to move.
       dy: Specifies the vertical location or change in location. If the dwFlags parameter contains the MOUSEEVENTF_ABSOLUTE flag, this parameter specifies a location. Otherwise, this parameter specifies the amount of mickeys (a measurement of mouse distance) to move.
       dwData: Specifies the amount of wheel movement if the dwFlags parameter contains the MOUSEEVENTF_WHEEL flag. A positive value indicates wheel movement away from the user; a negative value indicates wheel movement toward the user. This value is in terms of WHEEL_DELTA, approximately 120 mickeys. If the dwFlags parameter does not contain the MOUSEEVENTF_WHEEL flag, dwData should be set to zero.
       dwExtraInfo: 32 bits of additional application defined data. To retrieve this data, call the GetMessageExtraInfo function.

下面的代码是我们的一个练习,将Mouse先移到屏幕0,0位置,然后移动至中间,并单击:

var

  Count:Integer;

Begin

  SetCursorPOS(0,0);        //称动Mouse到0,0;其实,用Mouse_Event也能移动操作

  for Count :=0 to 400 do   //屏幕大小为800*600

    SetCursorPOS(Count,0);  //Y为0

//以下为单击操作,双击怎么做?回去好好想想

  Mouse_Event(MOUSEEVENTF_LEFTDOWN ,400,0,0,0);  //在400,0处按下Mouse左键

//Mouse_Event(MOUSEEVENTF_RIGHTDOWN ,400,0,0,0);  //不用说,右键

  Mouse_Event(MOUSEEVENTF_LEFTUP ,400,0,0,0);  //在400,0处放开Mouse左键

//Mouse_Event(MOUSEEVENTF_RIGHTUP ,400,0,0,0);  //不用说,右键

end;

这不就是一个自动点击机吗?当然,不完全,但自动点击的操作不就完成了吗!就这么简单

《作弊器编程》-WinAPI-键盘控制

  WinAPI中对键盘的操作函数只能用于Application自身,要想用自己的程序给其它程序窗体发送键码,需用到Hook(天,Hook可就复杂多了):而取得其它窗口输入的键码,就需用全局的Dll了(我晕,太多了)

  就里不再写代码了,在CashSoldier造钱战士 1.0 VIP源码的窗体控制中有全部源码(太多,这里写不了)un_SendKey.pas

  使用方法:

Const

  ControlKey = ‘^’;
  AltKey = ‘@’;
  ShiftKey = ‘~’;
  KeyGroupOpen = ‘{‘;
  KeyGroupClose = ‘}’;

//如当前窗口是IE窗口

Var

  Keys:String;

Begin

  Keys :=’@D’;     //按下 Alt+D,焦点为IE的地址输入框

  SendKeys(Keys);   //向当前窗口发送键码

  WaitForHook;      //等待结束

  Keys :=’http://alin.51.net’+$13;  //输入内容并按回车

  SendKeys(Keys);   //向当前窗口发送键码

  WaitForHook;      //等待结束

end;

《作弊器编程》-WinAPI-窗口控制

      一般来讲,对于我们常用的窗口控制的功能有如下要求:

    1、得到当前运行中的所有窗口信息

    2、取得最前端的窗口信息

    3、取得想控制的窗口信息等等

    在WinAPI中针对窗口控制的函数很多,完成以上功能的一般用的就以下几个函数:

    EnumWindows     取得当前系统中所有窗口句柄

    FindWindow      查找窗口

    SetWindowPos    设置窗口位置

    GetWindowRect   取得窗口大小

    …

    作弊器的目的在于让被蒙弊的程序认为我们的程序操作是由人在控制。同时,作弊器需像人一样取得针对的窗口信息,对它进行相应的操作。以上几个函数能够基本完成我们现在现要的操作。在CashSoldier造钱战士中的窗口控制及Spedia模拟器中,大量使用了以上函数;当然,所有函数每一个人使用都会采用不同的方法,如ShowWindow,MoveWindow可以用SetWindowPos来完成相应的功能。

    现在我们利用以上函数做一个自动点击机,它能找出广告窗口,同时每隔十秒点击相应的广告位置;(我们假设该广告条要点击的窗口ClassName为TBanner,广告条程序名称为Banner.EXE。广告条标题为SpediaBanner)。

function EnumWindowsProc(AHWnd: HWnd;LPARAM: lParam): boolean; stdcall;
var
  WndCaption: array[0..254] of char;             //窗口标题
  WndClassName: array[0..254] of char;           //窗口类名

  ARect:TRect;                                   //窗口坐标
begin
  if IsWindowVisible(AHwnd) then                 //是否可见窗口(我们要的广告窗口难道是不可见的吗?)
  Begin
    GetWindowText(AHWnd, @WndCaption, 254);      //取得窗口标题
    GetClassName(AHWnd, @WndClassName, 254);     //取得窗口类名

    if (StrPas(WndCaption) = ‘SpediaBanner’) and //是否运行的Banner.exe

       (StrPas(WndClassName = ‘TBanner’) then    //是否运行的窗口类为TBanner

    Begin                                        //是我们想要的窗口

      GetWindowRect(AHwnd,ARect);                //取得广告窗口的坐标位置(为什么要取出来呢?)

      ShowWindow(AHwnd,SW_SHOWNORMAL);           //按正常方式显示广告窗口(如果被隐藏了,不就对了吗)

//设置窗口位置及大小;实际上并没有改变大小和位置,因为用ShowWindow时,如果窗口被最小化时,就不起作用了(现在知道为何要先取坐标位置了吧)
      SetWindowPos(AHwnd,HWND_TOP,AHwnd.Left,AHwnd.Top,AHwnd.Right,AHwnd.Bottom,SWP_SHOWWINDOW);
      SetCursorPos(AHwnd.Top+10,AHwnd.Left+30);  //移动鼠标到广告条上(我随便加的位置,根据实际位置设置)

//单击广告条
      Mouse_Event(MOUSEEVENTF_LEFTDOWN ,AHwnd.Top+10,AHwnd.Left+30,0,0);
      Mouse_Event(MOUSEEVENTF_LEFTUP ,AHwnd.Top+10,AHwnd.Left+30,0,0);   

      Result :=False;                            //没必要再作枚举吧

      Exit;

    end;
  end;
  Result := True;            //返回TRUE表示继续下一个,否则,不再枚举
end;

procedure TForm.tm_AutoTimer(Sender: TObject);   //时钟控件,每隔一秒触发一次
begin
  tm_Auto.Enabled :=False;

  tm_Auto.Tag :=tm_Auto.Tag +1                   //每秒加一

  if tm_Auto.Tag >= 10 then                      //十秒触发一次

  Begin

    tm_Auto.Tag :=0;                             //恢复计数
    EnumWindows(@EnumWindowsProc, 0);            //取当前所有窗口

  end;
  tm_Auto.Enabled :=True;
end;

end;

几年前写的《作弊器编程》的东西(续) — nibulblog — [北方博客]

Delphi的TWebBrowser编程综述

转自:安全中国        

Delphi3开始有了TWebBrowser构件,不过那时是以ActiveX控件的形式出现的,而且需要自己引入,在其后的4.0和5.0中,它就在封装好shdocvw.dll之后作为Internet构件组之一出现在构件面板上了。常常听到有人骂Delphi的帮助做得极差,这次的TWebBrowser又是Microsoft的东东,自然不会好到哪里去,虽说MSDN上什么都有,可是内容太过庞杂,如果没有入口点更是件烦人的事,查找起来给人的感觉大概可以用一句话来形容:非常复杂、复杂非常。 
   这里有平时我自己用TWebBrowser做程序的一些心得和上网收集到的部分例子和资料,整理了一下,希望能给有兴趣用TWebBrowser编程的朋友带来些帮助。 
   1、初始化和终止化(Initialization & Finalization) 
     大家在执行TWebBrowser的某个方法以进行期望的操作,如ExecWB等的时候可能都碰到过“试图激活未注册的丢失目标”或“OLE对象未注册”等错误,或者并没有出错但是得不到希望的结果,比如不能将选中的网页内容复制到剪贴板等。以前用它编程的时候,我发现ExecWB有时侯起作用但有时侯又不行,在Delphi生成的缺省工程主窗口上加入TWebBrowser,运行时并不会出现“OLE对象未注册”的错误。同样是一个偶然的机会,我才知道OLE对象需要初始化和终止化(懂得的东东实在太少了)。 
     我用我的前一篇文章《Delphi程序窗口动画&正常排列平铺的解决》所说的方法编程,运行时出了上面所说的错误,我便猜想应该有OleInitialize之类的语句,于是,找到并加上了下面几句话,终于搞定!究其原因,我想大概是由于TWebBrowser是一个嵌入的OLE对象而不算是用Delphi编写的VCL吧。 
     initialization 
      OleInitialize(nil); 
     finalization 
      try 
       OleUninitialize; 
      except 
      end; 
     这几句话放在主窗口所有语句之后,“end.”之前。 
   ———————————————————————————–
   2、EmptyParam 
     在Delphi 5中TWebBrowser的Navigate方法被多次重载: 
     procedure Navigate(const URL: WideString); overload; 
     procedure Navigate(const URL: WideString; var Flags: 
   OleVariant); overload; 
     procedure Navigate(const URL: WideString; var Flags: 
   OleVariant; var TargetFrameName:     OleVariant); overload; 
     procedure Navigate(const URL: WideString; var Flags: 
   OleVariant; var TargetFrameName:     OleVariant; var PostData: 
   OleVariant); overload; 
     procedure Navigate(const URL: WideString; var Flags: 
   OleVariant; var TargetFrameName:     OleVariant; var PostData: 
   OleVariant; var Headers: OleVariant); overload; 
     而在实际应用中,使用后几种方法调用时,由于我们很少用到后面几个参数,但函数声明又要求是变量参数,一般的做法如下: 
     var 
      t:OleVariant; 
     begin 
      webbrowser1.Navigate(edit1.text,t,t,t,t); 
     end; 
     需要定义变量t(还有很多地方要用到它),很麻烦。其实我们可以用EmptyParam来代替(EmptyParam是一个公用的Variant空变量,不要对它赋值),只需一句话就可以了: 
     webbrowser1.Navigate(edit1.text,EmptyParam,EmptyParam,EmptyParam,EmptyParam); 
     虽然长一点,但比每次都定义变量方便得多。当然,也可以使用第一种方式。 
     webbrowser1.Navigate(edit1.text) 
   ———————————————————————————–
   3、命令操作   常用的命令操作用ExecWB方法即可完成,ExecWB同样多次被重载: 
     procedure ExecWB(cmdID: OLECMDID; cmdexecopt:   OLECMDEXECOPT); overload; 
     procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT; 
                                        var pvaIn:  OleVariant); overload; 
     procedure ExecWB(cmdID: rOLECMDID; cmdexecopt: OLECMDEXECOPT; 
                                             var pvaIn:   OleVariant; var pvaOut: OleVariant); overload; 

     打开: 弹出“打开Internet地址”对话框,CommandID为OLECMDID_OPEN(若浏览器版本为IE5.0, 
         则此命令不可用)。 
     另存为:调用“另存为”对话框。 
         ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,   EmptyParam,  EmptyParam); 

     打印、打印预览和页面设置: 调用“打印”、“打印预览”和“页面设置”对话框(IE5.5及以上版本才支持打 
                   印预览,故实现应该检查此命令是否可用)。  

         ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, 
                                      EmptyParam,  EmptyParam); 
         if QueryStatusWB(OLECMDID_PRINTPREVIEW)=3 then 
          ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT, 
              EmptyParam,EmptyParam); 
         ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT,   EmptyParam,  EmptyParam); 

     剪切、复制、粘贴、全选: 功能无须多说,需要注意的是:剪切和粘贴不仅对编辑框文字,而且对网页上的非编 
                  辑框文字同样有效,用得好的话,也许可以做出功能特殊的东东。获得其命令使能状 
                  态和执行命令的方法有两种(以复制为例,剪切、粘贴和全选分别将各自的关键字替 
                  换即可,分别为CUT,PASTE和SELECTALL): 
      A、用TWebBrowser的QueryStatusWB方法。 
        if(QueryStatusWB(OLECMDID_COPY)=OLECMDF_ENABLED) or 
         OLECMDF_SUPPORTED) then 
         ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT, 
   EmptyParam, 
              EmptyParam); 
      B、用IHTMLDocument2的QueryCommandEnabled方法。 
        var 
         Doc: IHTMLDocument2; 
        begin 
         Doc :=WebBrowser1.Document as IHTMLDocument2; 
         if Doc.QueryCommandEnabled(’Copy’) then 
          Doc.ExecCommand(’Copy’,false,EmptyParam); 
        end; 
     查找: 参考第九条“查找”功能。 
   ———————————————————————————–
   4、字体大小 
     类似“字体”菜单上的从“最大”到“最小”五项(对应整数0~4,Largest等假设为五个菜单项的名字,Tag 
   属性分别设为0~4)。 
      A、读取当前页面字体大小。 
        var 
         t: OleVariant; 
        Begin 
         WebBrowser1.ExecWB(OLECMDID_ZOOM,  OLECMDEXECOPT_DONTPROMPTUSER,  EmptyParam,t); 
         case t of 
         4: Largest.Checked :=true; 
         3: Larger.Checked :=true; 
         2: Middle.Checked :=true; 
         1: Small.Checked :=true; 
         0: Smallest.Checked :=true; 
         end; 
        end; 
      B、设置页面字体大小。 
        Largest.Checked :=false; 
        Larger.Checked :=false; 
        Middle.Checked :=false; 
        Small.Checked :=false; 
        Smallest.Checked :=false; 
        TMenuItem(Sender).Checked :=true; 
        t :=TMenuItem(Sender).Tag; 
        WebBrowser1.ExecWB(OLECMDID_ZOOM,  OLECMDEXECOPT_DONTPROMPTUSER,  t,t); 
   ———————————————————————————–

   5、添加到收藏夹和整理收藏夹 
   

 const 
        CLSID_ShellUIHelper: TGUID =  ’{64AB4BB7-111E-11D1-8F79-00C04FC2FBE1}’; 
       var 
        p:procedure(Handle: THandle; Path: PChar); stdcall; 
       procedure TForm1.OrganizeFavorite(Sender: Tobject); 
       var 
        H: HWnd; 
       begin 
        H := LoadLibrary(PChar(’shdocvw.dll’)); 
        if H <> 0 then 
        begin 
              p := GetProcAddress(H, PChar(’DoOrganizeFavDlg’)); 
         if Assigned(p) then p(Application.Handle,   PChar(FavFolder)); 
        end; 
         FreeLibrary(h); 
       end; 
       procedure TForm1.AddFavorite(Sender: TObject); 
       var 
        ShellUIHelper: ISHellUIHelper; 
        url, title: Olevariant; 
       begin 
        Title := Webbrowser1.LocationName; 
        Url := Webbrowser1.LocationUrl; 
        if Url <> ’’ then 
        begin 
         ShellUIHelper := CreateComObject(CLSID_SHELLUIHELPER) as 
   IShellUIHelper; 
         ShellUIHelper.AddFavorite(url, title); 
        end; 
       end;

 

用上面的通过ISHellUIHelper接口来打开“添加到收藏夹”对话框的方法比较简单,但是有个缺陷,就是打开的窗口不是模式窗口,而是独立于应用程序的。可以想象,如果使用与OrganizeFavorite过程同样的方法来打开对话框,由于可以指定父窗口的句柄,自然可以实现模式窗口(效果与在资源管理器和IE中打开“添加到收藏夹”对话框相同)。问题显然是这样的,上面两个过程的作者当时只知道shdocvw.dll中DoOrganizeFavDlg的原型而不知道DoAddToFavDlg的原型,所以只好用ISHellUIHelper接口来实现(或许是他不够严谨,认为是否是模式窗口无所谓?)。 
     下面的过程就告诉你DoAddToFavDlg的函数原型。需要注意的是,这样打开的对话框并不执行“添加到收藏夹”的操作,它只是告诉应用程序用户是否选择了“确定”,同时在DoAddToFavDlg的第二个参数中返回用户希望放置Internet快捷方式的路径,建立.Url文件的工作由应用程序自己来完成。 
       procedure TForm1.AddFavorite(IE: TEmbeddedWB); 
        procedure CreateUrl(AUrlPath, AUrl: PChar); 
        var 
         URLfile: TIniFile; 
        begin 
         URLfile := TIniFile.Create(String(AUrlPath)); 
        RLfile.WriteString(’InternetShortcut’, ’URL’, 
   String(AUrl)); 
        RLfile.Free; 
        end; 
       var 
        AddFav: function(Handle: THandle; 
         UrlPath: PChar; UrlPathSize: Cardinal; 
         Title: PChar; TitleSize: Cardinal; 
         FavIDLIST: pItemIDList): Bool; stdcall; 
        FDoc: IHTMLDocument2; 
        UrlPath, url, title: array[0..MAX_PATH] of char; 
        H: HWnd; 
        pidl: pItemIDList; 
        FRetOK: Bool; 
       begin 
        FDoc := IHTMLDocument2(IE.Document); 
        if FDoc = nil then exit; 
        StrPCopy(Title, FDoc.Get_title); 
        StrPCopy(url, FDoc.Get_url); 
        if Url <> ’’ then 
        begin 
         H := LoadLibrary(PChar(’shdocvw.dll’)); 
         if H <> 0 then 
         begin 
          SHGetSpecialFolderLocation(0, CSIDL_FAVORITES, pidl); 
          AddFav := GetProcAddress(H, PChar(’DoAddToFavDlg’)); 
          if Assigned(AddFav) then 
           FRetOK :=AddFav(Handle, UrlPath, Sizeof(UrlPath), 
   Title, Sizeof(Title), pidl) 
         end; 
         FreeLibrary(h); 
         if FRetOK then 
          CreateUrl(UrlPath, Url); 
        end 
       end; 
   ———————————————————————————–
   6、使WebBrowser获得焦点 
     TWebBrowser非常特殊,它从TWinControl继承来的SetFocus方法并不能使得它所包含的文档获得焦点,从而不能立即使用Internet 
   Explorer本身具有得快捷键,解决方法如下:< 
     procedure TForm1.SetFocusToDoc; 
     begin 
      if WebBrowser1.Document <> nil then 
       with WebBrowser1.Application as IOleobject do 
        DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, 
   GetClientRect); 
     end; 
     除此之外,我还找到一种更简单的方法,这里一并列出: 
     if WebBrowser1.Document <> nil then 
      IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow).focus 
     刚找到了更简单的方法,也许是最简单的: 
     if WebBrowser1.Document <> nil then 
      IHTMLWindow4(WebBrowser1.Document).focus 
     还有,需要判断文档是否获得焦点这样来做: 
     if IHTMLWindow4(WebBrowser1.Document).hasfocus then 
   ———————————————————————————–

   7、点击“提交”按钮 
     如同程序里每个窗体上有一个“缺省”按钮一样,Web页面上的每个Form也有一个“缺省”按钮——即属性为“Submit”的按钮,当用户按下回车键时就相当于鼠标单击了“Submit”。但是TWebBrowser似乎并不响应回车键,并且,即使把包含TWebBrowser的窗体的KeyPreview设为True,在窗体的KeyPress事件里还是不能截获用户向TWebBrowser发出的按键。

我的解决办法是用ApplicatinEvents构件或者自己编写TApplication对象的OnMessage事件,在其中判断消息类型,对键盘消息做出响应。至于点击“提交”按钮,可以通过分析网页源代码的方法来实现,不过我找到了更为简单快捷的方法,有两种,第一种是我自己想出来的,另一种是别人写的代码,这里都提供给大家,以做参考。 
     A、用SendKeys函数向WebBrowser发送回车键 
       在Delphi 
   5光盘上的InfoExtrasSendKeys目录下有一个SndKey32.pas文件,其中包含了两个函数SendKeys和AppActivate,我们可以用SendKeys函数来向WebBrowser发送回车键,我现在用的就是这个方法,使用很简单,在WebBrowser获得焦点的情况下(不要求WebBrowser所包含的文档获得焦点),用一条语句即可: 
      Sendkeys(’~’,true);// press RETURN key 
      SendKeys函数的详细参数说明等,均包含在SndKey32.pas文件中。 
     B、在OnMessage事件中将接受到的键盘消息传递给WebBrowser。 
      procedure TForm1.ApplicationEvents1Message(var Msg: TMsg; 
   var Handled: Boolean); 
      {fixes the malfunction of some keys within webbrowser 
   control} 
      const 
       StdKeys = [VK_TAB, VK_RETURN]; { standard keys } 
       ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT]; { 
   extended keys } 
       fExtended = $01000000; { extended key flag } 
      begin 
       Handled := False; 
       with Msg do 
       if ((Message >= WM_KEYFIRST) and (Message <= WM_KEYLAST)) 
   and 
        ((wParam in StdKeys) or 
        {$IFDEF VER120}(GetKeyState(VK_CONTROL) < 0) or {$ENDIF} 
        (wParam in ExtKeys) and 
        ((lParam and fExtended) = fExtended)) then 
       try 
        if IsChild(Handle, hWnd) then { handles all browser 
   related messages } 
        begin 
         with {$IFDEF 
   VER120}Application_{$ELSE}Application{$ENDIF} as 
           IOleInPlaceActiveObject do 
          Handled := TranslateAccelerator(Msg) = S_OK; 
          if not Handled then 
          begin 
           Handled := True; 
           TranslateMessage(Msg); 
           DispatchMessage(Msg); 
          end; 
          end; 
       except 
       end; 
      end; // MessageHandler 
     (此方法来自EmbeddedWB.pas) 
   ———————————————————————————–
   8、直接从TWebBrowser得到网页源码及Html 
     下面先介绍一种极其简单的得到TWebBrowser正在访问的网页源码的方法。一般方法是利用TWebBrowser控件中的Document对象提供的IPersistStreamInit接口来实现,具体就是:先检查WebBrowser.Document对象是否有效,无效则退出;然后取得IPersistStreamInit接口,接着取得HTML源码的大小,分配全局堆内存块,建立流,再将HTML文本写到流中。程序虽然不算复杂,但是有更简单的方法,所以实现代码不再给出。其实基本上所有IE的功能TWebBrowser都应该有较为简单的方法来实现,获取网页源码也是一样。下面的代码将网页源码显示在Memo1中。 
      Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterHtml); 
     同时,在用TWebBrowser浏览HTML文件的时候要将其保存为文本文件就很简单了,不需要任何的语法解析工具,因为TWebBrowser也完成了,如下: 
      Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterText); 
   ———————————————————————————–
   9、“查找”功能 
     查找对话框可以在文档获得焦点的时候通过按键Ctrl-F来调出,程序中则调用IOleCommandTarget对象的成员函数Exec执行OLECMDID_FIND操作来调用,下面给出的方法是如何在程序中用代码来做出文字选择,即你可以自己设计查找对话框。 
      var 
       Doc: IHtmlDocument2; 
       TxtRange: IHtmlTxtRange; 
      begin 
       Doc :=WebBrowser1.Document as IHtmlDocument2; 
       Doc.SelectAll;    //此处为简写,选择全部文档的方法请参见第三条命令操作 
                   //这句话尤为重要,因为IHtmlTxtRange对象的方法能够操作的前提是 
                   //Document已经有一个文字选择区域。由于接着执行下面的语句,所以不会 
                   //看到文档全选的过程。 
       TxtRange :=Doc.Selection.CreateRange as IHtmlTxtRange; 
       TxtRange.FindText(’Text to be searched’,0.0); 
       TxtRange.Select; 
      end; 
     还有,从Txt.Get_text可以得到当前选中的文字内容,某些时候是有用的。 
   ———————————————————————————–
   10、提取网页中所有链接 
     这个方法来自大富翁论坛hopfield朋友的对一个问题的回答,我本想自己试验,但总是没成功。 
     var 
      doc:IHTMLDocument2; 
      all:IHTMLElementCollection; 
      len,i:integer; 
      item:OleVariant; 
     begin 
      doc:=WebBrowser1 .Document as IHTMLDocument2; 
      all:=doc.Get_links;             //doc.Links亦可 
      len:=all.length; 
      for i:=0 to len-1 do begin 
       item:=all.item(i,varempty);        //EmpryParam亦可 
       memo1.lines.add(item.href); 
      end; 
     end; 
   ———————————————————————————–
   11、设置TWebBrowser的编码 
     为什么我总是错过很多机会?其实早就该想到的,但是一念之差,便即天壤之别。当时我要是肯再多考虑一下,多试验一下,这就不会排到第11条了。下面给出一个函数,搞定,难以想象的简单。 
     procedure SetCharSet(AWebBrowser: TWebBrowser; ACharSet: 
   String); 
     var 
      RefreshLevel: OleVariant; 
     Begin 
      IHTMLDocument2(AWebBrowser.Document).Set_CharSet(ACharSet); 
      RefreshLevel :=7;              //这个7应该从注册表来,帮助有Bug。 
      AWebBrowser.Refresh2(RefreshLevel); 
     End;