bool tuodong = false; //建立一个委托
delegate void LINEDDAPROC(int X, int Y, IntPtr lpData);
//调用adi32.dll
[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; private void button1_Click(object sender, EventArgs e)
{
//清除此路径上的所有标记
graphicsPath.ClearMarkers();
//在此路径上绘出一个矩形
graphicsPath.AddRectangle(new Rectangle(10, 10, 100, 100));
//设定时间间隔为100
timer1.Interval = 100;
//引发 enabled 事件
timer1.Enabled = true;
} private void MovingDots(int X, int Y, IntPtr lpData)
{
counter = (counter + 1) % 15;
Color vColor;
if (counter < 6)
{
vColor = Color.Black;
}
else if (counter < 12)
{
vColor = Color.White;
}
else
{
vColor = Color.White;
}
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);
}
}
} //鼠标按下
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
tuodong = true;
} //鼠标拖动
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (tuodong)
{
//清除此路径上的所有标记
graphicsPath.ClearMarkers();
//在此路径上绘出一个矩形
graphicsPath.AddRectangle(new Rectangle(50,50,e.X - 50,e.Y - 50));
//设定时间间隔为100
timer1.Interval = 100;
//引发 enabled 事件
timer1.Enabled = true;
} } //鼠标释放
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
tuodong = false;
}
delegate void LINEDDAPROC(int X, int Y, IntPtr lpData);
//调用adi32.dll
[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; private void button1_Click(object sender, EventArgs e)
{
//清除此路径上的所有标记
graphicsPath.ClearMarkers();
//在此路径上绘出一个矩形
graphicsPath.AddRectangle(new Rectangle(10, 10, 100, 100));
//设定时间间隔为100
timer1.Interval = 100;
//引发 enabled 事件
timer1.Enabled = true;
} private void MovingDots(int X, int Y, IntPtr lpData)
{
counter = (counter + 1) % 15;
Color vColor;
if (counter < 6)
{
vColor = Color.Black;
}
else if (counter < 12)
{
vColor = Color.White;
}
else
{
vColor = Color.White;
}
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);
}
}
} //鼠标按下
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
tuodong = true;
} //鼠标拖动
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (tuodong)
{
//清除此路径上的所有标记
graphicsPath.ClearMarkers();
//在此路径上绘出一个矩形
graphicsPath.AddRectangle(new Rectangle(50,50,e.X - 50,e.Y - 50));
//设定时间间隔为100
timer1.Interval = 100;
//引发 enabled 事件
timer1.Enabled = true;
} } //鼠标释放
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
tuodong = false;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
Point startPoint = new Point();
Point endPoint = new Point();
bool shouldDraw = false;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void Form1_Paint(object sender, PaintEventArgs e)
{
if (!shouldDraw)
return;
Pen pen = new Pen(Color.Black);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; Point[] points = new Point[5];
points[0] = startPoint;
points[1] = new Point(endPoint.X, startPoint.Y);
points[2] = endPoint;
points[3] = new Point(startPoint.X, endPoint.Y);
points[4] = startPoint;
e.Graphics.DrawLines(pen, points);
this.Invalidate(); } private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startPoint = e.Location;
endPoint = e.Location;
shouldDraw = true;
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
endPoint = e.Location;
this.Invalidate();
} private void Form1_MouseUp(object sender, MouseEventArgs e)
{
shouldDraw = false;
}
}
}