StringGrid - formatowanie komórek

jozkan

Przykład formatowania komórek StringGrida. TSetup_SG opisuje wszystkie komórki. Procedura WysokoscStringGridaDostosujDoWidocznychCalkiemWierszy jest zastosowana ze względów czysto estetycznych. Procedurę FormatujSG należy umieścić w OnDrawCell StringGrida


type

     Twyrownanie_w_komorce=  (leftK,rightK,topK,bottomK,h_centerK,v_centerK);

     Tsetup_komorki=record
                    fontSG_name:string;
                    fontSG_size:Integer; //lub tylko fontSG:TFont
                    fontSG_style:TFontStyles;
                    fontSG_color:TColor;
                    brush_colorSG:TColor;
                    zawijaj_wierszeSG:Boolean;
                    margLSG:Integer;//piks
                    margRSG:Integer;
                    margTSG:Integer;
                    margBSG:Integer;
                    widthSG:Integer;
                    heightSG:Integer;
                    wyrownanie_w_komorceSG: set of Twyrownanie_w_komorce;
                    end;

     Tsetup_SG = array of array of Tsetup_komorki;

procedure WysokoscStringGridaDostosujDoWidocznychCalkiemWierszy(SG:TStringGrid);
var
   w,vis_rows1,vis_rows2,sg_height,vis_height,rows_height,delta_height,scoll_height:Integer;
begin
   vis_rows1:=SG.VisibleRowCount;
   if vis_rows1 > 1 then
   begin
      sg_height:=SG.Height;
      scoll_height:=SG.Height-SG.ClientHeight;

      vis_height:=0;
      for w:=0 to SG.VisibleRowCount do
      vis_height:=vis_height+SG.RowHeights[w];

      delta_height:=SG.Height-scoll_height-vis_height;
      if delta_height > 0 then
      begin
         repeat
            SG.Height:=SG.Height-1;
            if sg_height-SG.Height > 40 then
            begin
               SG.Height:=sg_height;
               vis_rows2:=vis_rows1;
               Break;
            end;
            vis_rows2:=SG.VisibleRowCount;
         until vis_rows2<>vis_rows1;
         if vis_rows2<>vis_rows1 then
         SG.Height:=SG.Height+1+SG.GridLineWidth;
      end;
   end;
end;

procedure FormatujSG(SG:TStringGrid;xCol,xRow:Integer; rect_komorki:TRect;XState:TGridDrawState;
                     var setup_SG:Tsetup_SG);
var
   wys,wyrownanie: Integer;
begin
   wyrownanie:=Dt_Noprefix;

   with SG.Canvas,setup_SG[xCol][xRow] do
   begin
      if zawijaj_wierszeSG then
      begin
         if wyrownanie_w_komorceSG=[leftK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_Left or Dt_WordBreak
         else
         if wyrownanie_w_komorceSG=[h_centerK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_Center or Dt_WordBreak
         else
         if wyrownanie_w_komorceSG=[rightK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_Right or Dt_WordBreak;
      end
      else
      if not zawijaj_wierszeSG then
      begin
         if wyrownanie_w_komorceSG=[leftK,topK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Left or Dt_Top
         else
         if wyrownanie_w_komorceSG=[leftK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Left or Dt_VCenter
         else
         if wyrownanie_w_komorceSG=[leftK,bottomK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Left or Dt_Bottom

         else
         if wyrownanie_w_komorceSG=[h_centerK,topK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Center or Dt_Top
         else
         if wyrownanie_w_komorceSG=[h_centerK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Center or Dt_VCenter
         else
         if wyrownanie_w_komorceSG=[h_centerK,bottomK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Center or Dt_Bottom

         else
         if wyrownanie_w_komorceSG=[rightK,topK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Right or Dt_Top
         else
         if wyrownanie_w_komorceSG=[rightK,v_centerK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Right or Dt_VCenter
         else
         if wyrownanie_w_komorceSG=[rightK,bottomK] then
         wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Right or Dt_Bottom;
      end
      else
      wyrownanie:=wyrownanie or Dt_SingleLine or Dt_Left or Dt_Top;

      if gdSelected in XState then
      Brush.Color:=$00F9F0A2
      else
      Brush.Color:=brush_colorSG;
      FillRect(rect_komorki);

      rect_komorki.Left:=rect_komorki.Left+margLSG;
      rect_komorki.Top:=rect_komorki.Top+margTSG;
      rect_komorki.Right:=rect_komorki.Right-margRSG;
      rect_komorki.Bottom:=rect_komorki.Bottom-margBSG;

      Font.Name:=fontSG_name;
      Font.Size:=fontSG_size;
      Font.Style:=fontSG_style;
      Font.Color:=fontSG_color;

      if TextHeight('My')+margTSG+margBSG > heightSG then
      begin
         heightSG:=TextHeight('My')+margTSG+margBSG;
         SG.RowHeights[XRow]:=heightSG;
         WysokoscStringGridaDostosujDoWidocznychCalkiemWierszy(SG);
      end;

      if zawijaj_wierszeSG then
      begin
         wys:=DrawText(Handle,PChar(SG.Cells[xCol,xRow]),-1,rect_komorki,wyrownanie or DT_CALCRECT);
         if wys+margTSG+margBSG > heightSG then
         begin
            heightSG:=wys+margTSG+margBSG;
            SG.RowHeights[XRow]:=heightSG;
            WysokoscStringGridaDostosujDoWidocznychCalkiemWierszy(SG);
         end;
      end;
      DrawText(Handle,PChar(SG.Cells[XCol,XRow]),-1,rect_komorki,wyrownanie);
   end;
end;




FAQ

2 komentarzy

SG - obiekt do formatowania
xCol,xRow - kolumna, wiersz (adres komórk do formatowaniai)
rect_komork - 'rect_komorki'
XState - kombinajca zbioru: gdSelected, gdFocused, gdFixed
setup_SG - struktura z informacją jak sformatować

UWAGA:
JEST if vis_rows1>1 then MA BYC if vis_rows1 > 1 then
zreszta wszedzie gdzie jest > (znaczek and gt srednik) zamienic trzeba na > (znak wiekszosci)

Moze ktos podac jak wywolywac te procedury? Co podac jako parametry procedury FormatujSG.