如何画一个可选择的线路? [英] How to draw a selectable line?
问题描述
我想创建一个应用程序,用户能够操纵他划清界线。喜欢的东西删除线或选择它。我应该怎么办呢?
在此先感谢
我设法使用硬编码的矩形做到这一点。但我不还是有一个想法如何使用的drawLine()
我可以使用 drawPath
做的去做?点击测试
下面是代码:
私人布尔selectGraph = FALSE;
私人矩形myrec =新的Rectangle(50,50,100,100);
私有图形克;
私人无效panel1_Paint(对象发件人,PaintEventArgs的E)
{
SolidBrush SB =新SolidBrush(Color.Blue);
笔P =新朋(Color.Blue,5);
e.Graphics.DrawRectangle(P,myrec);
e.Graphics.FillRectangle(SB,myrec);
}
私人无效panel1_MouseUp(对象发件人,MouseEventArgs E)
{
点MPT =新的点(e.X,e.Y);
如果(e.Button == MouseButtons.Left)
{
如果(myrec.Contains(MPT))
{
selectGraph = TRUE;
button1.Enabled = TRUE;
}
,否则
{
selectGraph = FALSE;
button1.Enabled = FALSE;
}
}
的Invalidate();
}
好吧,你可以用的东西开始像一个简单的行
类:
公共类线
{
公共点开始{搞定;组; }
公共点结束{搞定;组; }
}
然后,你可以有你的方式:
专用线线=新线();
保护覆盖无效的OnPaint(PaintEventArgs的E)
{
e.Graphics.DrawLine(Pens.Red,this.Line.Start,this.Line.End);
}
保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
如果(e.Button == MouseButtons.Left)
{
this.Line.Start = e.Location;
this.Refresh();
}
,否则如果(e.Button == MouseButtons.Right)
{
this.Line.End = e.Location;
this.Refresh();
}
}
所以基本上然后他们可以删除 this.Line
也许对MiddleButton点击什么的。这应该足以让你开始。
我已经的创建了一个示例,显示如何可以做到这一点。设置一些破发点,看看事情是如何完成的。
I want to create an application which the user is able to manipulate the line he draw. Something like Deleting the line or Selecting it. How should I do that?
Thanks in advance
I managed to do it using a hard coded rectangle. But I don't still have an idea how to do it using the drawLine()
Can I use drawPath
to do the hit test?
Here is the code:
private bool selectGraph = false;
private Rectangle myrec = new Rectangle(50, 50, 100, 100);
private Graphics g;
private void panel1_Paint(object sender, PaintEventArgs e)
{
SolidBrush sb = new SolidBrush(Color.Blue);
Pen p = new Pen(Color.Blue, 5);
e.Graphics.DrawRectangle(p, myrec);
e.Graphics.FillRectangle(sb, myrec);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Point mPT = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
if (myrec.Contains(mPT))
{
selectGraph = true;
button1.Enabled = true;
}
else
{
selectGraph = false;
button1.Enabled = false;
}
}
Invalidate();
}
Well you could start with something like a simple Line
class:
public class Line
{
public Point Start { get; set; }
public Point End { get; set; }
}
Then you could have your form:
private Line Line = new Line();
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, this.Line.Start, this.Line.End);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Line.Start = e.Location;
this.Refresh();
}
else if (e.Button == MouseButtons.Right)
{
this.Line.End = e.Location;
this.Refresh();
}
}
So basically then they could delete the this.Line
maybe on "MiddleButton" click or something. This should be enough to get you started.
I've created a sample that shows how this can be done. Set some break points and see how things are done.
这篇关于如何画一个可选择的线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!