Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Komponente TMemoEx - ein TMemo mit Zeilennummern (https://www.delphipraxis.net/56014-komponente-tmemoex-ein-tmemo-mit-zeilennummern.html)

turboPASCAL 30. Okt 2005 14:30


Komponente TMemoEx - ein TMemo mit Zeilennummern
 
Liste der Anhänge anzeigen (Anzahl: 2)
Irgend wann mal braucht man Zeilennummern in einem Memo. Klar, es gibt SynEdit ist aber manchmal ein bissel gross... Warum nicht mal als kleine Komponente ?

Ja ist doch einfach..., das dachte ich mir heute früh und tippte darauf los. Gar nicht so einfach denke ich jetzt.

Soweit funktioniert es jetzt, jedoch gibt es noch ein Problem. Wenn in einer neuen Zeile ein Zeichen eingeg. wird springt der Zähler auf eine neue Zeile.


Wer kann helfen (Bitte nicht nur "Ich" sagen :wink: )?


Delphi-Quellcode:
unit MemoEx;

{...}

implementation

{...}

function CountVisibleLines(const Memo: TMemo): Integer;
var
  OldFont: HFont;
  DC: THandle;
  TextMetric: TTextMetric;
begin
  Result := 0;

  DC := GetDC(Memo.Handle);
  try
    OldFont := SelectObject(DC, Memo.Font.Handle);
    try
      GetTextMetrics(DC, TextMetric);
      Result := (Memo.ClientRect.Bottom - Memo.ClientRect.Top) div
        (TextMetric.tmHeight + TextMetric.tmExternalLeading);
    finally
      SelectObject(DC, OldFont);
    end;
  finally
    ReleaseDC(Memo.Handle, DC);
  end;
end;

procedure CalcRect(var R: TRect; aScrollBars: TScrollStyle);
begin
  if aScrollBars = ssBoth then
  begin
    R.Right := R.Right - GetSystemMetrics(SM_CYVSCROLL);
    R.Bottom := R.Bottom - GetSystemMetrics(SM_CYHSCROLL);
  end else
  if aScrollBars = ssHorizontal then
  begin
    R.Bottom := R.Bottom - GetSystemMetrics(SM_CYHSCROLL);
  end else
  if aScrollBars = ssVertical then
    R.Right := R.Right - GetSystemMetrics(SM_CYVSCROLL);
end;

procedure TMemoEx.WMPaint(var Msg: TWMPaint);
var
  canv: Tcanvas;
  r: trect;
  i, h, y: Integer;
  FirstVisibleLine,
  VisibleLines: Integer;
begin
  inherited;

  canv := tcanvas.Create;
  canv.Handle := GetWindowDC(Handle);

  r := ClientRect;
  if self.BorderStyle = bsSingle then
  begin
    r.Top := +GetSystemmetrics(SM_CXEDGE);
    r.Left := +GetSystemmetrics(SM_CXEDGE);
    r.Bottom := r.Bottom + GetSystemmetrics(SM_CXEDGE);
  end;

  FirstVisibleLine := SendMessage(self.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  VisibleLines := CountVisibleLines(Self);

  with canv do
  begin
    r.Right := FGutterSize;
    brush.Color := clbtnface;

    if FGutterType = gtLine then
    begin
      brush.Color := clWindow;
      pen.Color := cl3DDkShadow;
      canv.MoveTo(r.Right - 2, r.Top);
      canv.LineTo(r.Right - 2, r.Bottom);
    end else
    begin
      FillRect(r);
      Frame3D(canv, r, clBtnHighlight, clBtnShadow, 1);
    end;

    Font := self.Font;
    Font.Color := clBtnText;

    if self.Lines.Count < VisibleLines then h := self.Lines.Count
    else h := FirstVisibleLine + (VisibleLines - 1);

    y := 0;
    for i := FirstVisibleLine to h do
    begin
      TextOut(r.Left + 8, r.Top + (y * textheight('X')),
        format('%4.4d', [i + 1]));
      inc(y);
    end;
  end;

  canv.Free;
end;

{...}

Lannes 2. Nov 2005 23:27

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
Hallo,

hast Du Dein Problem schon behoben?
Hier eine mögliche Lösung:
Delphi-Quellcode:
//... für diese Zeilen
//if self.Lines.Count < VisibleLines then h := self.Lines.Count
//    else h := FirstVisibleLine + (VisibleLines - 1);
//... das einsetzen
   if self.Lines.Count < VisibleLines then
      begin
      if (Length(self.Text) > 0 ) and
         (Length(self.Text) = self.SelStart) and (self.Text[Length(self.Text)] = #10) then
         h := self.Lines.Count
         else
           h := self.Lines.Count-1;
      end
      else
        begin
        h := FirstVisibleLine + (VisibleLines-1);
        end;
Hoffe das es das ist was Du Dir vorgestellt hast.

Master_RC 2. Nov 2005 23:33

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
danke, sowas hab ich gesucht :)

Ich finde auch, dass e snicht mehr haben sollte :)

Lannes 2. Nov 2005 23:37

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
Hallo,

etwas müsste noch verbessert werden:
Wenn Scrollbars eingeblendet werden/sind
bzw. die Zeilenanzahl das Ende des Memos erreichen wirkt das noch nicht sauber gelöst.
In einem Standard-Memo sieht das besser aus.

Master_RC 2. Nov 2005 23:40

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
---------------------------
Error
---------------------------
Control 'MemoEx1' has no parent window.
---------------------------
OK
---------------------------

turboPASCAL 3. Nov 2005 00:02

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
Delphi-Quellcode:
procedure TMemoEx.Change;
begin
  inherited Change;

  {if OldLineCount <> Lines.Count then
  begin
    OldLineCount := Lines.Count;
    Invalidate;
  end; }
  Invalidate;
end;
@Master_RC bitte in MemoEx.pas die if-Abfrage wie oben ausklammern, dann funktioniert es.


Zitat:

Zitat von Lannes
hast Du Dein Problem schon behoben?

Nein, noch nicht wirklich, ich wollte erst noch das flackern beim eingeben von Zeichen beseitigen...

Master_RC 3. Nov 2005 00:16

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
danke! ;)

//EDIT:
Man sollte noch einstellen können, wie oft sich die Zahlen am Rand aktualisieren

turboPASCAL 3. Nov 2005 00:59

Re: Komponente TMemoEx - ein TMemo mit Zeilennummern
 
Liste der Anhänge anzeigen (Anzahl: 1)
@Master_RC verstehe ich jetzt nicht so recht, wie ist das gemeint ?

----------

Der Fehler ist noch nicht weg, ich komme aber auch nicht dahinter wie er entsteht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:00 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz