110,530
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
Graphics gra;
Point firstpoint;
Point secondpoint;
int s = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
gra = this.CreateGraphics();
firstpoint = new Point(0, 0);
secondpoint = new Point(0, 0);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
s = 0;
if (firstpoint.X == 0)
{
firstpoint.X = e.X;
firstpoint.Y = e.Y;
s = 1;
Pen thepen = new Pen(Color.Red, 4);
gra.DrawRectangle(thepen, firstpoint.X, firstpoint.Y, 0.5F, 0.5F);
}
if (s == 0)
{
secondpoint.X = e.X;
secondpoint.Y = e.Y;
s = 2;
Pen thepen = new Pen(Color.Red, 4);
gra.DrawRectangle(thepen, secondpoint.X, secondpoint.Y, 0.5F, 0.5F);
}
if (s == 2)
{
Pen p = new Pen(Color.Red, 1);
float[] dashValues = { 2f, 2f };
p.DashPattern = dashValues;
gra.DrawLine(p, firstpoint, secondpoint);
firstpoint.X = 0;
firstpoint.Y = 0;
secondpoint.X = 0;
secondpoint.Y = 0;
}
}
}
public partial class Form1 : Form
{
Graphics gra;
int s = 0;
private List<PointF> _list = new List<PointF>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
gra = this.CreateGraphics();
this.Cursor = Cursors.Cross;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Pen thepen = new Pen(Color.Red, 4);
if (e.Button == MouseButtons.Left)
{
_list.Add(new Point(e.X, e.Y));
if (_list.Count != 0)
{
if (_list.Count == 1)
{
gra.DrawRectangle(thepen, _list[0].X, _list[0].Y, 0.5F, 0.5F);
}
if (_list.Count > 1)
{
Pen p = new Pen(Color.Red, 1);
float[] dashValues = { 2f, 2f };
//float[] dashValues = { 5, 2, 15, 4 };
p.DashPattern = dashValues;
gra.DrawRectangle(thepen, _list[_list.Count - 1].X, _list[_list.Count - 1].Y, 0.5F, 0.5F);
gra.DrawLine(p, _list[_list.Count - 1], _list[_list.Count-2]);
this.Cursor = Cursors.Cross;
}
}
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (_list.Count != 0)
{
if (_list.Count > 2)
{
if (e.X == _list[0].X && e.Y == _list[0].Y)
{
this.Cursor = Cursors.Hand;
}
if (e.X != _list[0].X || e.Y != _list[0].Y)
{
this.Cursor = Cursors.Cross;
}
}
}
}
}
Graphics gra;
Point firstpoint;
Point secondpoint;
private void Form1_Load(object sender, EventArgs e)
{
gra = this.CreateGraphics();
firstpoint = new Point(0, 0);
secondpoint = new Point(0, 0);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstpoint.X = e.X;
firstpoint.Y = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
secondpoint.X = e.X;
secondpoint.Y = e.Y;
gra.DrawLine(Pens.Black, firstpoint, secondpoint);
}
protected override void DrawElement(Graphics g, RectangleF rect)
{
//base.DrawElement(g, rect);
g.SmoothingMode = SmoothingMode.AntiAlias;
PointF[] pts = _pts.ToArray();
if (pts.Length > 1)
{
Brush brush = GetFill();
if (brush != null)
{
g.FillPolygon(brush, pts);
}
Pen p = GetBorderPen();
if (p != null)
{
g.DrawPolygon(p, pts);
}
}
}