Q&A

  • 그리드 소스입니다. 왜 않
그리드를 만들어서
속성을 하나 추가했습니다.
Strings 인데요.

속성 작동은 잘 돼는데...
폼에 이 컨트롤을 떨궈놓고
그 Strings 속성값에 값을 넣어주고..
다 잘 되갑니다.

근데 그 Strings 값을 이용하여
DrawItem처리시 값을 참조 못하는데요.

폼 Create시에 별도의 값처리 없이
폼에 디자인시에 지정한 Strings값은
자동으로 셋팅돼지 않나요?

폼이 만들어지기 전에
그리드 먼저 Create하고
그런 후에 폼이 만들어질 때
폼에 있는 각 컨트롤의 속성값을 대입시켜주지 않나요?
OnwnerDraw도 시켜줬는데..

참고로 이 컨트롤은 각 열의 색상값을 전경색,배경색을 집어넣어주면
그 값을 표시하는 컬러지정가능 그리드입니다.
아무리 풀려고 해도 해결이 않돼 올려봅니다.
누가 내 문제좀 해결해줘~~~~요~~~~ㅠ.ㅠ

///////////////////////////////////////////////////
//폼에서 Create시에 참고로 테스팅 한 것입니다.
procedure TfrmDataGrid.FormCreate(Sender: TObject);
begin
//아래 주석 두 줄을 빼면 색상 표시는 됨.
//단 이렇게 하려면 폼에 이 컨트롤을 놓을 때마다
//메모컨트롤 하나를 필요로 하면서 아래 코드 두 줄이 필요함.
//매번 이렇게 해야 하나요??
//Memo1.Lines.Assign(sgData.CellColorFormat);
//sgData.SetCellColorFormat(Memo1.Lines);

ViewDataList; //값을 뿌려주는 함수
end;



/////////////////////////////////////////////
// 그리드 소스
unit OStringGrid;

interface

uses
SysUtils, Classes, Controls, Grids, Windows,Graphics, Dialogs;

type
TCellInfo = record
   X   : integer;
   Y   : integer;
   BC  : TColor;
   FC  : TColor;
end;

TOStringGrid=class(TStringGrid)
   private
     FCellColorFormat : TStrings;

   protected
     function  GetColorInfo(myStr:String): TCellInfo;
     function  SetCellInfos:Boolean;
     procedure GetCellColor(ACol,ARow:Integer; out bColor, fColor: TColor);

     procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;

   public
     FCellInfos       : Array of TCellInfo;
     constructor Create (AOwner: TComponent); override;
     Destructor  Destroy; override;
     procedure   Assign(source:TPersistent); override;
     procedure UnselectAll;

     function  GetCellColorFormat: TStrings;
     procedure SetCellColorFormat(Value:TStrings);

   published
     property CellColorFormat:TStrings read GetCellColorFormat write SetCellColorFormat;
end;

procedure Register;
function InvertColor(Color:TColor; DColor:TColor):TColor;

implementation

function InvertColor(Color:TColor; DColor:TColor):TColor;
begin
RESULT := DColor;
case Color of
   clAqua      : RESULT:=clTeal;
   clBlack     : RESULT:=clWhite;
   clBlue      : RESULT:=clMaroon;
   clDkGray    : RESULT:=clFuchsia;
   clFuchsia   : RESULT:=clDkGray;
//    clGray      : RESULT:=clPurple;
   clGreen     : RESULT:=clRed;
   clLime      : RESULT:=clSilver;//clYellow;
   clLtGray    : RESULT:=clLime      ;
   clMaroon    : RESULT:=clOlive;    //clBlue;
   clNavy      : RESULT:=clNavy;
   clOlive     : RESULT:=clMaroon;//clNavy;
   clPurple    : RESULT:=clGray;
   clRed       : RESULT:=clYellow;//clGreen;
//    clSilver    : RESULT:=clLtGray;
   clTeal      : RESULT:=clAqua;
   clWhite     : RESULT:=clBlack;
   clYellow    : RESULT:=clRed;//clLime;
end;
end;

procedure Register;
begin
RegisterComponents('Sample', [TOStringGrid]);
end;


{ TOStringGrid }
constructor TOStringGrid.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
if not Assigned(FCellColorFormat) then
   FCellColorFormat := TStringList.Create;
end;

Destructor  TOStringGrid.Destroy;
begin
FCellColorFormat.Free;
inherited Destroy;
end;

procedure   TOStringGrid.Assign(source:TPersistent);
begin
if source is TOStringGrid then begin
   if Assigned(TOStringGrid(source).FCellColorFormat) then
      FCellColorFormat.Assign(TOStringGrid(source).FcellColorFormat);
   inherited Assign(source);
end;//if
end;

{CellColorFormat Property function}
function TOStringGrid.GetCellColorFormat: TStrings;
begin
RESULT := FCellColorFormat;
end;

{CellColorFormat Property function}
procedure TOStringGrid.SetCellColorFormat(Value:TStrings);
begin
//FCellColorFormat := Value;
if Assigned(Value) then
   FCellColorFormat.Assign(Value);

//해당 구조체를 변환한다.
SetCellInfos;
Repaint;
end;

//주어진 라인의 컬러정보를 읽어서 파싱함
function TOStringGrid.GetColorInfo(myStr:String):TCellInfo;
Var
i     : Integer;
j     : Integer;
dcStr : String;
onStr : String;
ColCnt: Integer;
getVal: Integer;
toI   : Integer;
fromI : Integer;
FCellInfo : TCellInfo;
begin
FCellInfo.X := -1;
FCellInfo.Y := -1;
FCellInfo.BC:= clBlack;
FCellInfo.FC:= clBlack;

myStr := Trim(myStr);
j     := Length(myStr);

ColCnt:= 1;
fromI := 1;

for i:=1 to J do begin
   onStr := Copy(myStr,i,1);
   if ((onStr=',') Or (i=J)) then begin

     if(i=J) then toI := i
     else         toI := i-1;

     dcStr := Copy(myStr,fromI,(toI-FromI+1));
     getVal:= -1;
     if(ColCnt>2) then dcStr := '$'+dcStr;
     if((ColCnt>=1) And (ColCnt<=4)) then
       getVal := StrToInt(dcStr);
     if(ColCnt=1) then FCellInfo.X  := getVal
     else if(ColCnt=2) then FCellInfo.Y  := getVal
     else if(ColCnt=3) then FCellInfo.BC := getVal
     else if(ColCnt=4) then FCellInfo.FC := getVal;

     Inc(ColCnt);
     fromI := i+1;
   end;//if
end;//for

RESULT:=FCellInfo;
end;


function  TOStringGrid.SetCellInfos:Boolean;
var
i     : integer;
begin
result:=True;
//크기조정하기    
try
   SetLength(FCellInfos,FCellColorFormat.Count);
except
   result:=False;
end;
if result=False then exit;

for i:=0 to FCellColorFormat.Count-1 do begin
   FCellInfos[i]:=GetColorInfo(FCellColorFormat.Strings[i]);
end;
end;

//주어진 컬륨의 색정보 구하기
procedure TOStringGrid.GetCellColor(ACol,ARow : integer; out bColor, fColor: TColor);
var
I : Integer;
begin

//default color
if ((ACol<=(FixedCols-1)) or (ARow<=FixedRows-1)) then
   bColor := FixedColor
else
   bColor := Color;
fColor := Font.Color;

//define color
for I:=0 to High(FCellInfos) do begin
   if ((FCellInfos[I].X = ACol) and (FCellInfos[I].Y = ARow)) then begin
     bColor := FCellInfos[I].BC;
     fColor := FCellInfos[I].FC;
     break;
   end;//if

   //8,-1
   if ((FCellInfos[I].X = ACol) and (FCellInfos[I].Y<0)) then begin
     bColor := FCellInfos[I].BC;
     fColor := FCellInfos[I].FC;
     break;
   end;//if

   //8,-1
   if ((FCellInfos[I].X<0) and (FCellInfos[I].Y=ARow)) then begin
     bColor := FCellInfos[I].BC;
     fColor := FCellInfos[I].FC;
     break;
   end;//if

end;//for
end;//procedure

//>Created at 2004-06-14 by Myung Seok Oh
procedure TOStringGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
AState: TGridDrawState);
var
bColor, fColor : TColor;
begin
inherited;
GetCellColor(ACol,ARow,bColor,fColor);
With Canvas Do Begin
   if (gdSelected in AState) then begin  // wenn selektiert -> INVERTIEREN
      Brush.Color := InvertColor(bColor,clBlack);
      Font.Color  := InvertColor(fColor,clWhite);
   end else begin // Ansonsten nicht !
      Brush.Color := bColor;
      Font.Color  := fColor;
   end;
   Brush.Style := bsSolid;
   FillRect( ARect );//BackGround
   TextRect( ARect, ARect.left+2, ARect.top+2, Cells[ ACol, ARow ] );
End;
end;

//>Created at 09-Jul-2002 (11:08:35 ) by benjamin wittfoth
procedure TOStringGrid.UnselectAll;
var
ARect:TGridRect;
begin
ARect.Left:=0;ARect.Top:=0;ARect.Right:=0;ARect.Bottom:=0;
Selection:=ARect;
end;


end.
0  COMMENTS