AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

TStringGrid: über mehrere Zellen schreiben

Ein Thema von hoika · begonnen am 14. Sep 2009 · letzter Beitrag vom 15. Sep 2009
 
Keldorn

Registriert seit: 6. Mär 2003
Ort: Meißen
876 Beiträge
 
Delphi 10.1 Berlin Professional
 
#7

Re: TStringGrid: über mehrere Zellen schreiben

  Alt 15. Sep 2009, 09:36
dein code geht aber nicht richtig, wenn Du colsizing aktiv hast.
auf die Schnelle würde ich das ändern in:

- die verbundene Zelle im Grid selber werden leider erst immer beim 2. Klick richtig gezeichnet.
Delphi-Quellcode:
//Auslagern
function IsCellMerged(Point: TPoint; Rect: TRect): Boolean;
begin
  Inc(Rect.Right);
  Inc(Rect.Bottom);
  Result := PtInRect(Rect, Point);
end;

procedure TForm3.Grid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);

var
  Index : Integer;
  I : Integer;
  LargeRect : TRect;
  CellSpan : TRect;
  Grid : TStringGrid;
begin

  Grid:= Sender as TStringGrid;
// Grid.DefaultDrawing:= False; //im Formcreate

  for I := 0 to Length(FMergedCells) - 1 do
  begin
    CellSpan := FMergedCells[I];

    if IsCellMerged(Point(ACol, ARow), CellSpan) then
    begin
      // Calculate rect of whole merged cell
      //--->> probelm ist hier, wenn Du scrollen mußt hast Du hier ein Problem
      LargeRect := Classes.Rect(
        Grid.CellRect(CellSpan.Left, CellSpan.Top).TopLeft,
        Grid.CellRect(CellSpan.Right, CellSpan.Bottom).BottomRight);

      Inc(Rect.Right, Grid.GridLineWidth);
      Inc(Rect.Bottom, Grid.GridLineWidth);

      Index := SaveDC(Grid.Canvas.Handle);
      try
        Grid.Canvas.Refresh;

        IntersectClipRect(Grid.Canvas.Handle, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);

        // Draw the merged cell, but only the part that is supposed to be drawn
        // by this particular event call.

        if Arow<Grid.fixedrows then //nur für fixierte rows
          begin
            //uses extctrls, graphutil
            Grid.Canvas.Font.color:=clWindowText;
            Frame3D(Grid.Canvas,
                    LargeRect,
                    GetHighLightColor(Grid.FixedColor),
                    GetShadowColor(Grid.FixedColor),
                    1);
          end
         else
          begin
// if ((IsCellMerged(Point(acol,arow),Cellspan)) and (IsCellMerged(Point(Grid.col,Grid.row),Cellspan))) or (gdSelected in State) then
            //ist die aktuelle Zelle selektiert im Cellspan?
            if (IsCellMerged(Point(Grid.col,Grid.row),Cellspan)) then
              begin
                Grid.canvas.Brush.color:=clHighlight;
                Grid.canvas.Font.color :=clHighlightText;
              end
             else
              begin
                Grid.canvas.Brush.color:=Grid.color;
                Grid.canvas.Font.color :=clWindowText;
              end;
            Grid.Canvas.FillRect(LargeRect);
          end;

        Grid.Canvas.TextRect(LargeRect, LargeRect.Left + 2, LargeRect.Top + 2,
                             Grid.Cells[FMergedCells[I].Left, FMergedCells[I].Top]);

        if (IsCellMerged(Point(Grid.col,Grid.row),Cellspan)) then
          Grid.canvas.DrawFocusRect(LargeRect);


      finally
        RestoreDC(Grid.Canvas.Handle, Index);
        Grid.Canvas.Refresh;
      end;

      Exit;
    end;
  end;

  //sonst Zellen normal zeichnen
  if (acol<Grid.fixedcols) or (arow<Grid.fixedrows) then
    begin
      Grid.canvas.Brush.color:=grid.FixedColor;
      Grid.Canvas.FillRect(Rect);
      Frame3D(Grid.Canvas,
              Rect,
              GetHighLightColor(Grid.FixedColor),
              GetShadowColor(Grid.FixedColor),
              1);
      Grid.Canvas.TextRect(Rect,rect.Left+2,rect.Top+2,Grid.Cells[acol,arow]);
    end
   else
    begin
      if gdSelected in State then
        begin
          Grid.canvas.Brush.color:=clHighlight;
          Grid.canvas.Font.color :=clHighlightText;
        end
       else
        begin
          Grid.canvas.Brush.color:=grid.color;
          Grid.canvas.Font.color :=clWindowText;
        end;
      Grid.Canvas.FillRect(Rect);
      Grid.Canvas.TextRect(Rect,rect.Left+2,rect.Top+2,Grid.Cells[acol,arow]);
    end;
end;

procedure TForm3.Grid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
Var CellSpan : TRect;
    i:integer;
    Grid:TStringGrid;
begin
  Grid:= Sender as TStringGrid;

  for I := 0 to Length(FMergedCells) - 1 do
  begin
    CellSpan := FMergedCells[I];
    if IsCellMerged(point(Grid.col,Grid.row),CellSpan) or
       IsCellMerged(point(acol,arow),CellSpan) then
      begin
        //besser wäre hier nicht das ganze Grid neuzuzeichnen, sondern nur die Cellspan
        grid.Repaint;
        exit;
      end;
    end;
end;
Probleme sind immer z.B.:
- wenn das Grid zu klein ist Scrollen
- Cursortasten, wenn die verbundenen Zellen keine fixierten sind

das Frame3d ist doch ganz einfach, mit gethighlight und getshadowcolor hast auch gleich die richtigen Farben, wenns mal nicht clbtnface die fixedcolor ist.

Gruß Frank
Angehängte Grafiken
Dateityp: jpg colspan_106.jpg (44,0 KB, 39x aufgerufen)

Lükes Grundlage der Programmierung:
Es wird nicht funktionieren
(Murphy)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:45 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