绘制继承自 LineShape 的新 Curved Shape

发布于 2024-08-15 02:00:36 字数 2123 浏览 4 评论 0原文

我使用 Microsoft.VisualBasic。PowerPacks。 LineShape 组件。 这个组件很好,但我想画一条曲线而不是一条直线。我开始修改 OnPaint:

    protected override void OnPaint(PaintEventArgs pevent)
    {
        //base.OnPaint(pevent);            
        pevent.Graphics.DrawLines(Pens.Green, new Point[] { 
            new Point(X1, Y1), new Point(40, 10), new Point(X2, Y2)});
    }

该组件已正确绘制,但它没有检测到鼠标事件(单击、向下)。请帮助,这是我的整个测试代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private ShapeContainer shapeContainer1;

        public Form1()
        {
            this.shapeContainer1 = new PowerPacks.ShapeContainer();
            InitializeComponent();

            // add the shapeContainer1
            this.Controls.Add(this.shapeContainer1);

            // add a blue LineShape
            this.shapeContainer1.Shapes.Add(
                new LineShape(60, 10, 90, 70));
            (this.shapeContainer1.Shapes.get_Item(0) as LineShape).BorderColor = Color.Blue;

            // add a red (green in OnPaint) MyCourveShape
            this.shapeContainer1.Shapes.Add(
                new MyCourveShape(10, 10, 50, 50));
            (this.shapeContainer1.Shapes.get_Item(1) as MyCourveShape).BorderColor = Color.Red;
        }
    }

    public class MyCourveShape : Microsoft.VisualBasic.PowerPacks.LineShape
    {
        public MyCourveShape(int x1, int y1, int x2, int y2) 
        { 
            this.X1 = x1;
            this.X2 = x2;
            this.Y1 = y1;
            this.Y2 = y2; 
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            //base.OnPaint(pevent);            
            pevent.Graphics.DrawLines(Pens.Green, new Point[] { 
                new Point(X1, Y1), new Point(40, 10), new Point(X2, Y2)});
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            MessageBox.Show("MyCourveShape clicked!");
        }
    }
}

I use Microsoft.VisualBasic.PowerPacks. LineShape component.
This component is good, but I want to draw a curve instead of a right line. I stardet to modify the OnPaint:

    protected override void OnPaint(PaintEventArgs pevent)
    {
        //base.OnPaint(pevent);            
        pevent.Graphics.DrawLines(Pens.Green, new Point[] { 
            new Point(X1, Y1), new Point(40, 10), new Point(X2, Y2)});
    }

The component is correctly drawn, but it does not detect the Mouse events (click , down). Please help, this is my entire test code:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private ShapeContainer shapeContainer1;

        public Form1()
        {
            this.shapeContainer1 = new PowerPacks.ShapeContainer();
            InitializeComponent();

            // add the shapeContainer1
            this.Controls.Add(this.shapeContainer1);

            // add a blue LineShape
            this.shapeContainer1.Shapes.Add(
                new LineShape(60, 10, 90, 70));
            (this.shapeContainer1.Shapes.get_Item(0) as LineShape).BorderColor = Color.Blue;

            // add a red (green in OnPaint) MyCourveShape
            this.shapeContainer1.Shapes.Add(
                new MyCourveShape(10, 10, 50, 50));
            (this.shapeContainer1.Shapes.get_Item(1) as MyCourveShape).BorderColor = Color.Red;
        }
    }

    public class MyCourveShape : Microsoft.VisualBasic.PowerPacks.LineShape
    {
        public MyCourveShape(int x1, int y1, int x2, int y2) 
        { 
            this.X1 = x1;
            this.X2 = x2;
            this.Y1 = y1;
            this.Y2 = y2; 
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            //base.OnPaint(pevent);            
            pevent.Graphics.DrawLines(Pens.Green, new Point[] { 
                new Point(X1, Y1), new Point(40, 10), new Point(X2, Y2)});
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            MessageBox.Show("MyCourveShape clicked!");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在我安 2024-08-22 02:00:36

您还必须重写 HitTest() 方法。基本实现仍然假设它是一条直线。

You have to override the HitTest() method as well. The base implementation still assumes it is a straight line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文