一、要点
本控件解决了CE下绘图闪烁的问题
二、源代码
namespace RealTimeGraphCtlCF
{
public partial class GraphPane : UserControl
{
private Graphics gHdc;
private XAxis xAxis;
private YAxis yAxis;
private int _xMax;
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(100)]
public int XAxisMax
{
get { return _xMax; }
set { _xMax = value; }
}
private int _yMax;
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(100)]
public int YAxisMax
{
get { return _yMax; }
set { _yMax = value; }
}
private string _xTitle = "X";
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue("X")]
public string XAxisTitle
{
get { return _xTitle; }
set { _xTitle = value; }
}
private string _yTitle = "Y";
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue("Y")]
public string YAxisTitle
{
get { return _yTitle; }
set { _yTitle = value; }
}
private int _xTicCount = 100;
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(100)]
public int XAxisTicCount
{
get { return _xTicCount; }
set { _xTicCount = value; }
}
private int _yTicCount = 100;
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(100)]
public int YAxisTicCount
{
get { return _yTicCount; }
set { _yTicCount = value; }
}
private Pen _linePen;
[EditorBrowsable(EditorBrowsableState.Never)]
public Pen LinePen
{
get { return _linePen; }
set { _linePen = value; }
}
public List<Point> PointList;
private const int ticLength = 2;
public GraphPane()
{
InitializeComponent();
this.PointList = new List<Point>();
this._linePen = new Pen(Color.Black, 1);
}
private void GraphPane_Paint(object sender, PaintEventArgs e)
{
Bitmap b = new Bitmap(this.Width, this.Height);
gHdc = Graphics.FromImage((System.Drawing.Image)b);
gHdc.FillRectangle(new SolidBrush(Color.White), this.ClientRectangle);
Point orgin = new Point(20, 20);
int yAxisLength = ClientRectangle.Height - 2 * orgin.Y;
yAxis = new YAxis(gHdc, this.ClientRectangle, orgin, yAxisLength, _yMax, _yTitle);
yAxis.Draw(_yTicCount, ticLength);
int xAxisLength = ClientRectangle.Width - 2 * orgin.X;
xAxis = new XAxis(gHdc, this.ClientRectangle, orgin, xAxisLength, _xMax, _xTitle);
xAxis.Draw(_xTicCount, ticLength);
DrawLines(gHdc);
e.Graphics.DrawImage((System.Drawing.Image)b, 0, 0);
b.Dispose();
}
public void DrawLines(Graphics gHdc)
{
Point previous = new Point();
Point current = new Point();
if (PointList.Count >= 2)
{
previous = PointList[0];
for (int i = 1; i < PointList.Count; i++)
{
current = PointList[i];
gHdc.DrawLine(LinePen, previous.X, previous.Y, current.X, current.Y);
previous = current;
}
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}
}
}
三、参考文献
http://blog.csdn.net/yefanqiu/archive/2006/11/21/1402520.aspx