C# winform-.NET Compact Framework 3.5中的OnPaint
我目前正在一个智能设备项目中制作一个曲线进度条。我重写了OnPaint函数C# winform-.NET Compact Framework 3.5中的OnPaint,c#,winforms,compact-framework,onpaint,C#,Winforms,Compact Framework,Onpaint,我目前正在一个智能设备项目中制作一个曲线进度条。我重写了OnPaint函数 protected override void OnPaint(PaintEventArgs e) { gx = e.Graphics; // Draw the original curved progress bar int intPosition1 = m_NumberOfSpoke; for (int intCounter1
protected override void OnPaint(PaintEventArgs e)
{
gx = e.Graphics;
// Draw the original curved progress bar
int intPosition1 = m_NumberOfSpoke;
for (int intCounter1 = 0; intCounter1 < m_NumberOfSpoke; intCounter1++)
{
intPosition1 = intPosition1 % m_NumberOfSpoke;
DrawLine(e.Graphics,
GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition1]),
GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition1]),
Color.DarkGray, m_SpokeThickness);
intPosition1++;
}
// Draw a part of the progress bar to reflect the changing current value(such as 30%)
int intPosition = CurrentValue;
for (int intCounter1 = 0; intCounter1 < CurrentValue; intCounter1++)
{
intPosition = intPosition % CurrentValue;
DrawLine(gx,
GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
Color.Yellow, m_SpokeThickness);
intPosition++;
}
base.OnPaint(e);
}
在Override OnPaint()函数中,我用深灰色绘制了弯曲的进度条,并用黄色绘制了进度条的一部分,以反映进度条不断变化的值(例如30%)。但是,当值不断变化时,我可以看到进度条的部分颜色变为黄色。但是,弯曲的进度条本身也会重新绘制。有人知道如何避免在值更改时重新绘制原始曲线进度条吗?因此,当值发生变化时,它仅用黄色重新绘制进度条的一部分,而不是用灰色重新绘制原始进度条。下面是我在OnPaint函数中使用的代码
protected override void OnPaint(PaintEventArgs e)
{
gx = e.Graphics;
// Draw the original curved progress bar
int intPosition1 = m_NumberOfSpoke;
for (int intCounter1 = 0; intCounter1 < m_NumberOfSpoke; intCounter1++)
{
intPosition1 = intPosition1 % m_NumberOfSpoke;
DrawLine(e.Graphics,
GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition1]),
GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition1]),
Color.DarkGray, m_SpokeThickness);
intPosition1++;
}
// Draw a part of the progress bar to reflect the changing current value(such as 30%)
int intPosition = CurrentValue;
for (int intCounter1 = 0; intCounter1 < CurrentValue; intCounter1++)
{
intPosition = intPosition % CurrentValue;
DrawLine(gx,
GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
Color.Yellow, m_SpokeThickness);
intPosition++;
}
base.OnPaint(e);
}
protected override void OnPaint(PaintEventArgs e)
{
gx=e.图形;
//绘制原始曲线进度条
int intPosition1=m_个辐条;
for(int-intCounter1=0;intCounter1
我曾尝试使用Override OnBackgroundPaint绘制原始曲线进度条作为背景,以避免在OnPaint中重新绘制它,但它不起作用。加载表单时,我看不到任何内容。有什么想法吗
提前谢谢你的帮助
这是对实际屏幕的大量抽绳调用,很可能会导致闪烁。您应该通过创建一个后缓冲区来加倍缓冲区,对其执行所有绘图操作,然后通过调用
DrawBitmap
沿着以下几行将其显示到屏幕上:
protected override void OnPaint(PaintEventArgs e)
{
using(var buffer = new Bitmap(this.Width, this.Height))
using(var gx = Graphics.FromImage(buffer))
{
// for loops to draw to gx
....
e.Graphics.DrawBitmap(buffer, ...);
}
}
我也非常倾向于不做上面提到的事情,而是缓存缓冲区,以防止每次调用时产生位图的垃圾
Bitmap m_buffer;
Gramphic m_gx;
protected override void OnPaint(PaintEventArgs e)
{
if(m_buffer == null)
{
m_buffer = new Bitmap(this.Width, this.Height))
m_gx = Graphics.FromImage(buffer))
}
// clear the backbuffer with a FillRect
// for loops to draw to m_gx
....
e.Graphics.DrawBitmap(m_buffer, ...);
}
我甚至可能会更进一步,如果控件的灰色部分总是相同的,并执行“三重缓冲”,保留带有灰色绘制的图像的缓存版本,然后在OnPaint中,将其显示在图形上,绘制黄色,然后显示在屏幕上
Bitmap m_buffer;
Bitmap m_backimage;
Gramphic m_gx;
protected override void OnPaint(PaintEventArgs e)
{
if(m_buffer == null)
{
m_backimage = new Bitmap(this.Width, this.Height);
var g = Graphics.FromImage(m_backImage);
// for loop to draw the grey stuff to g
....
m_buffer = new Bitmap(this.Width, this.Height))
m_gx = Graphics.FromImage(buffer))
}
m_gx.DrawImage(m_backImage);
// for loop to draw *just the yellow* to m_gx
....
e.Graphics.DrawBitmap(m_buffer, ...);
}
您可能还需要覆盖OnResize和其他一些东西,再加上扩展Dispose来清理这些成员级GDI对象,但性能会更好,而且不会闪烁。谢谢您的帮助,Chris。最终,我解决了这个问题。请问您是否了解Compact Framework中的线程技术?因为我现在有个问题。