{"id":196,"date":"2020-08-15T11:22:29","date_gmt":"2020-08-15T11:22:29","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=196"},"modified":"2020-08-15T11:22:29","modified_gmt":"2020-08-15T11:22:29","slug":"customtooltip-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/15\/customtooltip-cs\/","title":{"rendered":"CustomToolTip.cs"},"content":{"rendered":"\n<p>Customer ToolTip Class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.ComponentModel;\nusing System.Drawing;\nusing System.Drawing.Drawing2D;\nusing System.Windows.Forms;\n\/\/\/ &lt;summary>\n\/\/\/     A custom tooltip class which adds some things I would like to see. I originally wrote this to have access to the\n\/\/\/     tooltip text.\n\/\/\/ &lt;\/summary>\npublic class CustomToolTip : ToolTip\n{\n    private readonly float[]    _GradientPositions;\n    private readonly ColorBlend gradientBlender;\n    private          Color      _gradientEndColor    = Color.DeepSkyBlue;\n    private          Color      _gradientMiddleColor = Color.LightSkyBlue;\n    private          Color      _gradientStartColor  = Color.LightSkyBlue;\n    public CustomToolTip()\n    {\n        Font                      =  new Font(\"Arial Narrow\", 9.75F, FontStyle.Bold, GraphicsUnit.Point, 0);\n        _GradientPositions        =  new float[3];\n        _GradientPositions[0]     =  0f;\n        _GradientPositions[1]     =  .3f;\n        _GradientPositions[2]     =  1f;\n        gradientBlender           =  new ColorBlend(3) {Positions = _GradientPositions};\n        gradientBlender.Colors[0] =  _gradientStartColor;\n        gradientBlender.Colors[1] =  _gradientMiddleColor;\n        gradientBlender.Colors[2] =  _gradientEndColor;\n        OwnerDraw                 =  true;\n        Popup                     += OnPopup;\n        Draw                      += OnDraw;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     The font used in the drawing of the text.\n    \/\/\/ &lt;\/summary>\n    [Description(\"The font used in the drawing of the text.\")]\n    public Font Font\n    {\n        get;\n        set;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     The background image used in the control.\n    \/\/\/ &lt;\/summary>\n    [Description(\"The background image used in the control.\")]\n    public Image BackgroundImage\n    {\n        get;\n        set;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the color of the border\n    \/\/\/ &lt;\/summary>\n    [Description(\"Sets the color of the border\")]\n    public Color BorderColor\n    {\n        get;\n        set;\n    } = Color.DodgerBlue;\n    \/\/\/ &lt;summary>\n    \/\/\/     Enables or disables the drawing of the border\n    \/\/\/ &lt;\/summary>\n    [Description(\"Enables or disables the drawing of the border\")]\n    public bool EnableBorder\n    {\n        get;\n        set;\n    } = true;\n    \/\/\/ &lt;summary>\n    \/\/\/     Enables or disables the drawing of the Gradient Background\n    \/\/\/ &lt;\/summary>\n    [Description(\"Enables or disables the drawing of the Gradient Background\")]\n    public bool EnableGradientBackground\n    {\n        get;\n        set;\n    } = false;\n    public string Text\n    {\n        get;\n        set;\n    } = \"\";\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the direction of the gradient\n    \/\/\/ &lt;\/summary>\n    [Description(\"Sets the direction of the gradient\")]\n    public LinearGradientMode GradientMode\n    {\n        get;\n        set;\n    } = LinearGradientMode.Vertical;\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the color of the start of the gradient\n    \/\/\/ &lt;\/summary>\n    [Description(\"Sets the color of the start of the gradient\")]\n    public Color GradientStartColor\n    {\n        get => _gradientStartColor;\n        set\n        {\n            _gradientStartColor       = value;\n            gradientBlender.Colors[0] = _gradientStartColor;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the color of the middle of the gradient\n    \/\/\/ &lt;\/summary>\n    [Description(\"Sets the color of the middle of the gradient\")]\n    public Color GradientMiddleColor\n    {\n        get => _gradientMiddleColor;\n        set\n        {\n            _gradientMiddleColor      = value;\n            gradientBlender.Colors[1] = _gradientMiddleColor;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the color of the end of the gradient\n    \/\/\/ &lt;\/summary>\n    [Description(\"Sets the color of the end of the gradient\")]\n    public Color GradientEndColor\n    {\n        get => _gradientEndColor;\n        set\n        {\n            _gradientEndColor         = value;\n            gradientBlender.Colors[2] = _gradientEndColor;\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Set or Get the lower,center,upper bounds for the gradient brush\n    \/\/\/ &lt;\/summary>\n    [Category(\"Behavior\")]\n    [Description(\"Set or Get the lower,center,upper bounds for the gradient brush\")]\n    public float[] GradientPositions\n    {\n        get => _GradientPositions;\n        set\n        {\n            _GradientPositions[0]     = value[0];\n            _GradientPositions[1]     = value[1];\n            _GradientPositions[2]     = value[2];\n            gradientBlender.Positions = _GradientPositions;\n        }\n    }\n    private void OnPopup(object sender, PopupEventArgs e)\n    {\n        var tt     = GetToolTip(e.AssociatedControl);\n        var newstr = tt.Replace(@\"\\n\", Environment.NewLine);\n        e.ToolTipSize = newstr.MeasureText(Font, 1, 1);\n    }\n    private void OnDraw(object sender, DrawToolTipEventArgs e)\n    {\n        var fs = Font.GetFontSize();\n        var g  = e.Graphics;\n        g.CompositingQuality = CompositingQuality.HighQuality;\n        g.InterpolationMode  = InterpolationMode.High;\n        Text                 = e.ToolTipText;\n        var newstr = e.ToolTipText.Replace(@\"\\n\", Environment.NewLine);\n        if (BackgroundImage == null)\n            if (EnableGradientBackground)\n            {\n                var b = new LinearGradientBrush(e.Bounds, Color.Black, Color.Black, GradientMode) {InterpolationColors = gradientBlender};\n                g.FillRectangle(b, e.Bounds);\n            }\n            else\n            {\n                g.FillRectangle(new SolidBrush(BackColor), new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));\n            }\n        else\n            g.DrawImage(new Bitmap(BackgroundImage, e.Bounds.Width, e.Bounds.Height), new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));\n        g.DrawString(newstr, Font, new SolidBrush(ForeColor), new PointF(e.Bounds.X + fs.Width \/ 2, e.Bounds.Y + fs.Height \/ 2));\n        if (EnableBorder)\n            g.DrawRectangle(new Pen(new SolidBrush(BorderColor), 1), new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1));\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Customer ToolTip Class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[5,104,105],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/196"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}