コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StyledToolTip"/> class.
        /// </summary>
        public StyledToolTip()
            : base()
        {
            //Default settings
            this.ShowAlways    = true;
            this.OwnerDraw     = true;
            this.DisplayShadow = false;

            this.Popup += (s, e) =>
            {
                //Set the tooltip to a nice size like in VS.
                e.ToolTipSize = new Size(e.ToolTipSize.Width + 15, e.ToolTipSize.Height + 10);

                //Remove the shadow via Win32 interop (I'm disabling the DROPSHADOW-Classstyle)
                try
                {
                    if (!this.DisplayShadow && this.Handle != IntPtr.Zero)
                    {
                        HandleRef hRef    = new HandleRef(this, this.Handle);
                        IntPtr    exStyle = NativeWrappers.GetClassLongPtr(hRef, NativeConstants.GCL_STYLE);
                        if (((int)exStyle & NativeConstants.CS_DROPSHADOW) == NativeConstants.CS_DROPSHADOW)
                        {
                            exStyle = new IntPtr((int)exStyle & ~NativeConstants.CS_DROPSHADOW);
                            NativeWrappers.SetClassLongPtr(hRef, NativeConstants.GCL_STYLE, exStyle);
                        }
                    }
                }
                catch
                {
                    //If anything goes wrong here, for example because we're using this tooltip on an unsupported platform, we don't want the application to crash but just to suppress the exception.
                }
            };

            //Render the tooltip
            this.Draw += (s, e) =>
            {
                e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;                                                                                             //Make the text look nice
                e.Graphics.FillRectangle(new SolidBrush(StyleProvider.ActiveStyle.BackColor), e.Bounds);                                                                       //Draw the background
                e.Graphics.DrawRectangle(new Pen(StyleProvider.ActiveStyle.BorderColor), new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - 1, e.Bounds.Height - 1))); //Draw the border
                e.Graphics.DrawString(e.ToolTipText, e.Font, new SolidBrush(StyleProvider.ActiveStyle.ForegroundColor), e.Bounds, new StringFormat()
                {
                    Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisWord
                });                                                                                                                                                                                                                                                               //Draw the text
            };
        }