TIP: Click on subject to list as thread! ANSI
echo: delphi
to: BAYKAN TOKMAKCIOGLU
from: KEN PETERSEN
date: 1997-11-03 17:11:00
subject: Cursor Position in TMemo

Hello Baykan!
Wednesday October 08 1997 17:21, Baykan Tokmakcioglu wrote to All:
 BT> I'm new in Delphi and currently using Delphi 1.0. My problem is i can
 BT> not get the cursor position in TMemo (I think it called 'caret' in
 BT> Windows). most editors (including delphi's editor) shows cursor (or
Some time ago, this code was posted in a local delphi conference:
type
  TMyMemo = class(TMemo)
  public
    function GetCurrentLine : Integer;
    function GetCurrentPosition : Integer;
    function GetTopLine : Integer;
    function GetMaxLinesVisible : Integer;
    function GetLinesVisible : Integer;
    procedure SetCurrentLine(Value: integer);
    procedure SetCurrentPosition(Value: integer);
    procedure SetTopLine(Value: integer);
  end; // class
implementation
function TMyMemo.GetCurrentLine: integer;
{Get line number containing caret}
begin
  result := SendMessage(Handle, EM_LINEFROMCHAR, SelStart, 0);
end;
function TMyMemo.GetCurrentPosition: integer;
{Get character position of caret within line}
begin
  result := SelStart - SendMessage(Handle, EM_LINEINDEX, (SendMessage(Handle,
EM_LINEFROMCHAR, SelStart, 0)), 0);
end;
function TMyMemo.GetTopLine: integer;
{Get number of topmost visible line}
begin
  result := SendMessage(Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
end;
function TMyMemo.GetMaxLinesVisible: integer;
{Returns number of lines that can be shown in the current font}
var
   hndl: HDC;
   tm: TTextMetric;
begin
  if Visible then begin
    hndl := GetDC(Handle);
    SelectObject(hndl, Font.Handle);
    GetTextMetrics(hndl, tm);
    ReleaseDC(Handle, hndl);
    result := ClientHeight div tm.tmHeight;
  end
else
   result := -1;
end;
function TMyMemo.GetLinesVisible: integer;
{Return actual number of lines visible}
var
   c: integer;
   n: integer;
begin
if Visible then
   begin
   n := GetMaxLinesVisible;
   {truncate value to actual number of lines visible if necessary}
   c := Lines.Count - SendMessage(Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
   if c < n then
      result := c
   else
      result := n;
   end
else
   result := 0;
end;
procedure TMyMemo.SetCurrentLine(Value: integer);
{Put caret on start of selected line}
var
   cl: integer;
begin
  cl := Value;
  {Restrict range to available lines}
  if cl < 0 then cl := 0;
  if cl > Lines.Count - 1 then cl := Lines.Count - 1;
  SelLength := 0;
  SelStart := SendMessage(Handle, EM_LINEINDEX, cl, 0);
end;
procedure TMyMemo.SetCurrentPosition(Value: integer);
var
   cl: integer;
   cp: integer;
begin
  {Value must be within range}
  cl := GetCurrentLine;
  cp := Value;
  if cp < 0 then cp := 0;
  if (cp > Length(Lines[cl])) then cp := Length(Lines[cl]);
  {Put caret in selected position}
  SelLength := 0;
  SelStart := SendMessage(Handle, EM_LINEINDEX, cl, 0) + cp;
end;
procedure TMyMemo.SetTopLine(Value: integer);
{Put selected line at top of memo}
var
   tl: integer;
begin
  tl := Value;
  if tl < 0 then tl := 0;
  if tl > Lines.Count - 1 then tl := Lines.Count - 1;
  SendMessage(Handle, EM_LINESCROLL, 0, tl - SendMessage(Handle,
EM_GETFIRSTVISIBLELINE, 0, 0));
end;
Ken
... Public speaking is very easy.
--- GoldED/386 3.00.Alpha4+
---------------
* Origin: Living in interesting times (2:238/31)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.