有一个drawgrid控件,设置为5行5列,要在每个单元格中写入不同的数据(文本,要可以换行,每行文本可自定义,自定义颜色),如何实现?谢谢!!!!

解决方案 »

  1.   

    procedure Tfm_main.GetTWInfo;  
    var
      outputTW: TRect;
      RowCount, RecNo, ColNo, RowNo, twstate: Integer;
      twname: string;
      DIVNUM, MODNUM: Integer;
    begin
      if OpenSQL(adoqry_tw,
        'select A.*,B.name as bm_name ,c.name as type_name from  tw_info A,tw_bm B,tw_type C where A.twbm=b.id and A.tw_type=c.id order by a.code') =
        True then
      begin
        twRow := adoqry_tw.RecordCount;
        RowCount := twRow div drwgrd_tw.ColCount;
        drwgrd_tw.RowCount := RowCount + 1;
      end;
      adoqry_tw.First;
      while not adoqry_tw.Eof do
      begin
        RecNo := adoqry_tw.RecNo;
        DIVNUM := RecNo div drwgrd_tw.ColCount; //行号
        MODNUM := RecNo mod drwgrd_tw.ColCount; //列号
        ColNo := 0;
        RowNo := 0;
        if RecNo <= drwgrd_tw.ColCount then
        begin
          ColNo := 0;
          RowNo := RecNo - 1;
        end;
        if (DIVNUM >= 1) and (RecNo > drwgrd_tw.ColCount) and (MODNUM = 0) then
        begin
          ColNo := DIVNUM - 1;
          RowNo := drwgrd_tw.ColCount - 1;
        end;
        if (DIVNUM >= 1) and (RecNo > drwgrd_tw.ColCount) and (MODNUM <> 0) then
        begin
          ColNo := DIVNUM;
          RowNo := MODNUM - 1;
        end;
        outputTW := drwgrd_tw.CellRect(RowNo, ColNo);
        twname := Trim(adoqry_tw.FieldByName('name').AsString);
        twstate := adoqry_tw.FieldByName('tw_state').AsInteger;
        drwgrd_tw.Canvas.Brush.Color := clWhite;
        drwgrd_tw.Canvas.Font.Color := clBlack;
        if twstate = 2 then //使用
        begin
          drwgrd_tw.Canvas.Brush.Color := clLime;
        end;
        if twstate = 3 then //预定
        begin
          drwgrd_tw.Canvas.Brush.Color := clActiveCaption;
          drwgrd_tw.Canvas.Font.Color := clWindow;
        end;
        if twstate = 4 then //禁用
        begin
          drwgrd_tw.Canvas.Brush.Color := clYellow;
        end;
        drwgrd_tw.Canvas.TextRect(outputTW, outputtw.Left + 2, outputtw.Top + 2, twname);
        drwgrd_tw.Canvas.TextOut(outputtw.Left + 1, outputtw.Top + 20,
          Trim(adoqry_tw.FieldByName('type_name').AsString));
        adoqry_tw.Next;
      end;
    end;
      

  2.   

    自由界面和报表的完美解决方案。
    http://www.anylib.com