如何在RichView中获取可见项的底部坐标?

时间:2014-04-09 05:01:38

标签: delphi delphi-7 delphi-2010

我正在开展一个项目,我需要TRichview中最后一个可见项目的像素。

使用' LastVisibleItem' TRichView的属性我能够找到项目Start Cordinate

但问题是我需要一个最后一个可见单词的像素值。

任何人都能告诉我怎么能得到它吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我有点不确定你的LastVisibleItem财产是如何运作的。下面是一个建议的解决方案,以获取最后一个可见字符的右上角坐标。希望它适合你。

//Function GetCharPos source: http://www.delphipages.com/forum/showthread.php?t=33707
function GetCharPos(AOwner : TControl; Index : LongInt) : TPoint;
var
  p : TPoint;
begin
  AOwner.Perform(EM_POSFROMCHAR,WPARAM(@p),Index);
  Result := p;
end;

//Inspired by: http://www.swissdelphicenter.ch/en/showcode.php?id=1213
function GetLastVisibleCharIndex(AOwner : TControl):integer;
var
  r: TRect;
begin
  //The EM_GETRECT message retrieves the formatting rectangle of an edit control.
  AOwner.Perform(EM_GETRECT, 0, Longint(@r));
  r.Right := r.Right - 1;
  r.Bottom := r.Bottom - 2;
  //The EM_CHARFROMPOS message retrieves information about the character closest to a specified point in the client area of an edit control
  result := AOwner.Perform(EM_CHARFROMPOS, 0, Integer(@r.BottomRight));
end;

//Get the Top-Right coordinate of the last visible character
function GetLastVisibleCharPos(AOwner : TControl):TPoint;
var Index : integer;
begin
  index := GetLastVisibleCharIndex(AOwner);
  result := GetCharPos(AOwner, index);
end;

使用示例:

procedure TForm2.Button3Click(Sender: TObject);
var
  p : TPoint;
begin
  p := GetLastVisibleCharPos(RichEdit1);

  DrawCrossHair(p); //Help visualize the point
end;

//Helper proc to draw a cross-hair
procedure TForm2.DrawCrossHair(p : TPoint);
var
  aCanvas: Tcanvas;
  X, Y: Integer;
begin

  aCanvas := TCanvas.Create;
  Y := RichEdit1.Height;
  X := RichEdit1.Width;
  try
    aCanvas.Handle := GetDC(RichEdit1.Handle);
    aCanvas.Font := RichEdit1.Font;

    aCanvas.Pen.color := clGreen; // Color of line

    //Draw vertical line
    aCanvas.MoveTo(p.x, 0);
    aCanvas.LineTo(p.x, Y);

    //Draw horizontal line
    aCanvas.MoveTo(0, p.Y);
    aCanvas.LineTo(x, p.y);
  finally
    ReleaseDC(RichEdit1.Handle, aCanvas.Handle);
    aCanvas.Free;
  end;
end;