比如在保存按钮的onclick事件里: for rownum:=1 to StringGrid1.rows do for colnum:=0 to StringGrid1.cols-1 do begin showmessage(inttostr(StringGrid1.canvas.brush.color)); end;不行吧????取不出来
不好意思,我不知道哦我已经叫其它人来看了 不过 for rownum:=1 to StringGrid1.rows do for colnum:=0 to StringGrid1.cols-1 do begin showmessage(inttostr(StringGrid1.canvas.brush.color)); end; 肯定是不行的,因为rows和cols不过是个tstrings的数组罢了
大地说的这句:实际上每个CELL都是画布,完全可以对它设置颜色 有点问题他们其实是一个画布,只是根据cell的rect来画的,看vcl源码可以看出procedure TStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); begin if DefaultDrawing then Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol, ARow]); inherited DrawCell(ACol, ARow, ARect, AState); end;procedure TCustomDrawGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); var Hold: Integer; begin if Assigned(FOnDrawCell) then begin if UseRightToLeftAlignment then begin ARect.Left := ClientWidth - ARect.Left; ARect.Right := ClientWidth - ARect.Right; Hold := ARect.Left; ARect.Left := ARect.Right; ARect.Right := Hold; ChangeGridOrientation(False); end; FOnDrawCell(Self, ACol, ARow, ARect, AState); if UseRightToLeftAlignment then ChangeGridOrientation(True); end; end;鼠标点击的话,可以通过屏幕取色等方法来实现,但是你肯定不是要的这个吧? 我想你是想通过遍历cell的来取得每个cell的颜色, 回去试了一下,用这种方法可以实现,但是感觉效果不太好 贴出来,给大家参考一下,看看大家还有没有更好的方法procedure TForm1.BitBtn1Click(Sender: TObject); var i,j:integer; begin with StringGrid1 do begin for i:= 0 to RowCount-1 do for j:= 0 to ColCount-1 do Cells[i,j]:= ColorToString(canvas.Pixels[CellRect(i,j).Left+5,CellRect(i,j).Top+5]) end; end;嘿嘿,说白了就是取象素点的颜色
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids;type TAddData = class rest: boolean; end; TForm1 = class(TForm) Grid: TStringGrid; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure GridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure GridDblClick(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var yy, mm, dd: integer; n: TAddData; leap: boolean; i, j ,count: integer; begin if not TryStrToInt(edit1.Text, yy) then exit; if not TryStrToInt(edit2.Text, mm) then exit; if isLeapyear(yy) then leap:= true; case mm of 1,3,5,7,8,10,12: dd:= 31; 2: if Leap then dd:= 29 else dd:= 28; 4,6,9,11: dd:= 30; end; Count:= 1; For i:= 1 to Grid.RowCount - 1 do for j:= 1 to Grid.ColCount - 1 do begin Grid.Cells[j, i]:= IntToStr(Count); case DayOfWeek(EncodeDate(yy, mm, count)) of 7, 6: TAddData(Grid.Objects[j, i]).rest:= true; else TAddData(Grid.Objects[j, i]).rest:= false; end; inc(Count); if Count > dd then exit; end; end;procedure TForm1.FormCreate(Sender: TObject); var n: TAddData; i, j: integer; begin for i:= 0 to Grid.RowCount - 1 do for j:= 0 to Grid.ColCount - 1 do begin n:= TAddData.Create; n.rest:= false; Grid.Objects[j, i]:= n; end; end;procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if (aCol = 0) or (aRow = 0) then exit; if not TaddData(Grid.Objects[aCol, aRow]).Rest then Grid.canvas.Brush.Color:= clWhite else Grid.Canvas.Brush.Color:= clBlue; Grid.Canvas.FillRect(rect); DrawText(Grid.Canvas.Handle, Pchar(Grid.Cells[aCol, aRow]), Length(Grid.Cells[aCol, aRow]), rect, 0); end;procedure TForm1.GridDblClick(Sender: TObject); begin TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest:= not TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest; Grid.Repaint; end;procedure TForm1.Button2Click(Sender: TObject); begin if TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest then Showmessage('休息') else Showmessage('不休息'); end;end.
靠,这错了 procedure TForm1.Button1Click(Sender: TObject); var yy, mm, dd: integer; n: TAddData; leap: boolean; i, j ,count: integer; begin if not TryStrToInt(edit1.Text, yy) then exit; if not TryStrToInt(edit2.Text, mm) then exit; if isLeapyear(yy) then leap:= true; case mm of 1,3,5,7,8,10,12: dd:= 31; 2: if Leap then dd:= 29 else dd:= 28; 4,6,9,11: dd:= 30; end; Count:= 1; For i:= 1 to Grid.RowCount - 1 do for j:= 1 to Grid.ColCount - 1 do begin Grid.Cells[j, i]:= IntToStr(Count); case DayOfWeek(EncodeDate(yy, mm, count)) of 1, 7: TAddData(Grid.Objects[j, i]).rest:= true;//这里 else TAddData(Grid.Objects[j, i]).rest:= false; end; inc(Count); if Count > dd then exit; end; end;
要发 mail我:[email protected]
red,green,blue:byte;
begin Red:=Byte(StringGrid1.Canvas.Brush.Color);
Green:=Byte(rgb shr 8);
Blue:=Byte(rgb shr 16);
end;
,并且按照日,一,二,三,四,五,六星期对应排列,其中周日和周六是默认得节假日(蓝色,工作日用白色)
,当我点击工作日的时候,工作日变成蓝色,等设置完12个月的节假日后,点保存,存入数据库中。
我要取出单元格的颜色,就是为了么取得此单元格的内容。
http://expert.csdn.net/Expert/topic/2455/2455776.xml?temp=.989361
各位路过的老大也来看看,400分。
dx系列的控件应该可以实现
for rownum:=1 to StringGrid1.rows do
for colnum:=0 to StringGrid1.cols-1 do
begin
showmessage(inttostr(StringGrid1.canvas.brush.color));
end;不行吧????取不出来
是无法取得正确的canvas.brush.color
因为只保留最后一个重画的cell的颜色如果你没写重画事件,他就会把颜色设为默认值个人认为,如果不借助外部手段来保存颜色的画,应该是不可能实现的
可以取出最后一次的颜色但是如果想直接读取cell的行列值来去画布的内容,是不行的
因为以前的值已经被新的画布给覆盖了还是用数组吧
不过
for rownum:=1 to StringGrid1.rows do
for colnum:=0 to StringGrid1.cols-1 do
begin
showmessage(inttostr(StringGrid1.canvas.brush.color));
end;
肯定是不行的,因为rows和cols不过是个tstrings的数组罢了
干脆就自己画单元格!至于数据不好保存的情况,
如果不好用数组,
可以考虑用TSTRINGLIST类来实现。!!!!
除非那个颜色你自己早就保存了!!!!我曾经实现几种状态的时候,有四种情况,四种颜色的时候,
我也是自己保存了颜色的。
用TSTRINGLIST类保存,这个类的最好!
你画Grid的时候,必须有颜色的
就是你选择了那个格子的时候,就设置某个变量保存所选择的格子,然后再画上颜色我做的日程记事器就是这样实现了
选择格子-保存所选的位置(日期)-OnDrawCell中处理颜色
通过建立一张表来维护
Pos; Color
1:2;Red
3:5;Blue
这种结构不知道你能否理解我所说的意思
那能不能用如下來取得呢???
MyStringGrid.Objects[SomeCol, SomeRow] := TObject(clRed);
然後再取出呢??
现在的情况是,假如(周六和周日都是蓝色,此颜色是初始化时话上去的)
2002年1月份:
日|一|二|三|四|五|六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
用户选中8号和9号等(设定为节假日)
窗体上有spineYear控件,当它发生onchange事件的时候
比如变成2003年1月份
日|一|二|三|四|五|六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
用户把11号和18号的蓝色去掉了(设定为上班)
我把1年中的12个月都放在1个窗体上了,当用户点击保存按钮,系统要把是篮色的cell中的内容保存进数据库,最简单的方法就是找出是蓝色的cell了,不是吗??我现在的做法是,根据用户选择的人,系统根据人和当前的年,从数据库中找出节假日来,放到一张中间表(临时存储)中,系统根据用户选择的不同颜色(蓝色:节假日;白色:上班)对数据进行增,删操作,等用户点保存的时候,再一起保存进数据库中。
AState: TGridDrawState);
begin
if DefaultDrawing then
Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol, ARow]);
inherited DrawCell(ACol, ARow, ARect, AState);
end;procedure TCustomDrawGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
AState: TGridDrawState);
var
Hold: Integer;
begin
if Assigned(FOnDrawCell) then
begin
if UseRightToLeftAlignment then
begin
ARect.Left := ClientWidth - ARect.Left;
ARect.Right := ClientWidth - ARect.Right;
Hold := ARect.Left;
ARect.Left := ARect.Right;
ARect.Right := Hold;
ChangeGridOrientation(False);
end;
FOnDrawCell(Self, ACol, ARow, ARect, AState);
if UseRightToLeftAlignment then ChangeGridOrientation(True);
end;
end;鼠标点击的话,可以通过屏幕取色等方法来实现,但是你肯定不是要的这个吧?
我想你是想通过遍历cell的来取得每个cell的颜色,
回去试了一下,用这种方法可以实现,但是感觉效果不太好
贴出来,给大家参考一下,看看大家还有没有更好的方法procedure TForm1.BitBtn1Click(Sender: TObject);
var i,j:integer;
begin
with StringGrid1 do
begin
for i:= 0 to RowCount-1 do
for j:= 0 to ColCount-1 do
Cells[i,j]:=
ColorToString(canvas.Pixels[CellRect(i,j).Left+5,CellRect(i,j).Top+5])
end;
end;嘿嘿,说白了就是取象素点的颜色
在设置的时候将对应cell的color保存到 StringGrid1.Objects[1,1],
取出时, 再根据检查 StringGrid1.Objects[1,1], 从中取出对应的 color!!!
不过, 看过 myling(阿德) 的讨论, 也有点启发!
ColorToString(canvas.Pixels[CellRect(i,j).Left+2,CellRect(i,j).Top+2])我想应该很少出现这种情况吧?
另外他说的附加结构,
就是 aiirii 说的StringGrid1.Objects[1,1]和我说的数组,还有其它的结构数组太麻烦了! 可以试试aiirii 说的StringGrid1.Objects[1,1]嘿嘿,说实话,StringGrid还真没怎么用过,我都不知道有Objects[1,1]这个属性:)惭愧呀
aiirii,你说的方法,不知你考虑了这种情况没有:用户在1个日期上蓝色点过,后来又点成白色,
还有就是,在stringgrid初始化的时候我就开始对12个月进行初始化,那也要把初始化的周六和周日,都存进Objects里,如何删除呢???:当用户开始选了6月1日为节假日,后来又取消了,你怎么判断是6月1日???。还有就是如果用户设置完了1年的节假日后,再设置下一年的节假日,你的Objects又怎么样存贮???
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;type
TAddData = class
rest: boolean;
end; TForm1 = class(TForm)
Grid: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure GridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure GridDblClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
yy, mm, dd: integer;
n: TAddData;
leap: boolean;
i, j ,count: integer;
begin
if not TryStrToInt(edit1.Text, yy) then exit;
if not TryStrToInt(edit2.Text, mm) then exit; if isLeapyear(yy) then leap:= true;
case mm of
1,3,5,7,8,10,12: dd:= 31;
2: if Leap then dd:= 29 else dd:= 28;
4,6,9,11: dd:= 30;
end;
Count:= 1;
For i:= 1 to Grid.RowCount - 1 do
for j:= 1 to Grid.ColCount - 1 do
begin
Grid.Cells[j, i]:= IntToStr(Count);
case DayOfWeek(EncodeDate(yy, mm, count)) of
7, 6: TAddData(Grid.Objects[j, i]).rest:= true;
else TAddData(Grid.Objects[j, i]).rest:= false;
end;
inc(Count);
if Count > dd then exit;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
n: TAddData;
i, j: integer;
begin
for i:= 0 to Grid.RowCount - 1 do
for j:= 0 to Grid.ColCount - 1 do
begin
n:= TAddData.Create;
n.rest:= false;
Grid.Objects[j, i]:= n;
end;
end;procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (aCol = 0) or (aRow = 0) then exit;
if not TaddData(Grid.Objects[aCol, aRow]).Rest then
Grid.canvas.Brush.Color:= clWhite else
Grid.Canvas.Brush.Color:= clBlue;
Grid.Canvas.FillRect(rect);
DrawText(Grid.Canvas.Handle, Pchar(Grid.Cells[aCol, aRow]), Length(Grid.Cells[aCol, aRow]), rect, 0);
end;procedure TForm1.GridDblClick(Sender: TObject);
begin
TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest:= not TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest;
Grid.Repaint;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if TaddData(Grid.Objects[Grid.Col, Grid.Row]).rest then
Showmessage('休息') else
Showmessage('不休息');
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var
yy, mm, dd: integer;
n: TAddData;
leap: boolean;
i, j ,count: integer;
begin
if not TryStrToInt(edit1.Text, yy) then exit;
if not TryStrToInt(edit2.Text, mm) then exit; if isLeapyear(yy) then leap:= true;
case mm of
1,3,5,7,8,10,12: dd:= 31;
2: if Leap then dd:= 29 else dd:= 28;
4,6,9,11: dd:= 30;
end;
Count:= 1;
For i:= 1 to Grid.RowCount - 1 do
for j:= 1 to Grid.ColCount - 1 do
begin
Grid.Cells[j, i]:= IntToStr(Count);
case DayOfWeek(EncodeDate(yy, mm, count)) of
1, 7: TAddData(Grid.Objects[j, i]).rest:= true;//这里
else TAddData(Grid.Objects[j, i]).rest:= false;
end;
inc(Count);
if Count > dd then exit;
end;
end;
用你的方法,经过改动后,在没有与数据库连接的时候可以达到效果(一个stringgrid)
那如果从数据库取数据,并且一个窗体上12个stringgrid的话,是不是要有
每个stringgrid的Objects[j, i]呢?????
望不吝赐教
在用戶操作時, 有改變的, 就同時設置其Objects[x,y];
然後, 保存時, 先檢查 Objects 是否為 nil, 不是, 取其值, 不是, 用公用 function取其值;不過綜合考慮, 我覺得用 myling(阿德) 的方法應該代碼量比較少, 出錯可能性也很小!!
在诸位的帮助下,我得程序有了很大的进展,但你说:要取color可能是考虑偏了???我就不知其意了,还望告知一二