我需要的不是光标所在行列数,儿是相对于窗口或者屏幕的坐标值。

解决方案 »

  1.   

    function GetCaretPosition(var APoint: TPoint): Boolean; 
    var w: HWND; 
      aID, mID: DWORD; 
    begin 
      Result:= False; 
      w:= GetForegroundWindow; 
      if w <> 0 then 
      begin 
        aID:= GetWindowThreadProcessId(w, nil); 
        mID:= GetCurrentThreadid; 
        if aID <> mID then 
        begin 
          if AttachThreadInput(mID, aID, True) then 
          begin 
            w:= GetFocus; 
            if w <> 0 then 
            begin 
              Result:= GetCaretPos(APoint); 
              Windows.ClientToScreen(w, APoint); 
            end; 
            AttachThreadInput(mID, aID, False); 
          end; 
        end; 
      end; 
    end; 
    //Small demo: set cursor to active caret position 
    procedure TForm1.Timer1Timer(Sender: TObject); 
    var 
      Pt: TPoint; 
    begin 
      if GetCaretPosition(Pt) then 
      begin 
        ListBox1.Items.Add(Format('Caret position is %d %d', [Pt.x, Pt.y])); 
        SetCursorPos(Pt.X, Pt.Y); 
      end; 
    end; 
      

  2.   

    再多句嘴,如何获得Memo和edit的【光标】不是鼠标的,【坐标】不是行号!
      

  3.   

    你不说清楚,害我费了大工夫。改改上面的函数就行了
    其中Edit1是你要找光标的控件
    function GetCaretPosition(var APoint: TPoint): Boolean; 
    begin
      Result := False;
      if Form1.Edit1.Focused then
      begin  
        Result:= GetCaretPos(APoint); 
        Apoint := Form1.Edit1.ClientToScreen(APoint);
        Result := True; 
      end;
    end;