Exemple #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (_customDrawFailed)
            {
                return;
            }

            try
            {
                int left   = ClientRectangle.Left;
                int top    = ClientRectangle.Top;
                int right  = ClientRectangle.Right;
                int bottom = ClientRectangle.Bottom;

                bool drawPressed = _pressed || (_pressing && _hot);

                string backBrushId = drawPressed
                    ? "Sidebar.Button.BackgroundPressed"
                    : "Sidebar.Button.Background";

                Brush backBrush = GUIControls.ColorScheme.GetBrush(_colorScheme, backBrushId, ClientRectangle,
                                                                   SystemBrushes.Control);
                e.Graphics.FillPath(backBrush, _borderPath);

                Pen borderPen = GUIControls.ColorScheme.GetPen(_colorScheme, "Sidebar.Button.Border",
                                                               SystemPens.ControlDark);
                e.Graphics.DrawPath(borderPen, _borderPath);

                if (_hot)
                {
                    Pen hotPen = GUIControls.ColorScheme.GetPen(_colorScheme, "Sidebar.Button.BorderHot",
                                                                Pens.Blue);
                    e.Graphics.DrawLine(hotPen, left + 1, top + 2, left + 1, bottom - 3);
                    e.Graphics.DrawLine(hotPen, left + 2, top + 1, right - 3, top + 1);
                    e.Graphics.DrawLine(hotPen, right - 2, top + 2, right - 2, bottom - 3);
                    e.Graphics.DrawLine(hotPen, left + 2, bottom - 2, right - 3, bottom - 2);
                    e.Graphics.DrawRectangle(hotPen, left + 2, top + 2, ClientRectangle.Width - 5, ClientRectangle.Height - 5);
                }
                else
                {
                    string leftPenId  = drawPressed ? "Sidebar.Button.BorderDarkPressed" : "Sidebar.Button.BorderLight";
                    string rightPenId = drawPressed ? "Sidebar.Button.BorderLight" : "Sidebar.Button.BorderDark";

                    Pen leftPen = GUIControls.ColorScheme.GetPen(_colorScheme, leftPenId, Pens.White);
                    e.Graphics.DrawLine(leftPen, left + 1, top + 2, left + 1, bottom - 3);
                    e.Graphics.DrawLine(leftPen, left + 2, top + 1, right - 3, top + 1);

                    Pen rightPen = GUIControls.ColorScheme.GetPen(_colorScheme, rightPenId, SystemPens.ControlDark);
                    e.Graphics.DrawLine(rightPen, right - 2, top + 2, right - 2, bottom - 3);
                    e.Graphics.DrawLine(rightPen, left + 2, bottom - 2, right - 3, bottom - 2);
                }

                IntPtr         hdc     = e.Graphics.GetHdc();
                IntPtr         oldFont = Win32Declarations.SelectObject(hdc, _fontHandle);
                BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

                int delta          = drawPressed ? 1 : 0;
                int topSpace       = 6;
                int iconAreaHeight = 20;

                int  maxTextHeight = ClientRectangle.Height - topSpace - iconAreaHeight - 2;
                SIZE sz            = new SIZE();
                Win32Declarations.GetTextExtentPoint32(hdc, Text, Text.Length, ref sz);

                string textToDraw = Text;
                if (sz.cx > maxTextHeight)
                {
                    // calculate how many characters fit if we leave space for the ellipsis
                    SIZE szClip = new SIZE();
                    int  charsFit;
                    Win32Declarations.GetTextExtentExPoint(hdc, Text, Text.Length, maxTextHeight - 10,
                                                           out charsFit, IntPtr.Zero, out szClip);
                    if (charsFit > 0)
                    {
                        textToDraw = Text.Substring(0, charsFit) + "...";
                    }
                    else
                    {
                        textToDraw = Text.Substring(0, 1);
                    }

                    Win32Declarations.GetTextExtentPoint32(hdc, textToDraw, textToDraw.Length, ref sz);
                }

                if (_angle == 270)
                {
                    Win32Declarations.TextOut(hdc, ClientRectangle.Right - 2 + delta, ClientRectangle.Top + topSpace + delta,
                                              textToDraw, textToDraw.Length);
                }
                else if (_angle == 90)
                {
                    Win32Declarations.TextOut(hdc, ClientRectangle.Left + 2 + delta, ClientRectangle.Top + topSpace + sz.cx + delta,
                                              textToDraw, textToDraw.Length);
                }

                Win32Declarations.SetBkMode(hdc, oldMode);
                Win32Declarations.SelectObject(hdc, oldFont);
                e.Graphics.ReleaseHdc(hdc);

                if (_icon != null)
                {
                    int iconX = (ClientRectangle.Width - 16) / 2;
                    e.Graphics.DrawIcon(_icon, ClientRectangle.Left + iconX, ClientRectangle.Bottom - iconAreaHeight + delta);
                }
            }
            catch (Exception ex)
            {
                Core.ReportBackgroundException(ex);
                _customDrawFailed = true;
            }
        }