【问题标题】:Draw a new Curved Shape inherited from LineShape绘制继承自 LineShape 的新曲线形状
【发布时间】:2010-12-26 17:56:57
【问题描述】:

我使用 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!");
        }
    }
}

【问题讨论】:

    标签: .net .net-2.0 drawing components powerpacks


    【解决方案1】:

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

    【讨论】:

    • 我想这应该有点复杂...查看了基本实现...
    • 使用 GraphicsPath.IsOutlineVisible 和比较粗的笔。同样的 GraphicsPath 对象也适合绘画。
    猜你喜欢
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2014-04-29
    • 2021-12-01
    • 2015-04-19
    • 2020-04-16
    • 1970-01-01
    相关资源
    最近更新 更多