delegate void LINEDDAPROC(int X, int Y, IntPtr lpData);
[DllImport("gdi32.dll")]
static extern int LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd,
LINEDDAPROC lpLineFunc, IntPtr lpData);
private const byte PT_CLOSEFIGURE = 1;
private const byte PT_LINETO = 2;
private const byte PT_BEZIERTO = 4;
private const byte PT_MOVETO = 6;
[DllImport("gdi32.dll")]
static extern int SetPixel(IntPtr hdc, int X, int Y, int crColor);
GraphicsPath graphicsPath = new GraphicsPath();
private int counter = 0;
private IntPtr graphicsHandle = IntPtr.Zero;
//設計Zswang 2007-04-30 wjhu111#21cn.com 尊重作者,轉貼請注明出處
private void button1_Click(object sender, EventArgs e)
{
graphicsPath.ClearMarkers();
graphicsPath.AddRectangle(new Rectangle(10, 10, 100, 100));
timer1.Interval = 100;
timer1.Enabled = true;
}
private void MovingDots(int X, int Y, IntPtr lpData)
{
counter = (counter + 1) % 15;
Color vColor;
if (counter < 5)
vColor = Color.White;
else if (counter < 12)
vColor = Color.Red;
else vColor = Color.Blue;
SetPixel(graphicsHandle, X, Y, vColor.R | vColor.G << 8 | vColor.B << 16);
}
private void timer1_Tick(object sender, EventArgs e)
{
graphicsHandle = Graphics.FromHwnd(Handle).GetHdc();
for (int i = 0; i < graphicsPath.PathPoints.Length; i++)
{
if (graphicsPath.PathTypes[i] == (byte)(PT_CLOSEFIGURE | PT_LINETO))
{
for (int j = i; j >= 0; j--)
{
if (graphicsPath.PathTypes[j] == PT_MOVETO)
{
LineDDA(
(int)graphicsPath.PathPoints[i].X,
(int)graphicsPath.PathPoints[i].Y,
(int)graphicsPath.PathPoints[j].X,
(int)graphicsPath.PathPoints[j].Y,
MovingDots, IntPtr.Zero);
break;
}
}
continue;
}
if (i == graphicsPath.PathPoints.Length - 1)
LineDDA(
(int)graphicsPath.PathPoints[i].X,
(int)graphicsPath.PathPoints[i].Y,
(int)graphicsPath.PathPoints[0].X,
(int)graphicsPath.PathPoints[0].Y,
MovingDots, IntPtr.Zero);
else
LineDDA(
(int)graphicsPath.PathPoints[i].X,
(int)graphicsPath.PathPoints[i].Y,
(int)graphicsPath.PathPoints[i + 1].X,
(int)graphicsPath.PathPoints[i + 1].Y,
MovingDots, IntPtr.Zero);
}
}