开发者

Draw a new Curved Shape inherited from LineShape

开发者 https://www.devze.com 2022-12-14 05:04 出处:网络
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:

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));
      开发者_Python百科      (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!");
        }
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消