[code=csharp]using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WinceDemo
{
public partial class Form1 : Form
{
private Point [] pointList;
Bitmap bitmap = null;
Bitmap BackBitMap = null;
private Random random;
private int XCNT;
private int nYMaxScale;
private int nYMinScale;
public int yMaxScale
{
get
{
return nYMaxScale;
}
set
{
if (value > nYMinScale)
{
nYMaxScale = value;
}
else
{
nYMaxScale = 200;
}
}
}
public int yMinScale
{
get
{
return nYMinScale;
}
set
{
if (value < nYMaxScale)
{
nYMinScale = value;
}
else
nYMinScale = 0;
}
}
public Form1()
{
InitializeComponent();
}
private void picBox_OnPaint(object sender, PaintEventArgs e)
{
Graphics gCurMap = e.Graphics;
gCurMap.Clear(this.BackColor);
if (bitmap != null)
{
gCurMap.DrawImage(bitmap, 0, 0);
bitmap.Dispose();
bitmap = null;
}
}
private void drawCurve()
{
//bitmap = new Bitmap(BackBitMap.Width, BackBitMap.Width);
bitmap = new Bitmap(BackBitMap);
/*
Graphics backMap = Graphics.FromImage(bitmap);
// backMap.Clear(bitmap.BackColor);
// backMap.DrawImage(BackBitMap, 0, 0);
// Point pSrcF;
for (int nIdx = 0; nIdx < XCNT - 1; nIdx++)
{
pointList[nIdx].X =pointList[nIdx+1].X - 10;
pointList[nIdx].Y = pointList[nIdx+1].Y;
}
Point tempPoint = new Point();
//新生成曲线图定位点的最后一个点的坐标
tempPoint.X = 640;
//曲线上每个点的纵坐标随机生成,但保证在显示区域之内
tempPoint.Y = V2Y((float)100);
pointList[XCNT - 1] = tempPoint;
Pen curverPen = new Pen(Color.Red);
backMap.DrawLines(curverPen, pointList);
backMap.Dispose();
curverPen.Dispose();
*/
}
private void DrawBack()
{
BackBitMap = new Bitmap(790, 150);
Graphics backMap = Graphics.FromImage(BackBitMap);
backMap.Clear(this.BackColor);
Pen penw = new Pen(Color.Black, 1);
Rectangle rect = new Rectangle(0, 0, PicBox.Width -1, PicBox.Height-1);
backMap.DrawRectangle(penw, rect);
int nRowSpace = 30;
int nColSpace = 79;
int nIdx = 0;
//绘制坐标主线
for (nIdx = 0; nIdx < 4; nIdx++)
{
backMap.DrawLine(penw, 0, nIdx * nRowSpace + nRowSpace, 790-1, nIdx * nRowSpace + nRowSpace);
}
for (nIdx = 0; nIdx < 10; nIdx++)
{
backMap.DrawLine(penw, nIdx * nColSpace + nColSpace, 0, nIdx * nColSpace + nColSpace, 150-1);
}
/*
//绘制坐标次线
Pen pen = new Pen(Color.FromArgb(150, 150, 150), 1);
for (nIdx = 0; nIdx < 25; nIdx++)
{
if (((nIdx % 5) != 0) && (nIdx > 0))
{
backMap.DrawLine(pen, 0, nIdx * 12, 640, nIdx * 12);
}
}
for (nIdx = 0; nIdx < 45; nIdx++)
{
if (((nIdx % 4) != 0) && (nIdx > 0))
{
backMap.DrawLine(pen, nIdx * 16, 0, nIdx * 16, 300);
}
}
pen.Dispose();*/
penw.Dispose();
backMap.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
XCNT = 790 / 10;
yMaxScale = 150;
yMinScale = 0;
pointList = new Point[XCNT];
Point tempPoint;
random = new Random();
//初始化曲线上所有的点
for (int xPos = 0; xPos < XCNT; xPos++)
{
tempPoint = new Point();
//曲线的横坐标沿x轴依次递增
tempPoint.X = xPos * 10;
//曲线的纵坐标随机生成,但保证在显示区域内
tempPoint.Y = V2Y((float)0);
pointList[xPos] = tempPoint;
}
DrawBack();
PicBox.Paint += new PaintEventHandler(this.picBox_OnPaint);
}
private int V2Y(float nYVal)
{
int nRet = 0;
nRet = (int)(150 - ((nYVal - yMinScale) / (yMaxScale - yMinScale)) * 150);
return nRet;
}
private void Tim_Refresh_Tick(object sender, EventArgs e)
{
drawCurve();
PicBox.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
Tim_Referch.Enabled = true;
}
}
}
在PC上不会有这个问题,一导入到终端设备中就会有抖动情况,定时器时间为500MS一次。
3 个解决方案
#1
现在是在内存中先新建一个bitmap,将坐标线画好,然后在画需要的曲线,并添加Picturebox的Paint事件,在Paint中将画好曲线的BitMap直接画到Picturebox上。0.5S去更新一次。
#2
C++ 下一般是使用内存 DC 双缓冲可以解决刷屏闪的问题,C#为懂。
#3
内流满面啊,终于是不闪了,不容易搞一两天,WINCE。
#1
现在是在内存中先新建一个bitmap,将坐标线画好,然后在画需要的曲线,并添加Picturebox的Paint事件,在Paint中将画好曲线的BitMap直接画到Picturebox上。0.5S去更新一次。
#2
C++ 下一般是使用内存 DC 双缓冲可以解决刷屏闪的问题,C#为懂。
#3
内流满面啊,终于是不闪了,不容易搞一两天,WINCE。