如题,很急!!!!

解决方案 »

  1.   

    在ondrawcolumncell中写: 画这个格的时候取得它的颜色:去grid的canvas的brush值就行了
      

  2.   

    呵呵,我有个好控件能随便设置grid的属性,呵呵
    要发 mail我:[email protected]
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    red,green,blue:byte;
    begin  Red:=Byte(StringGrid1.Canvas.Brush.Color);
      Green:=Byte(rgb shr 8);
      Blue:=Byte(rgb shr 16);
    end;
      

  4.   

    取啊,我对stringgrid不太了解。帮不上忙。
      

  5.   

    在我现在做的考勤系统中:节假日设置中,有个界面中放置了12个stringgrid,来显示12个月
    ,并且按照日,一,二,三,四,五,六星期对应排列,其中周日和周六是默认得节假日(蓝色,工作日用白色)
    ,当我点击工作日的时候,工作日变成蓝色,等设置完12个月的节假日后,点保存,存入数据库中。
    我要取出单元格的颜色,就是为了么取得此单元格的内容。
      

  6.   

    没用过,不会,要不你用数组吧,:),帮帮看看我的问题:
    http://expert.csdn.net/Expert/topic/2455/2455776.xml?temp=.989361
    各位路过的老大也来看看,400分。
      

  7.   

    stringgrid用的倒不多
    dx系列的控件应该可以实现
      

  8.   

    没用过,不会,可能是写在procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);里判断canvas的brush的color.
      

  9.   

    delphiseabird(沙鸥) 的办法应可行啊.
      

  10.   

    比如在保存按钮的onclick事件里:
    for rownum:=1 to StringGrid1.rows do
    for colnum:=0 to StringGrid1.cols-1 do
    begin
        showmessage(inttostr(StringGrid1.canvas.brush.color));
    end;不行吧????取不出来
      

  11.   

    没用过StringGrid,不好意思~~~~~~~~~~~`  *^_^*
      

  12.   

    如果你的StringGrid1不重画,
    是无法取得正确的canvas.brush.color
    因为只保留最后一个重画的cell的颜色如果你没写重画事件,他就会把颜色设为默认值个人认为,如果不借助外部手段来保存颜色的画,应该是不可能实现的
      

  13.   

    通过click,触发它的重画事件
    可以取出最后一次的颜色但是如果想直接读取cell的行列值来去画布的内容,是不行的
    因为以前的值已经被新的画布给覆盖了还是用数组吧
      

  14.   

    不好意思,我不知道哦我已经叫其它人来看了
    不过
    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的数组罢了
      

  15.   

    截掉ondrawcolumncell事件啊。
    干脆就自己画单元格!至于数据不好保存的情况,
    如果不好用数组,
    可以考虑用TSTRINGLIST类来实现。!!!!
      

  16.   

    TSTRINGGRID控件的颜色只有在ondrawcolumncell事件只能取出来的。
    除非那个颜色你自己早就保存了!!!!我曾经实现几种状态的时候,有四种情况,四种颜色的时候,
    我也是自己保存了颜色的。
    用TSTRINGLIST类保存,这个类的最好!
      

  17.   

    这个东西不难阿,你用 API 函数获取当前鼠标所在位置的点的颜色就可以了,具体的自己找一下,呵呵,比较忙,赶快给分
      

  18.   

    应该换一种思路!
    你画Grid的时候,必须有颜色的
    就是你选择了那个格子的时候,就设置某个变量保存所选择的格子,然后再画上颜色我做的日程记事器就是这样实现了
    选择格子-保存所选的位置(日期)-OnDrawCell中处理颜色
      

  19.   

    要保存的不是最后的颜色,而是Grid的“描述”内容,(坐标和颜色一起保存)
    通过建立一张表来维护
    Pos; Color
    1:2;Red
    3:5;Blue
    这种结构不知道你能否理解我所说的意思
      

  20.   

    在STRINGGRID的xx事件中得到单元格的坐标(cells[x,y]),然后设置颜色。读取的时候,读的是cells里的日期内容,根据计算,自己判断应该是什么颜色。不知道这样是否满足你的要求。
      

  21.   

    看了樓上几位的意見, 我覺得, 如果是要建一個列表要記錄的話, 應該不用在這裹討論, 本來就很簡單的事情!! IORILI (眼镜@_@)  應該自己能解決!!
    那能不能用如下來取得呢???
      MyStringGrid.Objects[SomeCol, SomeRow] := TObject(clRed);
    然後再取出呢??
      

  22.   

    楼上的各位老大,设置单元格的颜色已经实现了,
    现在的情况是,假如(周六和周日都是蓝色,此颜色是初始化时话上去的)
       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了,不是吗??我现在的做法是,根据用户选择的人,系统根据人和当前的年,从数据库中找出节假日来,放到一张中间表(临时存储)中,系统根据用户选择的不同颜色(蓝色:节假日;白色:上班)对数据进行增,删操作,等用户点保存的时候,再一起保存进数据库中。
      

  23.   

    眼镜兄,你是不是要知道那些是用户选择作为工作日了,那就用个动态数组或者临时表存下来,至于显示吗,实际上每个CELL都是画布,完全可以对它设置颜色,关键是它被覆盖了以后然后要重新花的问题,别的到没什么,不过我想问题不是很大,我现在要出去,晚上回来再帮你试一下,呵呵,再看看别的兄弟的吧
      

  24.   

    大地说的这句:实际上每个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;嘿嘿,说白了就是取象素点的颜色
      

  25.   

    fengjn(颗粒):  把你的思路说一下。听听
      

  26.   

    为什么没有讨论下我的意见??
    在设置的时候将对应cell的color保存到 StringGrid1.Objects[1,1],
    取出时, 再根据检查 StringGrid1.Objects[1,1], 从中取出对应的 color!!!
    不过, 看过  myling(阿德)  的讨论, 也有点启发!
      

  27.   

    好热闹,Cell的颜色应该可以用Canvas来实现吧,我上次在Cell中画图就是这样实现的。
      

  28.   

    呵呵,颗粒 说的不错,万一正好是在字上的话,取得就是字的颜色但是只要把这个位置设准确一点
    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]这个属性:)惭愧呀
      

  29.   

    我也惭愧,是临时抱佛脚,不过
    aiirii,你说的方法,不知你考虑了这种情况没有:用户在1个日期上蓝色点过,后来又点成白色,
    还有就是,在stringgrid初始化的时候我就开始对12个月进行初始化,那也要把初始化的周六和周日,都存进Objects里,如何删除呢???:当用户开始选了6月1日为节假日,后来又取消了,你怎么判断是6月1日???。还有就是如果用户设置完了1年的节假日后,再设置下一年的节假日,你的Objects又怎么样存贮???
      

  30.   

    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.
      

  31.   

    靠,这错了
    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;
      

  32.   

    fengjn:
    用你的方法,经过改动后,在没有与数据库连接的时候可以达到效果(一个stringgrid)
    那如果从数据库取数据,并且一个窗体上12个stringgrid的话,是不是要有
    每个stringgrid的Objects[j, i]呢?????
      望不吝赐教
      

  33.   

    aiirii(ari):说得方法和这个一样吧,呵呵,,听听你对从数据库中取数据显示的观点
      

  34.   

    我覺得, 應該是這樣的, 初始化時不用同時初始化 StringGrid1.Objects[X,Y], 你可用個公用的 function 來設置相關的顯示;
    在用戶操作時, 有改變的, 就同時設置其Objects[x,y];
    然後, 保存時, 先檢查 Objects 是否為 nil, 不是, 取其值, 不是, 用公用 function取其值;不過綜合考慮, 我覺得用 myling(阿德) 的方法應該代碼量比較少, 出錯可能性也很小!!
      

  35.   

    這與界面與邏輯分開有點類似, 你初始化時對不同 Cell設置color 的規則, 應該體現在一個function 中, 那,也就是說, 你想取某一點初始化的color值, 也可從該 function 取得!當然, 用戶自己改變的, 就只能通過記錄或上面的几種方法來取得了!!
      

  36.   

    数据库是文档(文档不局限于数据库),grid是界面。在文档中保存了哪天应该休息,而grid只是文档的一种表现形式而已。要设置的是文档的内容,当文档的内容变化后需要在grid上反映出变化来。所以我认为楼主要取color可能是考虑偏了。
      

  37.   

    fengjn(颗粒)老兄:
       在诸位的帮助下,我得程序有了很大的进展,但你说:要取color可能是考虑偏了???我就不知其意了,还望告知一二