How to handle the mouseUp event outside the button?

2018-10-14%2012_04_04
i click the button,and when mouseUp inside the button,the button become “off”,but if i mouseUp outside the button,the button can not become “off”,it remains “On”,it make me a few hours to try, but all is failed,so need help,thank you very much!

code:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
using Rhino;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using System.Drawing;
using Grasshopper.Kernel.Attributes;
using Grasshopper.GUI.Canvas;


namespace FabUnionRobot
{
public class AttributesCustom2 : GH_Attributes<ComponentTest2>

{
    public override bool HasInputGrip => false;

    public AttributesCustom2(ComponentTest2 owner) : base(owner)
    {

    }

    protected override void Layout()
    {
        base.Layout();
        Rectangle r = GH_Convert.ToRectangle(this.Bounds);
        r.Height = 40;
        rec2 = new RectangleF(Pivot.X + 20, Pivot.Y , this.Bounds.Width/2, 25);
        this.Bounds = r;
    }
    
    private RectangleF rec2 { get; set; }

    public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
    {
        if (((e.Button == MouseButtons.Left) && this.rec2.Contains(e.CanvasLocation)) && (this.Owner != null))
        {
            (Owner as ComponentTest2).IsBtnDown = true;
            Owner.ExpireSolution(true);
            return GH_ObjectResponse.Handled;
        }
        return base.RespondToMouseDown(sender, e);
    }
    public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
    {
        if ((Owner as ComponentTest2).IsBtnDown)
        {
            (Owner as ComponentTest2).IsBtnDown = false;
            Owner.ExpireSolution(true);
            return GH_ObjectResponse.Release;
        }
        return base.RespondToMouseUp(sender, e);
    }

    protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
    {
        
       // Font font = new Font("MS UI Gothic", 3, FontStyle.Italic);
        if (channel == GH_CanvasChannel.Objects)
        {
            GH_Capsule BigCapsule = GH_Capsule.CreateCapsule(Bounds, GH_Palette.Normal);
            
            GH_CapsuleRenderEngine.RenderOutputGrip(graphics, canvas.Viewport.Zoom, this.OutputGrip, false);
            BigCapsule.Render(graphics, this.Selected, Owner.Locked, false);

            bool isbd = (Owner as ComponentTest2).IsBtnDown;
            GH_Capsule btn_Capsule;
            if (isbd)
            {
                btn_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.Black, "On", 2, 0);
               // btn_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
            }
            else
            {
                btn_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.Black, "Off", 2, 9);
               // btn_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
            }
            
            btn_Capsule.Render(graphics, this.Selected, base.Owner.Locked, false);



            btn_Capsule.Dispose();
            BigCapsule.Dispose();
            
        }
    }
}
public class ComponentTest2 : GH_Param<IGH_Goo>
{
    /// <summary>
    /// Initializes a new instance of the ComponentTest class.
    /// </summary>
    public bool IsBtnDown { get; set; }
    public override bool IconCapableUI => true;
    public override GH_ParamData DataType => GH_ParamData.local;
    public override GH_ParamKind Kind => GH_ParamKind.floating;

    public ComponentTest2()
      : base(new GH_InstanceDescription("ComponentTest", "CT",
          "This is the Description",
          CommonValues.Test, CommonValues.subCatalogOthers))
    {
        IsBtnDown = false;
    }
    public override void CreateAttributes()
    {
        base.m_attributes = new AttributesCustom2(this);
    }

    protected override void CollectVolatileData_Custom()
    {
        if(IsBtnDown)
        {
            this.m_data.Append( GH_Convert.ToGoo(true), new Grasshopper.Kernel.Data.GH_Path(0));

        }
        else
        {
            this.m_data.Append(GH_Convert.ToGoo(false), new Grasshopper.Kernel.Data.GH_Path(0));

        }
    }




    /// <summary>
    /// Provides an Icon for the component.
    /// </summary>
    protected override System.Drawing.Bitmap Icon
    {
        get
        {
            //You can add image files to your project resources and access them like this:
            // return Resources.IconForThisComponent;
            //return null;
            return Resource1.ABC2Matrix;
        }
    }

    /// <summary>
    /// Gets the unique ID for this component. Do not change this ID after release.
    /// </summary>
    public override Guid ComponentGuid
    {
        get { return new Guid("1f8281f7-4f08-4ab3-8823-6646071ce866"); }

    }
}
}