Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi StringGrid-Zelle während dem editieren farbig? (https://www.delphipraxis.net/1162-stringgrid-zelle-waehrend-dem-editieren-farbig.html)

Jester 27. Okt 2002 15:46


StringGrid-Zelle während dem editieren farbig?
 
Hallo,

ich habe ein Stringgrid, wo die aktuelle Zelle farbig unterlegt ist. Wenn ich jetzt aber in die Zelle was schreiben will, wird sie weiß.

Hat jemand eine Idee, wie die Zelle farbig bleibt, während ich sie editiere.

Danke schon mal im Vorraus.

sakura 27. Okt 2002 16:50

Ohne weiteres gar nicht. :evil:

Der Editor wird durch die (versteckte) Komponente TInplaceEditor dargestellt. An diese kannst Du nur herankommen, indem Du eine neue Komponente (sowohl vom Grid, als auch vom InplaceEditor) erstellst. Anschließend kannst Du auf die verschiedenen Eigenschaften zugreifen.

Folgender Quellcode ist eine Unit, welche je eine neue Komponente für das StringGrid und für das DrawGrid erstellen. Diese haben je zwei neue Eigenschaften (EditorColor, EditorFontColor). Diese kannst Du nutzen, um Deine Wünsche zu erfüllen.

Unit unter dem Namen ExtStringGrid.pas speichern und über das Menü Komponenten installieren. (Mehr dazu im Tutorial Erstellung von Komponenten)

Code:
[color=#000080][i]{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Unit Name : ExtStringGrid
 * Autor    : Daniel Wischnewski
 * Copyright : Copyright © 2001, 2002 by gate(n)etwork. All Rights Reserved.
 * Urheber  : Daniel Wischnewski
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}[/i][/color]

[b]unit[/b] ExtStringGrid;

[b]interface[/b]

[b]uses[/b]
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

[b]type[/b]
  TExtInplaceEdit = [b]class[/b](TInplaceEdit)
  [b]private[/b]
  [b]protected[/b]
  [b]public[/b]
  [b]published[/b]
  [b]end[/b];

  TExtStringGrid = [b]class[/b](TStringGrid)
  [b]private[/b]
    FEditorColor: TColor;
    FEditorFontColor: TColor;
    [b]procedure[/b] SetEditorColor([b]const[/b] Value: TColor);
    [b]procedure[/b] SetEditorFontColor([b]const[/b] Value: TColor);
    [b]procedure[/b] UpdateEditor(aEditor: TInplaceEdit);
  [b]protected[/b]
    [b]function[/b] CreateEditor: TInplaceEdit; [b]override[/b];
  [b]public[/b]
    [b]constructor[/b] Create(AOwner: TComponent); [b]override[/b];
  [b]published[/b]
    [b]property[/b] EditorColor: TColor [b]read[/b] FEditorColor [b]write[/b] SetEditorColor [b]default[/b] clWindow;
    [b]property[/b] EditorFontColor: TColor [b]read[/b] FEditorFontColor [b]write[/b] SetEditorFontColor [b]default[/b] clWindowText;
  [b]end[/b];

  TExtDrawGrid = [b]class[/b](TDrawGrid)
  [b]private[/b]
    FEditorColor: TColor;
    FEditorFontColor: TColor;
    [b]procedure[/b] SetEditorColor([b]const[/b] Value: TColor);
    [b]procedure[/b] SetEditorFontColor([b]const[/b] Value: TColor);
    [b]procedure[/b] UpdateEditor(aEditor: TInplaceEdit);
  [b]protected[/b]
    [b]function[/b] CreateEditor: TInplaceEdit; [b]override[/b];
  [b]public[/b]
    [b]constructor[/b] Create(AOwner: TComponent); [b]override[/b];
  [b]published[/b]
    [b]property[/b] EditorColor: TColor [b]read[/b] FEditorColor [b]write[/b] SetEditorColor [b]default[/b] clWindow;
    [b]property[/b] EditorFontColor: TColor [b]read[/b] FEditorFontColor [b]write[/b] SetEditorFontColor [b]default[/b] clWindowText;
  [b]end[/b];

[b]procedure[/b] [b]Register[/b];

[b]implementation[/b]

[b]procedure[/b] [b]Register[/b];
[b]begin[/b]
  RegisterComponents([color=#000080]'gate(n)etwork'[/color], [TExtStringGrid, TExtDrawGrid]);
[b]end[/b];

[color=#000080][i]{ TExtStringGrid }[/i][/color]

[b]constructor[/b] TExtStringGrid.Create(AOwner: TComponent);
[b]begin[/b]
  [b]inherited[/b];
  FEditorColor := clWindow;
  FEditorFontColor := clWindowText;
[b]end[/b];

[b]function[/b] TExtStringGrid.CreateEditor: TInplaceEdit;
[b]begin[/b]
[color=#000080][i]//  inherited;[/i][/color]
  Result := TExtInplaceEdit.Create(Self);
  UpdateEditor(Result);
[b]end[/b];

[b]procedure[/b] TExtStringGrid.SetEditorColor([b]const[/b] Value: TColor);
[b]begin[/b]
  FEditorColor := Value;
  UpdateEditor(InplaceEditor);
[b]end[/b];

[b]procedure[/b] TExtStringGrid.SetEditorFontColor([b]const[/b] Value: TColor);
[b]begin[/b]
  FEditorFontColor := Value;
  UpdateEditor(InplaceEditor);
[b]end[/b];

[b]procedure[/b] TExtStringGrid.UpdateEditor(aEditor: TInplaceEdit);
[b]begin[/b]
  [b]if[/b] aEditor <> [b]nil[/b] [b]then[/b]
    [b]if[/b] aEditor [b]is[/b] TExtInplaceEdit [b]then[/b]
      [b]with[/b] TExtInplaceEdit(aEditor) [b]do[/b]
      [b]begin[/b]
        Color := FEditorColor;
        Font.Color := FEditorFontColor;
      [b]end[/b];
[b]end[/b];

[color=#000080][i]{ TExtDrawGrid }[/i][/color]

[b]constructor[/b] TExtDrawGrid.Create(AOwner: TComponent);
[b]begin[/b]
  [b]inherited[/b];
  FEditorColor := clWindow;
  FEditorFontColor := clWindowText;
[b]end[/b];

[b]function[/b] TExtDrawGrid.CreateEditor: TInplaceEdit;
[b]begin[/b]
[color=#000080][i]//  inherited;[/i][/color]
  Result := TExtInplaceEdit.Create(Self);
  UpdateEditor(Result);
[b]end[/b];

[b]procedure[/b] TExtDrawGrid.SetEditorColor([b]const[/b] Value: TColor);
[b]begin[/b]
  FEditorColor := Value;
  UpdateEditor(InplaceEditor);
[b]end[/b];

[b]procedure[/b] TExtDrawGrid.SetEditorFontColor([b]const[/b] Value: TColor);
[b]begin[/b]
  FEditorFontColor := Value;
  UpdateEditor(InplaceEditor);
[b]end[/b];

[b]procedure[/b] TExtDrawGrid.UpdateEditor(aEditor: TInplaceEdit);
[b]begin[/b]
  [b]if[/b] aEditor <> [b]nil[/b] [b]then[/b]
    [b]if[/b] aEditor [b]is[/b] TExtInplaceEdit [b]then[/b]
      [b]with[/b] TExtInplaceEdit(aEditor) [b]do[/b]
      [b]begin[/b]
        Color := FEditorColor;
        Font.Color := FEditorFontColor;
      [b]end[/b];
[b]end[/b];

[b]end[/b].

Jester 27. Okt 2002 17:10

Es funzt, vielen Danek, total genial!!!

MisterEd 15. Jan 2003 14:36

hmm k ich glaube ich habe die komponente erfolgreich installiert nur wie benutze ich die jetzt :?:

lodda 15. Jan 2003 14:43

Was heißt du "glaubst" :?:

sakura 15. Jan 2003 14:54

Wenn die Installation geklappt hat, dann siehst Du in Deiner Komponentenpalette einen neuen Reiter gate(n)etwork. Dort siehst Du die beiden Komponenten. Diese kannst Du jetzt wie ein normales Stringgrid bzw. Drawgrid nutzen. Du hast lediglich zwei zusätzliche Eigenschaften für den Editor (Farbe und Schrift).

Maiky 4. Mär 2008 15:54

Re: StringGrid-Zelle während dem editieren farbig?
 
@sakura
Super und vielen Dank! Genau das habe ich gesucht. Gibt es eventuell noch die Möglichkeit der Textausrichtung im TInplaceEdit?

s-off 4. Mär 2008 16:56

Re: StringGrid-Zelle während dem editieren farbig?
 
Zitat:

Zitat von Maiky
Gibt es eventuell noch die Möglichkeit der Textausrichtung im TInplaceEdit?

Nein,

das machst Du in der DrawCell Methode unter Angabe des Textformates (z.B. DT_CENTER) als Argument an DrawText.

Maiky 4. Mär 2008 17:00

Re: StringGrid-Zelle während dem editieren farbig?
 
Das klappt so leider nicht, damit richte ich bereits meinen Text aus und dies hat keine Auswirkung auf die Textausrichtung beim editieren.

s-off 4. Mär 2008 17:47

Re: StringGrid-Zelle während dem editieren farbig?
 
Ups, da habe ich Dich wohl falsch verstanden.
Dann schau Dir doch einfach mal den InplaceEditor genauer an.

Maiky 4. Mär 2008 18:23

Re: StringGrid-Zelle während dem editieren farbig?
 
Willst mir damit sagen das ich etwas übersehen habe, als ich in die Hilfe von TInplaceEdit schaute?
Falls ja wäre ich froh wenn mich aufklärst was, ansonsten war der Tipp nicht sehr hilfreich in diesem Falle. 8)

Maiky 5. Mär 2008 10:18

Re: StringGrid-Zelle während dem editieren farbig?
 
So ich habe mal probiert es hinzubekommen. Leider erfolglos, vielleicht kann mir wer helfen?
Ich habe in die neue TExtInplaceEdit Klasse die Eigenschaft Alignment hinzugefügt und will diese im StringGrid dann immer setzen.

Momentan ist das Problem das er FEditorAlignment im StringGrid niemals ändert, wenn ich die darüber liegenden if-Bedingungen auskommentiere erhalte ich vom Objektinspector eine Speicherzugriff Verletzung. Wäre toll wenn wer den Fehler sehen würde und verbessern würde. Danke!

Delphi-Quellcode:
  TExtInplaceEdit = class(TInplaceEdit)
  private
     FAlignment: TAlignment;
  protected
     procedure CreateParams(var Params: TCreateParams); override;
     procedure SetAlignment(const Value: TAlignment);
  public
     Constructor Create(aOwner: TComponent); override;
  published
   property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
  end;


  TTeStringGrid = class(TStringGrid)
  private
    FEditorAlignment: TAlignment;
  protected
   procedure SetEditorAlignment(const Value: TAlignment);
  public
  published
    property EditorAlignment: TAlignment read FEditorAlignment write SetEditorAlignment default taLeftJustify;
  end;

{ TExtInplaceEdit ===============================================================}

constructor TExtInplaceEdit.Create(aOwner: TComponent);
begin
  inherited Create(AOwner);
  FAlignment := taLeftJustify;
end;

procedure TExtInplaceEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  //if Multiline then
  //  Params.Style := Params.Style or LongWord(ES_MultiLine);
  case Alignment of
    taLeftJustify:
      Params.Style := Params.Style or LongWord(ES_Left);
    taRightJustify:
      Params.Style := Params.Style or LongWord(ES_Right);
    else
      Params.Style := Params.Style or LongWord(ES_Center);
  end;
 
  //if WordWrap then
  //  Params.Style := Params.Style and (not LongWord(ES_AUTOHSCROLL))
  //else
  //  Params.Style := Params.Style and (not LongWord(0));
end;

procedure TExtInplaceEdit.SetAlignment(const Value: TAlignment);
begin
  if (FAlignment<>Value) then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;

{ TTeStringGrid ===============================================================}

procedure TTeStringGrid.SetEditorAlignment(const Value: TAlignment);
begin
  if (FEditorAlignment<>Value) then
  begin
     if InplaceEditor <> nil then
      if InplaceEditor is TExtInplaceEdit then
      begin
         FEditorAlignment := Value;
         with TExtInplaceEdit(InplaceEditor) do
         begin
            Alignment := FEditorAlignment;
         end;
      end;
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:03 Uhr.

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