读书人

gdi+画直线去重影解决方法

发布时间: 2012-05-16 23:40:10 作者: rapoo

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.


ptLast.X = -1;
ptLast.Y = -1;
}
// Convert and normalize the points and draw the reversible frame.
private void MyDrawReversibleLine(Point p1, Point p2)
{
// Convert the points to screen coordinates.
p1 = PointToScreen(p1);
p2 = PointToScreen(p2);
ControlPaint.DrawReversibleLine(p1, p2, Color.Red);
}
// Called when the left mouse button is released.
public void MyMouseUp(Object sender, MouseEventArgs e)
{
// Set internal flag to know we no longer "have the mouse".
bHaveMouse = false;
// If we have drawn previously, draw again in that spot
// to remove the lines.
if (ptLast.X != -1)
{
Point ptCurrent = new Point(e.X, e.Y);
}

Graphics g = this.CreateGraphics();
Pen pen = new Pen(System.Drawing.Color.Black);

g.DrawLine(pen, ptOriginal, ptLast);

pen.Dispose();
g.Dispose();


// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}
// Called when the mouse is moved.
public void MyMouseMove(Object sender, MouseEventArgs e)
{
Point ptCurrent = new Point(e.X, e.Y);
// If we "have the mouse", then we draw our lines.
if (bHaveMouse)
{
// If we have drawn previously, draw again in
// that spot to remove the lines.
if (ptLast.X != -1)
{
MyDrawReversibleLine(ptOriginal, ptLast);
}
// Update last point.
ptLast = ptCurrent;
// Draw new lines.
MyDrawReversibleLine(ptOriginal, ptCurrent);
}
}
// Set up delegates for mouse events.
protected override void OnLoad(System.EventArgs e)
{
this.Width = 800;
this.Height = 600;
MouseDown += new MouseEventHandler(MyMouseDown);
MouseUp += new MouseEventHandler(MyMouseUp);
MouseMove += new MouseEventHandler(MyMouseMove);
bHaveMouse = false;
}

}

读书人网 >C#

热点推荐