由于需要使用StringGrid做一个显示日历的功能,
并标出特定的日期。使用过程中遇到以下几问题:1。怎样使在grid 的某几个cell(如3行5列) 中显示字符串字体变成其他颜色?
2。怎样使在grid 的某几个cell(如3行7列) 变成其他颜色?

解决方案 »

  1.   

    在OnDrawCell中写:
    With StringGrid1 do
    begin
      If (ARow=3) andnot (acol=5) then //3行5列
      begin
        Canvas.Brush.Color:=clYellow;//改背景颜色
        Canvas.FillRect(Rect);
        Canvas.font.color:=ClBlack;//改字体颜色
        Canvas.TextOut(rect.left,rect.top,cells[acol,arow]);
      end;
    end;
      

  2.   

    改个错 ^_^
    With StringGrid1 do
    begin
      If (ARow=3) and (acol=5) then //3行5列
      begin
        Canvas.Brush.Color:=clYellow;//改背景颜色
        Canvas.FillRect(Rect);
        Canvas.font.color:=ClBlack;//改字体颜色
        Canvas.TextOut(rect.left,rect.top,cells[acol,arow]);
      end;
    end;
      

  3.   

    这个是StringGrid1 全操作
    希望对你有用(注:别人的资料)
    StringGrid行列的增加和删除
    如何编写使StringGrid中的一列具有Check功能,和CheckBox效果一样
    StringGrid组件Cells内容分行显示在Tstringgrid.ondrawcell事件中
    在StringGrid怎样制作只读的列在 OnSelectCell事件处理程序中
    stringgrid从文本读入的问题
    StringGrid组件Cells内容对齐
    StringGird的行列背景色设置
    怎么改变StringGrid控件某一列的背景和某一列的只读属性
    StringGrid控件标题栏的对齐
    怎么改变StringGrid控件某一列的背景和某一列的只读属性
    StringGrid控件标题栏的对齐
    在stringGrid中使用回车键模拟TAB键切换单元格的功能实现
    stringgrid如何清空
    让记录在StringGrid中分页显示在
    打印StringGrid
    如何实现在stringgrid中删除鼠标点中的那一行,下一行再顶上的效果
    让stringgrid点列头进行排序
    正确地设置StringGrid列宽而不截断任何一个文字方法
    实现StringGrid的删除,插入,排序行操作
    TstringGrid 的行列合并研究
      

  4.   

    StringGrid行列的增加和删除
    type
    TExCell = class(TStringGrid)public
    procedure DeleteRow(ARow: Longint);
    procedure DeleteColumn(ACol: Longint);
    procedure InsertRow(ARow: LongInt);
    procedure InsertColumn(ACol: LongInt);
    end;procedure TExCell.InsertColumn(ACol: Integer);
    begin
    ColCount :=ColCount +1;
    MoveColumn(ColCount-1, ACol);
    end;procedure TExCell.InsertRow(ARow: Integer);
    begin
    RowCount :=RowCount +1;
    MoveRow(RowCount-1, ARow);
    end;procedure TExCell.DeleteColumn(ACol: Longint);
    begin
    MoveColumn(ACol, ColCount -1);
    ColCount := ColCount - 1;
    end;procedure TExCell.DeleteRow(ARow: Longint);
    begin
    MoveRow(ARow, RowCount - 1);
    RowCount := RowCount - 1;
    end;如何编写使StringGrid中的一列具有Check功能,和CheckBox效果一样
    unit Unit1;interfaceuses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids;type
    TForm1 = class(TForm)
    grid: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure gridDrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
    procedure gridClick(Sender: TObject);private
    { Private declarations }public
    { Public declarations }end;var
    Form1: TForm1;
    fcheck,fnocheck:tbitmap;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
    var
    i:SmallInt;
    bmp:TBitmap;
    begin
    FCheck:= TBitmap.Create;
    FNoCheck:= TBitmap.Create;
    bmp:= TBitmap.create;
    try
    bmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));
    With FNoCheck Do Begin
    width := bmp.width div 4;
    height := bmp.height div 3;
    canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );
    End;
    With FCheck Do Begin
    width := bmp.width div 4;
    height := bmp.height div 3;
    canvas.copyrect(canvas.cliprect, bmp.canvas, rect( width, 0, 2*width, height ));
    End;
    finally
    bmp.free
    end;
    end;procedure TForm1.gridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    begin
    if not (gdFixed in State) then
    with TStringGrid(Sender).Canvas do
    begin
    brush.Color:=clWindow;
    FillRect(Rect);
    if Grid.Cells[ACol,ARow]='yes' then
    Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FCheck )
    else
    Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FNoCheck );
    end;
    end;procedure TForm1.gridClick(Sender: TObject);
    begin
    if grid.Cells[grid.col,grid.row]='yes' then
    grid.Cells[grid.col,grid.row]:='no'
    else
    grid.Cells[grid.col,grid.row]:='yes';
    end;end.
    2003-11-17 16:23:23 StringGrid组件Cells内容分行显示在Tstringgrid.ondrawcell事件中:DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);可以实现文字换行!
    2003-11-17 16:24:04 在StringGrid怎样制作只读的列在 OnSelectCell事件处理程序中,加入: (所有的列均设成可修改的)if Col mod 2 = 0 then
    grd.Options := grd.Options + [goEditing]
    else
    grd.Options := grd.Options - [goEditing];
    2003-11-17 16:25:07 stringgrid从文本读入的问题(Save/Load a TStringGrid to/from a file?)stringgrid从文本读入的问题(Save/Load a TStringGrid to/from a file?)// Save a TStringGrid to a file
    procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
    f: TextFile;
    i, k: Integer;
    begin
    AssignFile(f, FileName);
    Rewrite(f);
    with StringGrid do
    begin
    // Write number of Columns/Rows
    Writeln(f, ColCount);
    Writeln(f, RowCount);
    // loop through cells
    for i := 0 to ColCount - 1 do
    for k := 0 to RowCount - 1 do
    Writeln(F, Cells[i, k]);
    end;
    CloseFile(F);
    end;// Load a TStringGrid from a file
    procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
    f: TextFile;
    iTmp, i, k: Integer;
    strTemp: String;
    begin
    AssignFile(f, FileName);
    Reset(f);
    with StringGrid do
    begin
    // Get number of columns
    Readln(f, iTmp);
    ColCount := iTmp;
    // Get number of rows
    Readln(f, iTmp);
    RowCount := iTmp;
    // loop through cells & fill in values
    for i := 0 to ColCount - 1 do
    for k := 0 to RowCount - 1 do
    begin
    Readln(f, strTemp);
    Cells[i, k] := strTemp;
    end;
    end;
    CloseFile(f);
    end;// Save StringGrid1 to 'c:.txt':
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    SaveStringGrid(StringGrid1, 'c:.txt');
    end;// Load StringGrid1 from 'c:.txt':
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    LoadStringGrid(StringGrid1, 'c:.txt');
    end;*******************************************打开一个已有的文本文件,并将内容放到stringgrid中,文本行与stringgrid行一致;
    在文本中遇到空格则放入下一cells.
    搞定!注意,我只写了一个空格间隔的,你自己修改一下splitstring可以用多个空格分隔!procedure TForm1.Button1Click(Sender: TObject);
    var
    aa,bb:tstringlist;
    i:integer;
    begin
    aa:=tstringlist.Create;
    bb:=tstringlist.Create;
    aa.LoadFromFile('c:.txt');
    for i:=0 to aa.Count-1 do
    begin
    bb:=SplitString(aa.Strings[i],' ');
    stringgrid1.Rows[i]:=bb;
    end;
    aa.Free;
    bb.Free;
    end;其中splitstring为:function SplitString(const source,ch:string):tstringlist;
    var
    temp:string;
    i:integer;
    begin
    result:=tstringlist.Create;
    temp:=source;
    i:=pos(ch,source);
    while i<>0 do
    begin
    result.Add(copy(temp,0,i-1));
    delete(temp,1,i);
    i:=pos(ch,temp);
    end;
    result.Add(temp);
    end;
    StringGrid组件Cells内容对齐在StringGrid的DrawCell事件中添加类似的代码就可以了:VAR
    vCol, vRow : LongInt;
    begin
    vCol := ACol; vRow := ARow;
    WITH Sender AS TStringGrid, Canvas DO
    IF vCol = 2 THEN BEGIN ///对于第2列设置为右对齐
    SetTextAlign(Handle, TA_RIGHT);
    FillRect(Rect);
    TextRect(Rect, Rect.RIGHT-2, Rect.Top+2, Cells[vCol, vRow]);
    END;
    end;
      

  5.   

    2003-11-17 16:28:41 当我将StringGird的options属性中包含goRowSelect项时每当我选中StringGrid中一行, 则选中行用深蓝色显示,我想将深蓝色改为其他颜色应怎样该?当我将StringGird的options属性中包含goRowSelect项时每当我选中StringGrid中一行, 则选中行用深蓝色显示,我想将深蓝色改为其他颜色应怎样该?procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
    begin
    With StringGrid1 do
    begin
    If (ARow= Krow) and not (acol = 0) then
    begin
    Canvas.Brush.Color :=clYellow;// ClBlue;
    Canvas.FillRect(Rect);
    Canvas.font.color:=ClBlack;
    Canvas.TextOut(rect.left , rect.top, cells[acol, arow]);
    end;
    end;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
    ARow: Integer; var CanSelect: Boolean);
    begin
    krow := Arow; //*
    kcol := Acol;
    end;注意:必须把变量KROW的值初始为1或其他不为0的值,否则如果锁定第一行的话,第一行的颜色将被自设颜色取代,而锁定行不会被重画。
    2003-11-17 16:32:44 怎么改变StringGrid控件某一列的背景和某一列的只读属性,StringGrid控件标题栏的对齐.怎么改变StringGrid控件某一列的背景和某一列的只读属性,StringGrid控件标题栏的对齐.
    请参考以下代码:
    在OnDrawCell事件中处理背景色。程序如下:
    //将第二列背景变为红色。
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
    begin
    if not((acol=1) and (arow>=stringgrid1.fixedrows)) then exit;
    with stringgrid1 do
    begin
    canvas.Brush.color:=clRed;
    canvas.FillRect(Rect);
    canvas.TextOut(rect.left+2,rect.top+2,cells[acol,arow])
    end;
    end;//加入如下代码,那么StringGrid的第四列就只读了.其他列非只读
    procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    begin
    with StringGrid1 do begin
    if ACol = 4 then
    Options := Options - [goEditing]
    else Options := Options + [goEditing];
    end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
    dx,dy:byte;
    begin
    if (acol = 4) and not (arow = 0) then
    with stringgrid1 do
    begin
    canvas.Brush.color := clYellow;
    canvas.FillRect(Rect);
    canvas.font.color := clblue;
    dx:=2;//调整此值,控制字在网格中显示的水平位置
    dy:=2;//调整此值,控制字在网格中显示的垂直位置
    canvas.TextOut(rect.left+dx , rect.top+dy , cells[acol, arow]);
    end;
    //控制标题栏的对齐
    if (arow = 0) then
    with stringgrid1 do
    begin
    canvas.Brush.color := clbtnface;
    canvas.FillRect(Rect);
    dx := 12; //调整此值,控制字在网格中显示的水平位置
    dy := 5; //调整此值,控制字在网格中显示的垂直位置
    canvas.TextOut(rect.left + dx, rect.top + dy, cells[acol, arow]);
    end;
    end;
      

  6.   

    楼上的朋友真是厚道呀,
    不过小弟是cccccc鸟。
    不知道怎样调用,传什么参数,望大虾们指教
      

  7.   

    晕,代码全贴出来了,COPY过去运行先呀,
      

  8.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    With StringGrid1 do
    begin
      If (ARow=3) andnot (acol=5) then //3行5列
      begin
        Canvas.Brush.Color:=clYellow;//改背景颜色
        Canvas.FillRect(Rect);
        Canvas.font.color:=ClBlack;//改字体颜色
        Canvas.TextOut(rect.left,rect.top,cells[acol,arow]);
      end;
    end;
    end;
      

  9.   

    我copy 的是:
    With StringGrid1 do
    begin
      If (ARow=3) and (acol=5) then //3行5列
      begin
        Canvas.Brush.Color:=clYellow;//改背景颜色
        Canvas.FillRect(Rect);
        Canvas.font.color:=ClBlack;//改字体颜色
        Canvas.TextOut(rect.left,rect.top,cells[acol,arow]);
      end;
    end;
    ****If (ARow=3) and (acol=5) then //3行5列 这一行同
    If (ARow=3) andnot (acol=5) then //3行5列不一样。所以不行,不好意思合。不过  (ARow=3) andnot (acol=2) then 实现了 3+1行2+1列的cell 不被重绘。有没有直接的重绘某一列的。
      

  10.   

    错了错了,重绘某个cell 的!!不好意思。