首页  编辑  

ListView闪烁的问题

Tags: /超级猛料/VCL/ListView/   Date Created:

解决ListView闪烁的终极大法

By hsn

使用ListView的时候,如果内容稍多,即使采用BeginUpdate,EndUpdate,同样会闪烁的很厉害,采用下面的方法就可以解决这个问题:

首先在使用ListView的地方,都用一个Panel,然后在Panel里面放置ListView,最后在你的BeginUpdate和EndUpdate的前后添加如下代码:

 SendMessage(ListView1.Parent.Handle, WM_SETREDRAW, 0, 0);

 /// Do List Item Operation

 SendMessage(ListView1.Parent.Handle, WM_SETREDRAW, 1, 0);

 R := ListView1.ClientRect;

 InvalidateRect(ListView1.Handle, @R, False);

请对比如下代码的效果:

procedure TForm1 . Button1Click ( Sender : TObject ); var   i : Integer ; begin   with ListView1 . Items do   try     BeginUpdate ;     Clear ;     for i := 0 to 100 do     with Add do     begin       Caption := IntToStr ( i );       SubItems . Add ( 'Sub 1' );       SubItems . Add ( 'Sub 2' );       SubItems . Add ( 'Sub 3' );       SubItems . Add ( 'Sub 4' );       SubItems . Add ( 'Sub 5' );     end ;   finally     EndUpdate ;   end ; end ; procedure TForm1 . Button2Click ( Sender : TObject ); var   i : Integer ;   R : TRect ; begin   with ListView1 . Items do   try     SendMessage ( ListView1 . Parent . Handle , WM_SETREDRAW , 0 , 0 );     BeginUpdate ;     Clear ;     for i := 0 to 100 do     with Add do     begin       Caption := IntToStr ( i );       SubItems . Add ( 'Sub 1' );       SubItems . Add ( 'Sub 2' );       SubItems . Add ( 'Sub 3' );       SubItems . Add ( 'Sub 4' );       SubItems . Add ( 'Sub 5' );     end ;   finally     EndUpdate ;     SendMessage ( ListView1 . Parent . Handle , WM_SETREDRAW , 1 , 0 );     R := ListView1 . ClientRect ;     InvalidateRect ( ListView1 . Handle , @ R , False );   end ; end ;