日期:2014-05-19  浏览次数:20979 次

关于窗体控件刷新的问题,百思不得其解,请高手帮忙
我有一个WinForm窗体,比如设置了蓝色的背景色,该窗体上有一个Panel,颜色为透明,但是为该Panel设置了一个背景图片;在该Panel里面有一些Label,每个Label的背景色也都为透明;
现在的一个问题是,当窗体在进行切换并刷新时,会明显看到每个Label会先在其自己的区域里绘制为窗体的背景色,经过一小段时间后,才显示为透明,并显示Label的文本。有没有什么办法,可以阻止Label绘制那个区域,而直接显示文本,即阻止那个闪烁的过程。

我试过以下方法:
1)采用双缓冲技术:
                SetStyle(ControlStyles.UserPaint,   True)
                SetStyle(ControlStyles.AllPaintingInWmPaint,   True)   '禁止擦除背景.
                SetStyle(ControlStyles.DoubleBuffer,   True)   '   双缓冲
                UpdateStyles()

2)重写OnPaintBackground函数,让该函数留空。

但是都不能达到我想要的效果,不知各位高手有没有遇到过这方面的问题,有没有什么好的经验,不胜感激!!!

------解决方案--------------------
设置control.DoubleBuffered 属性(保护的,继承一下),这里即label
------解决方案--------------------
1。你为哪个控件设置了双缓冲?
2。你在哪个控件里写了 OnPaintBackground方法?
------解决方案--------------------
把label的parent属性设置为Panel
------解决方案--------------------
双缓冲也不避免的要晃动的,.Net特性决定了闪动的存在。

------解决方案--------------------
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace TranspControl
Public Class TranspControl
Inherits Control
' Methods
Public Sub New()
AddHandler Me.delay.Tick, New EventHandler(AddressOf Me.TimerOnTick)
Me.delay.Enabled = True
Me.delay.Interval = 50
End Sub

Protected Overrides Sub OnMove(ByVal e As EventArgs)
MyBase.RecreateHandle
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim alpha As Integer
Dim graphics As Graphics = e.Graphics
Dim rect As New Rectangle(0, 0, (MyBase.Width - 1), (MyBase.Height - 1))
Dim baseColor As Color = Me.brushColor
If (baseColor = Color.Transparent) Then
alpha = 0
Else
alpha = ((Me.opacity * &HFF) / 100)
End If
Dim pen As New Pen(Color.Black)
Dim brush As New SolidBrush(Color.FromArgb(alpha, baseColor))
graphics.FillEllipse(brush, rect)
graphics.DrawEllipse(pen, rect)
pen.Dispose
brush.Dispose
graphics.Dispose
End Sub

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
End Sub

Private Sub TimerOnTick(ByVal source As Object, ByVal e As EventArgs)
MyBase.RecreateHandle
Me.delay.Stop
End Sub


' Properties
Public Property BrushColor As Color
Get
Return Me.brushColor
End Get
Set(ByVal value As Color)
Me.brushColor = value
MyBase.RecreateHandle
End Set
End Property

Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim createParams As CreateParams = MyBase.CreateParams
createParams.ExStyle = (createParams.ExStyle Or &H20)
Return createParams