gdi+画直线去重影
部分代码
bool isok=false
point startP,endP
Pen p;
鼠标按下事件函数
startP.x=e.x
startP.y=e.y
isok=true
鼠标移到时间函数
if(isok)
{
endP.x=e.x
endP.y=e.y
Graphics g=this.CreateGraphics()
g.DrawLine(p,startP,endP);
}
鼠标松开事件函数
isok=false
这样画的结果是有重影,请问怎么去掉重影?
------解决方案-------------------- if(isok)
{
this.Refresh(); //清除以前画的线
endP.X=e.X;
endP.Y=e.Y ;
Graphics g = this.CreateGraphics();
g.DrawLine(p,startP,endP);
}
------解决方案-------------------- if (e.Button == MouseButtons.Left)
{
// 第一个 DrawReversible 为消除原来的辅助线
// 第二个 DrawReversible 画新的辅助线
if (rbLine.Checked)
{
ControlPaint.DrawReversibleLine(this.PointToScreen(ps), this.PointToScreen(pe), Color.Black);
pe = e.Location;
ControlPaint.DrawReversibleLine(this.PointToScreen(ps), this.PointToScreen(pe), Color.Black);
}
else
{
ControlPaint.DrawReversibleFrame(this.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
SelectRect.Width = e.X - SelectRect.X;
SelectRect.Height = e.Y - SelectRect.Y;
ControlPaint.DrawReversibleFrame(this.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
}
------解决方案--------------------
把你画好的线记住,在onpaint事件里将这些线画出来.
bool isok=false ;
Point startP, endP;
Pen p=Pens.Black;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startP.X=e.X ;
startP.Y=e.Y ;
isok = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isok)
{
this.Refresh();
endP.X=e.X;
endP.Y=e.Y ;
Graphics g = this.CreateGraphics();
g.DrawLine(p,startP,endP);
}
}
List<Point> sP = new List<Point>();
List<Point> eP = new List<Point>();
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isok = false;
sP.Add(startP);
eP.Add(e.Location);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < sP.Count; i++)
{
e.Graphics.DrawLine(p, sP[i],eP[i]);
}
}
------解决方案--------------------
C# code
public partial class Form1 : Form
{
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
// Called when the left mouse button is pressed.
public void MyMouseDown(Object sender, MouseEventArgs e)
{
// Make a note that we "have the mouse".
bHaveMouse = true;
// Store the "starting point" for this rubber-band rectangle.
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
// Special value lets us know that no previous
// rectangle needs to be erased.