如何得到 MS WORD里插入符号的位置?下面的这段代码能获得其他程序中插入符的位置,但在MS word中不行unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure myWM_ACTIVATE(var Message: TMessage); message WM_ACTIVATE;
    procedure Button1Click(Sender: TObject);//截获消息
  private
    { Private declarations }
  public
    { Public declarations }
     HActWin,Hself: HWND;
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.myWM_ACTIVATE(var Message: TMessage);
begin
  if Message.wParam = WA_CLICKACTIVE  then
  begin
    //SetActiveWindow(message.lParam);
    if (HActWin<>message.lParam) and (message.lParam<>Hself) then
      HActWin:=message.lParam;
  end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
  focushld,windowhld:hwnd;
  threadld:dword;
  P1:TPoint;
begin  SetActiveWindow(HActWin);
  windowhld:=GetForegroundWindow;
  threadld:=GetWindowThreadProcessId(Windowhld,nil); //Windowhld
  AttachThreadInput(GetCurrentThreadId,threadld,true);
  GetCaretPos(P1);
  Caption:=IntToStr(P1.X )+'::'+IntToStr(P1.y ) ;
  Focushld:=getfocus;
  AttachThreadInput(GetCurrentThreadId,threadld,false); if focushld = 0 then Exit;
end;end.

解决方案 »

  1.   

    我这有个函数可以知道标签的起始位置:
    function TCheckSyntax.GlobalSearch(Tag: string; wdWrap: Integer; var PosTag: Integer ): Boolean;
    var
      FindText, matchcase, matchwholeword, matchwildcards, matchsoundslike,
      matchallwordforms, goforward, wrap, format, replacewith, replace: olevariant;
    begin
      Result := false;
      PosTag := 0;
      FindText := Tag;
      replacewith := tag;
      matchcase := false;            //如果为 True,区分大小写
      matchwholeword := false;       //如果为 True,则只查找匹配的完整单词,而并非作为一个长单词的一部分的文字
      matchwildcards := True;        //如果为 True,则查找的文字包含特殊搜索操作符。
      matchsoundslike := false;      //如果为 True,则查找与待查找文字发音相近的单词
      matchallwordforms := false;    //如果为 True,则查找文字的所有形式(例如," sit” 将包含“sitting”和“sat”)
      goforward := true;             //如果为 True,则向下(向文档尾部)搜索。
      format := true;                //如果为 True,则查找格式而非文字。
      replace := false;              //如果为 True,指定执行替换的个数
      wrap := wdWrap;                //其中wdWrap为0时,到达搜索范围的开始或者结尾时,停止执行查找操作
                                     //其中wdWrap为1时,到达搜索区域的开始或者结尾时,继续执行查找操作。
      //如果存在则返回True
      if MSWordAppl.Selection.find.execute( findtext, matchcase, matchwholeword, matchwildcards,
                                            matchsoundslike,matchallwordforms, goforward, wrap,
                                            format, replacewith, replace ) then
      begin
        PosTag := MSWordAppl.Selection.Range.Start; //起始位置
        Result := True;
      end;
    end;不知对你有没有用!
      

  2.   

    将WORD文档的窗口句柄定义为HDC
    看看有没有影响
    因为文档的窗口句柄不同于其他的组件窗口句柄
      

  3.   

    我将WORD文档的窗口句柄定义为HDC,还是不行