110,545
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point P1 = new Point();
Point P2 = new Point();
bool isMouseDown = false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// Rectangle rect1 = new Rectangle(P1.X, P1.Y, P2.Y - P1.Y, P2.Y - P1.Y);
// Rectangle rect1 = new Rectangle(1, 1, 100, 100);
DrawTY(g, P1.X, P1.Y, P2.Y - P1.Y, P2.Y - P1.Y);
}
private void DrawTY(Graphics g, int a,int b,int c,int d )
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(a, b, c, d, 0, 360);
roundedRect.CloseFigure();
//从路径中创建区域
Region pathRegion = new Region(roundedRect);
// g.FillRegion(new SolidBrush(Color.Blue), pathRegion);
g.DrawPath(Pens.Blue, roundedRect);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (isMouseDown == false)
{
isMouseDown = true;
P1.X = e.X;
P1.Y = e.Y;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown == true)
{
P2.X = e.X;
P2.Y = e.Y;
this.Invalidate();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point P1 = new Point();
Point P2 = new Point();
bool isMouseDown = false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// g.DrawEllipse(new Pen(Color.Red, 3), 1, 1, 5, 5);
g.DrawRectangle(new Pen(Color.Blue, 3), P1.X > P2.X ? P2.X : P1.X, P1.Y > P2.Y ? P2.Y : P1.Y, Math.Abs(P2.X - P1.X), Math.Abs(P2.Y - P1.Y));
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (isMouseDown == false)
{
isMouseDown = true;
P1.X = e.X;
P1.Y = e.Y;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown == true)
{
P2.X = e.X;
P2.Y = e.Y;
this.Invalidate();
}
}
}
谢谢上面的同学,但是还有问题。
大家看我上面那一行代码,可以实现矩形的。
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown == true)
{
P2.X = e.X;
P2.Y = e.Y;
DrawTY(this.CreateGraphics(), P2.X, P2.Y, 100, 100);
this.Invalidate();
}
}
修改了下你的代码,在MouseMove中加入了Move的事件,这样才能看到圆,当然在窗体重绘过程中就有消失了,所以是闪烁的,但是并没有出现你所说的问题