一段个性化stringgrid的代码

需要注意的是 该段程序使用了 canvas。

procedure TW_CkbTaiZhang.KhLstDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
   if ARow=khlst.Row then
   with khlst.Canvas do //画 cell 的边框
    begin
      Pen.Color := $00ff0000; //定义画笔颜色(蓝色)
      MoveTo(Rect.Left, Rect.Bottom); //画笔定位
      LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线
      Brush.Color :=TColor(RGB(10,100,100));
      fillrect(rect);
      textout(rect.left+4,rect.top+4,khlst.cells[ACol,ARow]);   //一定要使用textout输出文字才能将效果输出。
    end;

   //实现 将 其他控件 浮动在表格框里的效果。
    if (gdFocused in state) then
    begin
      if KhLst.ItemCheck[4, ARow] then
      begin
        if ACol in [5] then
        begin
          cmbYearMonth.Left := Rect.Left + KhLst.Left;
          cmbYearMonth.Top := rect.Top +KhLst.Top;
          cmbYearMonth.Width :=rect.Right-rect.Left;
          cmbYearMonth.Height :=rect.Bottom - rect.Top;
          cmbYearMonth.Text :=khlst.Cells[ACol,ARow];
          cmbYearMonth.Visible := true;
          cmbYearMonth.SetFocus;
        end else cmbyearmonth.Visible := false;
        if ACol in [6] then
        begin
          cmbbxh.Left := Rect.Left + KhLst.Left;
          cmbbxh.Top := rect.Top +KhLst.Top;
          cmbbxh.Width :=rect.Right-rect.Left;
          cmbbxh.Height :=rect.Bottom - rect.Top;
          cmbbxh.Text :=khlst.Cells[ACol,ARow];
          cmbbxh.Visible := true;
          cmbbxh.SetFocus;
        end else cmbbxh.Visible :=false;
        if ACol in [7] then
        begin
          cmbbrl.Left := Rect.Left + KhLst.Left;
          cmbbrl.Top := rect.Top +KhLst.Top;
          cmbbrl.Width :=rect.Right-rect.Left;
          cmbbrl.Height :=rect.Bottom - rect.Top;
          cmbbrl.Text :=khlst.Cells[ACol,ARow];
          cmbbrl.Visible := true;
          cmbbrl.SetFocus;
        end else cmbbrl.Visible :=false;
        if aCol in [8] then
        begin
          cmbmc.Left := Rect.Left + KhLst.Left;
          cmbmc.Top := rect.Top +KhLst.Top;
          cmbmc.Width :=rect.Right-rect.Left;
          cmbmc.Height :=rect.Bottom - rect.Top;
          cmbmc.Text :=khlst.Cells[aCol,aRow];
          cmbmc.Visible := true;
          cmbmc.SetFocus;
        end else cmbmc.Visible :=false;
        if acol in [9] then
        begin
          cmbbl.Left := Rect.Left + KhLst.Left;
          cmbbl.Top := rect.Top +KhLst.Top;
          cmbbl.Width :=rect.Right-rect.Left;
          cmbbl.Height :=rect.Bottom - rect.Top;
          cmbbl.Text :=khlst.Cells[aCol,aRow];
          cmbbl.Visible := true;
          cmbbl.SetFocus;
        end else cmbbl.Visible :=false;
      end else
      begin
        cmbYearMonth.Visible :=false;
        cmbbxh.visible :=false;
        cmbmc.Visible :=false;
        cmbbrl.Visible :=false;
        cmbbl.Visible :=false;
      end;
    end;
end;

posted @ 2011-12-10 20:42  wmymtx  阅读(243)  评论(0编辑  收藏  举报