滚动时在面板中平滑绘制或绘画子控件 [英] Smooth drawing or painting of child controls in Panel while scrolling
问题描述
在Tab控件的TabPage中使用Panel控件时,我有很多子控件,例如RichTextBox,Button,Label等.
While using Panel control in TabPage of Tab control, I have quite a few child controls like RichTextBox, Buttons, Labels etc.
问题是当我在面板中滚动时,内部出现了闪烁.子控件不会像已经存在那样被顺畅地显示/淹没/绘制.
Problem is when I scroll in the Panel, there is a flickering inside. The child controls are not being shown/drown/painted smoothly like they are already there.
寻找可以使滚动平滑并消除闪烁效果的东西. 任何建议都会有很大帮助.我尝试了其他几种方法,例如DoubleBuffered,但并没有真正起作用.
Looking for something that could make this scrolling smooth and remove the flickering effect. Any suggestions would help a lot. I tried a several other methods like DoubleBuffered, but didn't really work.
推荐答案
好吧,我通过结合其他不同的建议解决了这个问题,以下是为我消除了闪烁的代码,实际上是使用Win API将其变为DoubleBuffered.
Well I solved the problem with combination of different other suggestions, below is the code that removed flickering for me, essentially making it DoubleBuffered using Win API.
public partial class SmoothScrollPanel : UserControl
{
public SmoothScrollPanel()
{
InitializeComponent();
// this.DoubleBuffered = true;
}
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
protected override void WndProc(ref Message m)
{
if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
&& (((int)m.WParam & 0xFFFF) == 5))
{
// Change SB_THUMBTRACK to SB_THUMBPOSITION
m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
}
base.WndProc(ref m);
}
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
}
这篇关于滚动时在面板中平滑绘制或绘画子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!