TStringGrid does not update when InvalidateCol () is called

I want to create a grid of rows to display some sort of vertical cursor to highlight the currently selected column. So in MouseDown, I call setCurPos, then I call InvalidateCol to undo the current column. This calls DrawCell. DrawCell draws the cursor on the current column.

The problem is this: if I have more lines in the grid, then it can display some of them that will not be visible (of course), so that the vertical scroll bar of the grid will automatically appear. When I scroll down to see the rows at the bottom of the table, the cursor does not color on these rows. It seems that the number of bottom lines (now visible on the screen) in which the cursor is NOT colored is proportional to the number of invisible lines at the top of the grid.

If I minimize and restore the application, the cursor is beautifully colored. So obviously invalidateColumn () is not working.

procedure TmyGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
VAR aCol, aRow: Integer;
begin
 MouseToCell(X, Y, ACol, ARow);
 ...                                                                  
    inherited MouseDown(Button, Shift, X, Y); 
    CursorPosFocus:= ACol;                          
end;


procedure TmyGrid.setCurPos(CONST NewColumn: Integer);                 
VAR OldPos: Integer;
begin
 ...
 OldPos:= CursorPos;
 FCursorPos:= NewColumn;    
 ...
 //- This is not working:
 //InvalidateCol(OldPos);
 //InvalidateCol(NewColumn);    
 //Update;

 //- THIS WORKS:
 InvalidateGrid; 
end;


procedure TmyGrid.DrawCell(ACol, ARow: integer; ARect: TRect; AState: TGridDrawState);
Var TempRect: TRect;
begin
 inherited;
  ...

 {DRAW CURSOR}
 if CursorPos= ACol then
  begin
   TempRect.Top   := 0;
   TempRect.Left  := ARect.Left;
   TempRect.Right := ARect.Right;
   TempRect.Bottom:= ClientHeight-2;     
   Frame3D(Canvas, TempRect, $909090, $808080, 1);       
  end;
end;

Delphi 7, Win XP

+3
source share
2 answers

You are not doing anything, you just caught the error in the implementation of the VCL grid that was in Delphi 4 VCL (I do not have earlier CDs here to check, but it could even be in 16 bits Delphi VCL already) and still with us at Delphi 2009.

, , InvalidateRect(). / 0 /. , . /, InvalidateRect() , , .

, , ; , , , . InvalidateRect() (, ), Windows API , CellRect() BoxRect().

InvalidateGrid() , - , , , , , InvalidateCol().

. - , , , . -

StringGrid1.Canvas.Brush.Color := RGB(Random(256), Random(256), Random(256));
StringGrid1.Canvas.FillRect(Rect);

OnDrawCell .

+4

:
, InvalidateGrid.

, . , . InvalidateCol().

0

Source: https://habr.com/ru/post/1759447/


All Articles