简体   繁体   中英

Delphi Grid DrawColumnCell can not clear

I am following examples to put a checkbox in a delphi column cell. I can place a checkbox image in a cell. But the underlying boolean value is longer than the image so the checkmark is over the first two letters of "True" or "False". How can I clear the rectangle in the column cell? I tried printing the original "True" or "False" in no color, or white on white, but the text is always Black no matter what choice I make. Sample of my code:

procedure TFUpdtLists.TreatGridDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State:  
TGridDrawState);
begin
  with Column do begin
    //  TreatGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);  ???
    if FieldName = 'Repro' then begin
      Canvas.Brush.Color:= TreatGrid.Color;
      Canvas.FillRect(Rect);//fails to clear cell, clears another area??
      if Field.Value = True then
        //#7 a checkmark and # 5 a blank image in the image list:
        MainView.ImageList1.Draw(TreatGrid.Canvas,Rect.Left,Rect.Top,7)
      else  
        MainView.ImageList1.Draw(TreatGrid.Canvas,Rect.Left,Rect.Top,5);
      TreatGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);//??
    end;
  end;
end;

Thanks in advance!

Pay attention to the Canvas you refer to in the code when you paint the background. Since there is no argument called Canvas and also Column doesn't have a Canvas it refers to the Canvas of the TFUpdtLists executing the code.

You have already corrected the code for the mageList1.Draw() methods and added the grids reference ( TreatGrid ). Do the same for the bg painting.

It appears you have tried calling DefaultDrawColumnCell() in different ways, but you should call it only for the columns that you don't draw yourself.

Let me also suggest that you quit using with ... because it prevents code completion from working and it makes debugging harder as well as increases significantly the risc of coding errors.

With the above mentioned changes the OnDrawColumnCell() procedure looks like this;

procedure TForm19.TreatGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Column.FieldName = 'Repro' then
  begin
    TreatGrid.Canvas.Brush.Color:= TreatGrid.Color;
    TreatGrid.Canvas.FillRect(Rect);
    if Column.Field.Value then
    //#7 a checkmark and # 5 a blank image in the image list:
      Form19.ImageList1.Draw(TreatGrid.Canvas,Rect.Left,Rect.Top,1)
    else
      Form19.ImageList1.Draw(TreatGrid.Canvas,Rect.Left,Rect.Top,0);
  end
  else
    TreatGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

You will want to correct the image indexes, which I changed to suit my imagelist in my test.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM