C#, Hvordan opnås opdatering af GUI efter DrawLine(..) etc.
Med nedenstående kode opdateres GUI først ved overgang fra misted fokus til fokus. Hvad skal der til for øjeblikkelig opdatering?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PlotTest
{
public partial class Form1 : Form
{
private Measure theMeasure;
public Form1()
{
InitializeComponent();
theMeasure = new Measure(this.pnlGraph);
}
private void btnInterval_Click(object sender, EventArgs e)
{
float min = Convert.ToInt32(txtMin.Text);
float max = Convert.ToInt32(txtMax.Text);
theMeasure.SetY(min, max);
}
private void btnValue_Click(object sender, EventArgs e)
{
float value = Convert.ToInt32(txtValue);
theMeasure.PlotValue(value);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace PlotTest
{
class Measure
{
private float[] mValue;
private DataPlot ValuePlot;
private static int Index;
private const int MAX_ARRAY = 512;
public Measure(System.Windows.Forms.Panel p)
{
ValuePlot = new DataPlot(p, 0F, 10F);
mValue = new float[MAX_ARRAY];
Index = 0;
}
public void SetY(float min, float max)
{
ValuePlot.PlotAmplitude(min, max);
}
public void PlotValue(float value)
{
mValue[Index++] = value;
Index %= MAX_ARRAY;
ValuePlot.PlotData(Index, mValue, Color.Red);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace PlotTest
{
class DataPlot
{
Graphics plot;
Rectangle pGrid;
Color bgColor;
float start, interval;
float vmin, vmax;
int nIntervals;
public DataPlot(System.Windows.Forms.Panel p, float min, float max)
{
int x,y,w,h;
p.BackgroundImage = new Bitmap (p.ClientSize.Width, p.ClientSize.Height, PixelFormat.Format24bppRgb);
plot = Graphics.FromImage(p.BackgroundImage);
bgColor = p.BackColor;
x = Convert.ToInt32(0.1 * p.ClientSize.Width);
y = Convert.ToInt32(0.1 * p.ClientSize.Height);
w = Convert.ToInt32(0.85 * p.ClientSize.Width);
h = Convert.ToInt32(0.85 * p.ClientSize.Height);
pGrid = new Rectangle(x,y,w,h);
PlotAmplitude(min,max);
}
public void PlotAmplitude(float min, float max)
{
plot.Clear(bgColor);
plot.FillRectangle(Brushes.LightYellow, pGrid);
PlotIntervals(min, max);
DrawGrid(start, interval, nIntervals);
}
private void PlotIntervals(float min, float max)
{
if (min == max)
min = 0.9F * max;
start = min;
interval = (Math.Abs(min) + Math.Abs(max)) /10.0F;
nIntervals = 10;
}
private void DrawGrid(float start, float interval, int number)
{
int y;
string s;
SizeF theSize;
Font theFont = new Font("Arial", 8);
vmin = start;
vmax = start + number * interval;
plot.DrawRectangle(Pens.Black, pGrid);
for (int i = 0; i < number; i++)
{
y = MakeY(pGrid, start + i * interval, vmin, vmax);
plot.DrawLine(Pens.Black, pGrid.X, y, pGrid.X + pGrid.Width, y);
s = String.Format("{0:f0}", start + i * interval);
theSize = plot.MeasureString(s, theFont);
plot.DrawString(s, theFont, Brushes.Black, pGrid.X - theSize.Width, Convert.ToInt32(y - 0.5 * theSize.Height));
}
}
private int MakeY(Rectangle r, float y, float ymin, float ymax)
{
return (Convert.ToInt32(r.Y + r.Height - r.Height * ((y - ymin) / (ymax - ymin))));
}
public void PlotData(int n, float[] v, Color c)
{
Pen thePen = new Pen(c);
int prev = 0;
int x1, y1;
int x2, y2;
for (int i = 0; i < n; i++)
{
x1 = Convert.ToInt32(pGrid.X + prev * pGrid.Width / (n-1));
x2 = Convert.ToInt32(pGrid.X + i * pGrid.Width / (n-1));
y1 = MakeY(pGrid, v[prev], vmin, vmax);
y2 = MakeY(pGrid, v[i], vmin, vmax);
plot.DrawLine(thePen, x1, y1, x2, y2);
prev = i;
}
}
}
}