Example #1
0
        private void ToolTipExDraw(
            object sender, DrawToolTipEventArgs e)
        {
            Graphics  g      = e.Graphics;
            Rectangle bounds = e.Bounds;
            int       alpha  = (int)(_opacity * 255);

            int defaultXOffset   = 12;
            int defaultTopHeight = 36;

            int tipTextXOffset = 3;
            int tipTextYOffset = 3;

            if (Handle != IntPtr.Zero && _opacity < 1D)
            {
                IntPtr hDC = g.GetHdc();

                NativeMethods.BitBlt(
                    hDC, 0, 0, bounds.Width, bounds.Height,
                    _backDc.Hdc, 0, 0, 0xCC0020);
                g.ReleaseHdc(hDC);
            }

            Color backNormalColor = Color.FromArgb(
                alpha, this.BackColor);
            Color baseColor = Color.FromArgb(
                alpha, ColorTable.BackHover);
            Color borderColor = Color.FromArgb(
                alpha, ColorTable.Border);

            using (LinearGradientBrush brush = new LinearGradientBrush(
                       bounds,
                       backNormalColor,
                       baseColor,
                       LinearGradientMode.Vertical))
            {
                g.FillRectangle(brush, bounds);
            }

            ControlPaint.DrawBorder(
                g,
                bounds,
                borderColor,
                ButtonBorderStyle.Solid);

            Rectangle imageRect = Rectangle.Empty;
            Rectangle titleRect;
            Rectangle tipRect;

            if (base.ToolTipIcon != ToolTipIcon.None)
            {
                tipTextXOffset = defaultXOffset;
                tipTextYOffset = defaultTopHeight;

                imageRect = new Rectangle(
                    bounds.X + defaultXOffset - (ImageSize.Width - 16) / 2,
                    bounds.Y + (defaultTopHeight - _imageSize.Height) / 2,
                    _imageSize.Width,
                    _imageSize.Height);

                Image image    = _image;
                bool  bDispose = false;

                if (image == null)
                {
                    Icon icon = GetIcon();
                    if (icon != null)
                    {
                        image    = icon.ToBitmap();
                        bDispose = true;
                    }
                }

                if (image != null)
                {
                    using (InterpolationModeGraphics ig =
                               new InterpolationModeGraphics(g))
                    {
                        if (_opacity < 1D)
                        {
                            ToolTipRenderHelper.RenderAlphaImage(
                                g,
                                image,
                                imageRect,
                                (float)_opacity);
                        }
                        else
                        {
                            g.DrawImage(
                                image,
                                imageRect,
                                0,
                                0,
                                image.Width,
                                image.Height,
                                GraphicsUnit.Pixel);
                        }
                    }

                    if (bDispose)
                    {
                        image.Dispose();
                    }
                }
            }

            StringFormat sf = new StringFormat();

            sf.LineAlignment = StringAlignment.Center;

            if (!string.IsNullOrEmpty(base.ToolTipTitle))
            {
                tipTextXOffset = defaultXOffset;
                tipTextYOffset = defaultTopHeight;

                int x = imageRect.IsEmpty ?
                        defaultXOffset : imageRect.Right + 3;

                titleRect = new Rectangle(
                    x,
                    bounds.Y,
                    bounds.Width - x,
                    defaultTopHeight);

                Color foreColor = Color.FromArgb(
                    alpha, ColorTable.TitleFore);

                using (Brush brush = new SolidBrush(foreColor))
                {
                    g.DrawString(
                        base.ToolTipTitle,
                        _titleFont,
                        brush,
                        titleRect,
                        sf);
                }
            }

            if (!string.IsNullOrEmpty(e.ToolTipText))
            {
                tipRect = new Rectangle(
                    bounds.X + tipTextXOffset,
                    bounds.Y + tipTextYOffset,
                    bounds.Width - tipTextXOffset * 2,
                    bounds.Height - tipTextYOffset);

                sf = StringFormat.GenericTypographic;

                Color foreColor = Color.FromArgb(
                    alpha, ColorTable.TipFore);

                using (Brush brush = new SolidBrush(foreColor))
                {
                    g.DrawString(
                        e.ToolTipText,
                        e.Font,
                        brush,
                        tipRect,
                        sf);
                }
            }
        }
Example #2
0
        protected override void OnRenderItemImage(

            ToolStripItemImageRenderEventArgs e)

        {

            ToolStrip toolStrip = e.ToolStrip;

            Graphics g = e.Graphics;



            if (toolStrip is ToolStripDropDown &&

               e.Item is ToolStripMenuItem)

            {

                bool bDrawLogo = NeedDrawLogo(toolStrip);

                int offsetMargin = bDrawLogo ? OffsetMargin : 0;



                ToolStripMenuItem item = (ToolStripMenuItem)e.Item;

                if (item.Checked)

                {

                    return;

                }

                Rectangle rect = e.ImageRectangle;

                if (e.Item.RightToLeft == RightToLeft.Yes)

                {

                    rect.X -= offsetMargin + 2;

                }

                else

                {

                    rect.X += offsetMargin + 2;

                }



                using (InterpolationModeGraphics ig =

                    new InterpolationModeGraphics(g))

                {

                    ToolStripItemImageRenderEventArgs ne =

                        new ToolStripItemImageRenderEventArgs(

                        g, e.Item, e.Image, rect);

                    base.OnRenderItemImage(ne);

                }

            }

            else

            {

                base.OnRenderItemImage(e);

            }

        }
Example #3
0
        private void RenderImageAndText(System.Drawing.Graphics g, Rectangle captionRect)
        {
            Rectangle imageRect = Rectangle.Empty;
            Rectangle textRect = Rectangle.Empty;
            int bordWidth = base.BorderWidth;
            int imageWidth = _captionHeight - 6;

            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.NoWrap;
            sf.Trimming = StringTrimming.EllipsisCharacter;

            bool rightToLeft = base.RightToLeft == RightToLeft.Yes;

            if (rightToLeft)
            {
                sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
            }

            switch (_captionStyle)
            {
                case CaptionStyle.Top:
                case CaptionStyle.Bottom:
                    if (_image != null)
                    {
                        imageRect = new Rectangle(
                            bordWidth, captionRect.Y + 3, imageWidth, imageWidth);
                    }
                    else
                    {
                        imageRect.X = bordWidth - 3;
                    }

                    textRect = new Rectangle(
                        imageRect.Right + 3,
                        captionRect.Y,
                        captionRect.Width - (imageRect.Right + 3) - bordWidth,
                        captionRect.Height);

                    if (rightToLeft)
                    {
                        imageRect.X = captionRect.Right - imageRect.Right;
                        textRect.X = captionRect.Right - textRect.Right;
                    }

                    sf.LineAlignment = StringAlignment.Center;

                    switch (_textAlign)
                    {
                        case ContentAlignment.BottomCenter:
                        case ContentAlignment.MiddleCenter:
                        case ContentAlignment.TopCenter:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case ContentAlignment.BottomLeft:
                        case ContentAlignment.MiddleLeft:
                        case ContentAlignment.TopLeft:
                            sf.Alignment = StringAlignment.Near;
                            break;
                        case ContentAlignment.BottomRight:
                        case ContentAlignment.MiddleRight:
                        case ContentAlignment.TopRight:
                            sf.Alignment = StringAlignment.Far;
                            break;
                    }

                    if (!string.IsNullOrEmpty(base.Text))
                    {
                        using(Brush brush = new SolidBrush(ColorTable.CaptionFore))
                        {
                            g.DrawString(
                                base.Text,
                                _captionFont,
                                brush,
                                textRect,
                                sf);
                        }
                    }
                    break;
                case CaptionStyle.Left:
                    if (_image != null)
                    {
                        imageRect = new Rectangle(
                            captionRect.X + 3, captionRect.Bottom - bordWidth - imageWidth,
                            imageWidth, imageWidth);
                    }
                    else
                    {
                        imageRect.Y = captionRect.Bottom - (bordWidth - 3);
                    }

                    textRect = new Rectangle(
                        captionRect.X,
                        captionRect.Y + bordWidth,
                        captionRect.Width,
                        imageRect.Y - 3 - bordWidth);

                    if (rightToLeft)
                    {
                        imageRect.Y = captionRect.Bottom - imageRect.Bottom;
                        textRect.Y = captionRect.Bottom - textRect.Bottom;
                    }

                    sf.LineAlignment = StringAlignment.Center;

                    switch (_textAlign)
                    {
                        case ContentAlignment.BottomCenter:
                        case ContentAlignment.MiddleCenter:
                        case ContentAlignment.TopCenter:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case ContentAlignment.BottomLeft:
                        case ContentAlignment.MiddleLeft:
                        case ContentAlignment.TopLeft:
                            sf.Alignment = StringAlignment.Near;
                            break;
                        case ContentAlignment.BottomRight:
                        case ContentAlignment.MiddleRight:
                        case ContentAlignment.TopRight:
                            sf.Alignment = StringAlignment.Far;
                            break;
                    }
                    RenderHelper.RenderVerticalText(
                        g,
                        base.Text,
                        textRect,
                        _captionFont,
                        ColorTable.CaptionFore,
                        sf,
                        true);
                    break;
                case CaptionStyle.Right:
                    if (_image != null)
                    {
                        imageRect = new Rectangle(
                            captionRect.X + 3, bordWidth,
                            imageWidth, imageWidth);
                    }
                    else
                    {
                        imageRect.Y = bordWidth - 3;
                    }

                    textRect = new Rectangle(
                        captionRect.X,
                        imageRect.Bottom + 3,
                        captionRect.Width,
                        captionRect.Height - (imageRect.Bottom + 3) - bordWidth);

                    if (rightToLeft)
                    {
                        imageRect.Y = captionRect.Bottom - imageRect.Bottom;
                        textRect.Y = captionRect.Bottom - textRect.Bottom;
                    }

                    sf.LineAlignment = StringAlignment.Center;

                    switch (_textAlign)
                    {
                        case ContentAlignment.BottomCenter:
                        case ContentAlignment.MiddleCenter:
                        case ContentAlignment.TopCenter:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case ContentAlignment.BottomLeft:
                        case ContentAlignment.MiddleLeft:
                        case ContentAlignment.TopLeft:
                            sf.Alignment = StringAlignment.Near;
                            break;
                        case ContentAlignment.BottomRight:
                        case ContentAlignment.MiddleRight:
                        case ContentAlignment.TopRight:
                            sf.Alignment = StringAlignment.Far;
                            break;
                    }
                    RenderHelper.RenderVerticalText(
                       g,
                       base.Text,
                       textRect,
                       _captionFont,
                       ColorTable.CaptionFore,
                       sf,
                       false);
                    break;
            }

            if (_image != null)
            {
                using (InterpolationModeGraphics ig = new InterpolationModeGraphics(g))
                {
                    g.DrawImage(
                        _image,
                        imageRect,
                        0,
                        0,
                        _image.Width,
                        _image.Height,
                        GraphicsUnit.Pixel);
                }
            }
        }