C#彻底解决listview的闪屏问题
2008-04-14 19:42
309 查看
也许很多人也遇到这样的问题,连续不断向listview中加载数据时界面会不停的闪烁,当你用listview.BeginUpdate(); listview.EndUpdate(); 虽然可以减轻一些,但也不能彻底消除闪屏。不信?可以试试下面的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int counter = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoubleBufferListView lv = new DoubleBufferListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ListView lv = new ListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(lv);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.View = View.Details;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.GridLines = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.BackColor = Color.Silver;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("aa");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("bb");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("cc");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Height = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Width = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void timer1_Tick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv .Items.Add(counter.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
counter++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
解决方法:
使用双缓冲,重写listview
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class DoubleBufferListView : ListView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DoubleBufferListView()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UpdateStyles();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int counter = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoubleBufferListView lv = new DoubleBufferListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(lv);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.View = View.Details;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.GridLines = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.BackColor = Color.Silver;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("aa");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("bb");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("cc");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Height = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Width = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void timer1_Tick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv .Items.Add(counter.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
counter++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
效果是不是很明显啊?呵呵。
参考:http://blog.csdn.net/jinjazz/archive/2007/12/05/1919359.aspx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int counter = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoubleBufferListView lv = new DoubleBufferListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ListView lv = new ListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(lv);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.View = View.Details;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.GridLines = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.BackColor = Color.Silver;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("aa");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("bb");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("cc");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Height = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Width = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void timer1_Tick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv .Items.Add(counter.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
counter++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
解决方法:
使用双缓冲,重写listview
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class DoubleBufferListView : ListView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DoubleBufferListView()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UpdateStyles();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int counter = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoubleBufferListView lv = new DoubleBufferListView();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(lv);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.View = View.Details;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.GridLines = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.BackColor = Color.Silver;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("aa");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("bb");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Columns.Add("cc");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Height = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv.Width = 300;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timer1.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void timer1_Tick(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lv .Items.Add(counter.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
counter++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
效果是不是很明显啊?呵呵。
参考:http://blog.csdn.net/jinjazz/archive/2007/12/05/1919359.aspx
相关文章推荐
- C#彻底解决listview的闪屏问题
- 彻底解决listview的闪屏问题(C#)
- 彻底解决listview的闪屏问题(C#)
- C# ListView双缓存代码,解决闪屏问题
- C#双缓存技术解决绘图时闪屏问题
- c#winform自定义listview,解决listview闪缩问题,添加listview单元格单击事件。
- C# 完美解决窗体切换闪屏问题
- 【C#】解决ListView,在代码部分使用Select=true选定时的背景问题
- C# 线程中更新ListView某单元格导致闪烁问题的解决
- C# 完美解决窗体切换闪屏问题
- c# winfrom 全屏显示,遮盖任务栏,解决闪屏问题
- C#使用双缓冲解决绘图闪屏的问题
- C#通过代码彻底结束桌面进程explorer,解决自动重启问题
- C#ListView失去焦点选中行不能高亮显示的问题解决
- [代码问题解决录A1]c#Listview控件动态添加数据并阻止重复添加
- 彻底解决listview,gridview的getview多次调用问题
- C# 中DataGridView和ListView闪烁问题的解决方法
- C# 解决winform界面闪屏问题
- 解决C#中ListView数据刷新时界面闪烁的问题
- 【C#】MDI父窗体嵌套子窗体 切换时闪屏问题解决办法