日期:2014-05-18  浏览次数:21124 次

关于Winform的屏幕刷新问题
自己做的Usercontrol,里面的内容是画的,有刷新问题,每次刷新的时候,屏幕都闪的厉害,有什么办法能让它不闪??

------解决方案--------------------
既然是画的,能不能先做成图片?然后显示?
------解决方案--------------------
在UserControl的构造函数中添加如下的一句话:
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);

------解决方案--------------------
参考这么绘制方法

'绘制窗体左、右、底部边框
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim bmp As New Bitmap(e.ClipRectangle.Width, Me.Height)
Dim g As Graphics = Graphics.FromImage(bmp)

'左边框
g.DrawImage(LeftEdge_Middle_Bitmap, 0, 0, BorderWidth, Me.Bounds.Height)
g.DrawImage(LeftEdge_Top_Bitmap, 0, 0, BorderWidth, CaptionHeight)
g.DrawImageLeftEdge_Bottom_Bitmap, 0, Me.Bounds.Height - LeftEdge_Bottom_Bitmap.Height, BorderWidth, LeftEdge_Bottom_Bitmap.Height)
'下边框
g.DrawImage(BottomEdge_Middle_Bitmap, 0, Me.Bounds.Height - BorderWidth, Me.Bounds.Width, BorderWidth)
g.DrawImage(BottomEdge_Left_Bitmap, 0, Me.Bounds.Height - BorderWidth, BottomEdge_Left_Bitmap.Width, BorderWidth)
g.DrawImage(BottomEdge_Right_Bitmap, Me.Bounds.Width - BottomEdge_Right_Bitmap.Width, Me.Bounds.Height - BorderWidth, BottomEdge_Right_Bitmap.Width, BorderWidth)
'右边框
g.DrawImage(RightEdge_Middle_Bitmap, Me.Bounds.Width - BorderWidth, 0, BorderWidth, Me.Bounds.Height)
g.DrawImage(RightEdge_Top_Bitmap, Me.Bounds.Width - BorderWidth, 0, BorderWidth, CaptionHeight)
g.DrawImage(RightEdge_Bottom_Bitmap, Me.Bounds.Width - BorderWidth, Me.Bounds.Height - RightEdge_Bottom_Bitmap.Height, BorderWidth, RightEdge_Bottom_Bitmap.Height)

e.Graphics.DrawImage(bmp, 0, 0) '窗体刷新就这一句进行绘制
g.Dispose() : bmp = Nothing
End Sub

------解决方案--------------------
使用双缓冲不能解决?
------解决方案--------------------
参考游戏制作的做法,,,,,,,,用DX SDK做它,哇卡卡
------解决方案--------------------
我这有段代码,楼主参考下:
public partial class UserControl1 : UserControl
{
private Timer timer;
private Rectangle m_Rect;
public UserControl1()
{
InitializeComponent();
timer = new Timer();
m_Rect = new Rectangle(0, 0, 100, 61);
timer.Interval = 20;
timer.Enabled = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
timer.Tick += new EventHandler(timer_Tick);
}

bool v = true;
bool h = true;
void timer_Tick(object sender, EventArgs e)
{
if (h)
{
this.m_Rect.X++;
}
else
{
this.m_Rect.X--;
}
if (v)
{
this.m_Rect.Y++;
}
else
{
this.m_Rect.Y--;
}
if (this.m_Rect.Right > this.ClientRectangle.Right)
{
h=false;
}
else if (this.m_Rect.X < this.ClientRectangle.X)
{
h = true;
}
if (this.m_Rect.Bottom > this.ClientRectangle.Bottom)
{
v = false;
}
else if (this.m_Rect.Y < this.ClientRectangle.Y)
{
v = true;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)