C#写的应用程序,在黑色背景下画几百个几个像素大小的圈、正方形等一些
基本图形。每过一段时间,这些图形的一部分需要更新一下。设计了几个类,每个类
表示一种图像,程序初始化生成几十个实例,更新时对每个实例更新其状态。类本身
有Display()实例函数,函数里判断对象状态有无变化,有变化则重画之,否则无动作。 不论刷新的频率有多慢,每次刷新时,总是闪得很厉害,把元件从几个类的
几百个降到只有一个类的几十个,还是闪得厉害,不知道是什么问题。 主要代码如下:
protected void UserRequestHandler(object sender, UserRequestEventArgs e)
{
switch(e.Request)
{
case RequestType.AdRequest:
Trace.WriteLine("UserRequestHandler()收到消息");
this.Refresh();
break;
default:
break;
}
}protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
PaintItems(e);
return ;
}protected void PaintItems(PaintEventArgs e)
{
Pen dPen = new Pen(Color.LightGreen,2);
Brush brush = Brushes.GreenYellow;
Graphics aGraphics = e.Graphics; // 更新状态
GenerateSwitchLamp(); // 重画
for(int i = 0; i < switches.GetLength(0); i ++)
{
if(switches[i].State == tjState.normal)
{
switches[i].Display( aGraphics, dPen );
}
}
// 其它元件
...
}
基本图形。每过一段时间,这些图形的一部分需要更新一下。设计了几个类,每个类
表示一种图像,程序初始化生成几十个实例,更新时对每个实例更新其状态。类本身
有Display()实例函数,函数里判断对象状态有无变化,有变化则重画之,否则无动作。 不论刷新的频率有多慢,每次刷新时,总是闪得很厉害,把元件从几个类的
几百个降到只有一个类的几十个,还是闪得厉害,不知道是什么问题。 主要代码如下:
protected void UserRequestHandler(object sender, UserRequestEventArgs e)
{
switch(e.Request)
{
case RequestType.AdRequest:
Trace.WriteLine("UserRequestHandler()收到消息");
this.Refresh();
break;
default:
break;
}
}protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
PaintItems(e);
return ;
}protected void PaintItems(PaintEventArgs e)
{
Pen dPen = new Pen(Color.LightGreen,2);
Brush brush = Brushes.GreenYellow;
Graphics aGraphics = e.Graphics; // 更新状态
GenerateSwitchLamp(); // 重画
for(int i = 0; i < switches.GetLength(0); i ++)
{
if(switches[i].State == tjState.normal)
{
switches[i].Display( aGraphics, dPen );
}
}
// 其它元件
...
}
SetStyle(ControlStyles.DoubleBuffer,true);
SetStyle(ControlStyles.AllPaintingInWmPaint,true);
SetStyle(ControlStyles.UserPaint,true);
{
const int WM_LBUTTONDOWN = 0x0201; switch(m.Msg)
{
case WM_LBUTTONDOWN:
this.Refresh();
break;
default:break;
}
try
{
base.WndProc(ref m);
}
catch(Exception ex)
{
Trace.WriteLine("WndProc():" + ex.ToString());
}
}