| |

VerySource

 Forgot password?
 Register
Search
View: 2811|Reply: 17

stringGrid flickering issue

[Copy link]

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-13 20:00:01
| Show all posts |Read mode
I have the following code in the drawcell event of stringGrid, which roughly means that every four rows alternately change the color,
Because there are many grids, when the scroll bar in the stringGrid changes, those grids flicker very much. How can I solve it?
code show as below:
var
  i, j: integer;
  rect: TRect;
begin
  with stringGrid do
  begin
    for i: = 1 to RowCount do
      for j: = 1 to ColCount do
      begin
        case ch of
          True: begin
                  Canvas.Brush.Color: = clSkyBlue; // ClBlue;
                  if (i mod 4 = 0) and (j = ColCount) then
                    if (i div 4) mod 2 = 0 then
                      ch: = False;
                end;
         False: begin
                 Canvas.Brush.Color: = clWindow;
                 if (i mod 4 = 0) and (j = ColCount) then
                   if (i div 4) mod 2 = 1 then
                   begin
                     ch: = True;
                   end;
            end;
         end;
            rect: = CellRect (j, i);
            Canvas.FillRect (rect);
            Canvas.font.color: = ClBlack;
            Canvas.TextOut (rect.left, rect.top, Cells [j, i]);
    end;
  end;
end;
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-11 01:45:01
| Show all posts
There is a problem with your code. In the Drawcell event, it is the event triggered by drawing each cell.
Think about whether your code has failed!
Remove the loop, and directly use the parameters Arow and Acol to represent the currently drawn rows and columns,
Talk about your code problem,
1. Rect already has such a parameter in the event, how do you still define it above?
2. Remove from circulation
3. Draw the cell and write it like this. Change this sentence:
   Canvas.TextOut(rect.left, rect.top, Cells[j, i]);
   To:
   Canvas.TextRect(Rect,Rect.left,Rect.top,Cells[Acol,Arow]);
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-12 15:15:01
| Show all posts
Save the color of each line, when you find the same, you don't need to redraw, just exit, so that it will not flicker.
Reply

Use magic Report

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Macau

 Author| Post time: 2020-6-13 20:45:01
| Show all posts
stevevai:
   Because my stringGrid has 120 rows and 30 columns, but it can display up to 20 rows and 12 columns. The other rows and columns need to be moved by the scroll bar to see. But the maximum number of ACOL in this Drawcell is also the most displayable column. That is, 12 columns, with your method there will be no flickering phenomenon, but when moving the unit or scroll bar, those colors are displayed abnormally. So I came up with this stupid method. Can you help to solve it.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-14 15:15:01
| Show all posts
You put the refresh control in the scroll key and stop refreshing when you scroll. Try it
Reply

Use magic Report

2

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Macau

 Author| Post time: 2020-6-16 08:00:01
| Show all posts
tonjmumu:

  But when I first loaded the form, there was already some flicker.
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-16 10:30:01
| Show all posts
Arow can determine which row you are in, so no matter whether there is a scroll bar, you can use Arow to complete the function you need. I haven't encountered the problem you said, wait for me to test it for you,
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-17 23:15:01
| Show all posts
I have encountered this problem:
But when moving the unit or scroll bar, those colors are displayed abnormally. You add the following code to the TopLeftChanged method of stringGrid:
    stringGrid.Enabled = false;
    stringGrid.Refresh;
    stringGrid.Enabled = true;
    stringGrid.SetFocus;
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-18 21:30:01
| Show all posts
The Drawcell of stringGrid only draws the displayed part. When the cell or scroll bar is moved, the stringGrid draws the previously displayed part, so the color is not normal. The TopLeftChanged event can be triggered when the cell or scroll bar is moved, and the refresh is forced in the TopLeftChanged event. Can be solved.
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-19 00:30:01
| Show all posts
After the test, this is a problem with your code algorithm. Let me show you my code:
with self.StringGrid1 do
     begin
          if ((Arow div 4) mod 2)=0 then
             canvas.Brush.Color:=clSkyBlue
          else
             canvas.Brush.Color:=clred;// ClBlue;

          Canvas.font.color:=Clblue;
          Canvas.TextRect(Rect,Rect.left,Rect.top,Cells[Acol,Arow]);
     end;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list