当按下任何箭头键时,Delphi -drawgrid显示多个绘制单元格实例。如何纠正它

时间:2013-06-22 14:26:30

标签: delphi delphi-xe2 pascal

当我按下所选按钮时,细胞会在Delphi drawgrid中绘制。但是当我按任意箭头键时,绘制的细胞会出现多次。

enter image description here

图为3个街区。蓝色的是实际的,而2个水色是按下任何箭头键后绘制的那个

调用create_env过程时会传递当前(或某个日期)日期。

    unit bookings2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Grids, Vcl.StdCtrls,
  Data.DB, Data.Win.ADODB;

type
  CellInfo = record
    BkColor: TColor;
  end;
  TScheduleItem = record
    Text : string;
    Row : integer;
    FirstX : integer;
    LastX : integer;
    Res_ID, Room_No :string;
  end;

  TArr_Rows = record
    row_no: integer;
    room_no: string;
  end;

  TArr_Col = record
    col_no: integer;
    b_date: Tdate;
  end;

  Tbookings2_frm = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    bGrid: TDrawGrid;
    Button1: TButton;
    qRooms: TADOQuery;
    qRoomsRoom_No: TStringField;
    qRoomsRoomType: TStringField;
    qRoomsDescription: TStringField;
    qRoomsdefault_price: TBCDField;
    qRoomsmax_pax: TIntegerField;
    qRoomsroom_status: TStringField;
    procedure bGridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure create_env(start_date:tDate);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Cells: array of CellInfo;

  public
    { Public declarations }
    tot_rooms:integer;
    rooms:array of string;
    Shedule_item:array of TScheduleItem;
    Arr_Rows:array of TArr_Rows;
    col_count:integer;
    days_h, sun_mon, mon:array[0..50] of string;
    short_days:array[1..7] of string;
    ShortMonthNames : array[1..12] of string;
  end;

var
  bookings2_frm: Tbookings2_frm;

implementation

{$R *.dfm}

uses data, data_guest;

procedure Tbookings2_frm.bGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  CellIndex: Integer;

begin
  CellIndex := (ARow * bGrid.RowCount) + ACol;

  if gdFixed in State then
  begin
    bGrid.Canvas.Brush.Color := bGrid.FixedColor;
  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 gdFocused in State then
    bGrid.Canvas.DrawFocusRect(Rect);

end;

procedure Tbookings2_frm.Button1Click(Sender: TObject);
var
  R: TGridRect;
  Row, Col: Integer;
begin
  {
  R.Left :=6;
  R.Top:=3;
  R.Right:=8;
  R.Bottom:=3;
}

  R := bgrid.Selection;
  //bGrid.Selection:=R;
  for Row := R.Top to r.Bottom do
  begin
    for Col := R.Left to R.Right do
    begin
      Cells[(Row * bGrid.RowCount) + Col].BkColor := clAqua;
    end;
  end;


end;

procedure Tbookings2_frm.create_env (start_date:tDate);
var
  room_count, row_count:integer;
  I, ACol, FixedWidth: integer;
  stDate, endDate:tDate;
  year,month,day,s_day:word;
  s, str1, str_stDate, str_endDate:string;
begin

    //check how many rooms
    qRooms.Close;
    qRooms.SQL.Clear;
    qRooms.SQL.Add('select * from pm_Rooms where room_status = ''AVAIL'' ');
    qRooms.Open;


    tot_rooms:=qrooms.RecordCount;
    SetLength(rooms, tot_rooms+4);

    SetLength (Shedule_item, 50);
    SetLength (Arr_Rows , tot_rooms+3);


    stDate:=start_date-10;
    endDate:=stDate+50;

    room_count:=qrooms.RecordCount;
    bgrid.RowCount:=room_count+3;    // 3 fixed rows

    with bgrid do
       begin
         for I := 1 to ColCount - 1 do
          begin
             bgrid.ColWidths[i]:=30;
             //bgrid.Canvas.
          end;

       end;
    bgrid.ColWidths[0]:=100;


//-------------------------
    SetLength(Cells, bGrid.RowCount * bGrid.ColCount);
    for I := Low(Cells) to High(Cells) do
    begin
      Cells[I].BkColor := bGrid.Color;
    end;
end;

end. 

1 个答案:

答案 0 :(得分:0)

您的单元格数组索引计算(Row * bGrid.RowCount) + Col不正确。它应该是(Row * bGrid.ColCount) + Col,因为每行都有ColCount元素。