这个是我的鼠标事件,已经算是平滑了,但是效率不高。
我需要能像WIN 7或者XP 系统自带的画板上面的画笔一样,画出来的线条是平滑的,而且就算你鼠标移动的再快也能画出这个曲线,而不会分开。我试过用用DrawLine画,但是宽度设置的大的话会出现很难看的线。
求解,求帮助,3Q。
我的代码如下,谁能帮我改进下?
private bool isMouseDown = false; private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
old = e.Location;
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
} Point old;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Graphics g = pictureBox1.CreateGraphics();
Rectangle rec = new Rectangle();
Point A = e.Location;
Point B = old;
if (A.X ==B.X)
{
while (A.Y != B.Y)
{
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(A, new Size(20, 20)));
A.Y = (A.Y > B.Y ? A.Y - 1 : A.Y < B.Y ? A.Y + 1 : A.Y);
}
}
else
{
float k = (float)(A.Y - B.Y) / (A.X - B.X);
float b = (A.Y + B.Y - (A.X + B.X) * k) / 2;
do
{ A.Y = (int)(k * A.X + b);
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(A, new Size(20, 20)));
if (A.X > B.X)
{
A.X--;
}
else if (A.X < B.X)
{
A.X++;
}
} while (A.X != B.X);
}
old = e.Location;
g.Dispose();
}
}
我需要能像WIN 7或者XP 系统自带的画板上面的画笔一样,画出来的线条是平滑的,而且就算你鼠标移动的再快也能画出这个曲线,而不会分开。我试过用用DrawLine画,但是宽度设置的大的话会出现很难看的线。
求解,求帮助,3Q。
我的代码如下,谁能帮我改进下?
private bool isMouseDown = false; private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
old = e.Location;
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
} Point old;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Graphics g = pictureBox1.CreateGraphics();
Rectangle rec = new Rectangle();
Point A = e.Location;
Point B = old;
if (A.X ==B.X)
{
while (A.Y != B.Y)
{
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(A, new Size(20, 20)));
A.Y = (A.Y > B.Y ? A.Y - 1 : A.Y < B.Y ? A.Y + 1 : A.Y);
}
}
else
{
float k = (float)(A.Y - B.Y) / (A.X - B.X);
float b = (A.Y + B.Y - (A.X + B.X) * k) / 2;
do
{ A.Y = (int)(k * A.X + b);
g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(A, new Size(20, 20)));
if (A.X > B.X)
{
A.X--;
}
else if (A.X < B.X)
{
A.X++;
}
} while (A.X != B.X);
}
old = e.Location;
g.Dispose();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货