以编程方式在delphi drawgrid中绘制线条并合并单元格

时间:2013-06-26 18:43:40

标签: delphi delphi-xe2 pascal

我想在drawgrid中禁用网格线,并为每个其他列自己绘制网格线。不需要行行。

我想合并固定区域中的两个单元格,使其看起来像是一列,如下图所示:

Grid with merged columns

我已将此代码添加到drawgrid的ondrawcell事件中以实现此目的:

procedure Tbookings3_Frm.bgridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  CellIndex: Integer;
    s:string;
  x:integer;
begin
  CellIndex := (ARow * bgrid.ColCount) + ACol;

  if gdFixed in State then
  begin
    bgrid.Canvas.Brush.Color := clskyblue;
  end
  else if (State * [gdSelected, gdHotTrack]) <> [] then
  begin
    bgrid.Canvas.Brush.Color := clHighlight;
  end
  else
  begin
    bgrid.Canvas.Brush.Color := Cells[CellIndex].BkColor;
  end;



  bgrid.Canvas.FillRect(Rect);

  if gdFixed in State then
    Frame3D(bgrid.Canvas, Rect, clHighlight, clBtnShadow, 1);

  if gdFocused in State then
    bgrid.Canvas.DrawFocusRect(Rect);

//---------------

  with (Sender as TDrawGrid).Canvas do
  begin
     // set font
    Font.Color := CLblack;
    FillRect(Rect);

    if ARow = 2 then
    begin
        x := (Rect.Right - Rect.Left - TextWidth(days_h[ACol])) div 2;
        TextOut(Rect.Left + x, Rect.Top + 2, days_h[ACol]);
    end;
  if ARow = 1 then
     begin
       x := (Rect.Right - Rect.Left - TextWidth(sun_mon[ACol])) div 2;
       TextOut(Rect.Left + x, Rect.Top + 2, sun_mon[ACol]);

     end;
  if ARow = 0 then
     begin
        x := (Rect.Right - Rect.Left - TextWidth(mon[ACol])) div 2;
        TextOut(Rect.Left + x, Rect.Top + 2, mon[ACol]);
      end;

  if (Acol = 0) and (ARow > 2) then
     begin
        s:=rooms[Arow];
        x := (Rect.Right - Rect.Left - TextWidth(s)) div 2;
        TextOut(Rect.Left + x, Rect.Top + 2, s);
     end;

//-------------------------------------------------



  end; //end canvas
//----------------
  if gdFocused in State then
    bgrid.Canvas.DrawFocusRect(Rect);
end;

1 个答案:

答案 0 :(得分:1)

您需要禁用网格的原生网格线,然后根据需要在OnDrawCell事件中围绕每个单元格绘制自己的网格线。 TRect表示正在绘制的单元格的内部区域,但您也可以在该Rect的外部绘制。要使两个单元格合并,您不会在它们之间绘制网格线。