Ejemplo n.º 1
0
        private Size MeasureAndDraw(Graphics g, bool enableDrawing, UI.ButtonState state, bool drawAsDefault, bool drawFocusCues, bool drawKeyboardCues)
        {
            if (enableDrawing)
            {
                g.PixelOffsetMode   = PixelOffsetMode.Half;
                g.CompositingMode   = CompositingMode.SourceOver;
                g.InterpolationMode = InterpolationMode.Bilinear;
            }

            int marginX       = UI.ScaleWidth(9);
            int marginYTop    = UI.ScaleHeight(8);
            int marginYBottom = UI.ScaleHeight(9);
            int paddingX      = UI.ScaleWidth(8);
            int paddingY      = UI.ScaleHeight(3);
            int offsetX       = 0;
            int offsetY       = 0;

            if (enableDrawing)
            {
                switch (state)
                {
                case UI.ButtonState.Disabled:
                    UI.DrawThemedButton(this, g, 0, 0, ClientSize.Width, ClientSize.Height, UI.ButtonState.Disabled);
                    break;

                case UI.ButtonState.Pressed:
                    UI.DrawThemedButton(this, g, 0, 0, ClientSize.Width, ClientSize.Height, UI.ButtonState.Pressed);
                    offsetX = 1;
                    offsetY = 1;
                    break;

                case UI.ButtonState.Hot:
                    UI.DrawThemedButton(this, g, 0, 0, ClientSize.Width, ClientSize.Height, UI.ButtonState.Hot);
                    break;

                case UI.ButtonState.Normal:
                    using (Brush backBrush = new SolidBrush(this.BackColor))
                    {
                        g.FillRectangle(backBrush, ClientRectangle);
                    }
                    break;
                }
            }

            Rectangle actionImageRect;

            Brush textBrush = new SolidBrush(SystemColors.WindowText);

            if (this.actionImage == null)
            {
                actionImageRect = new Rectangle(offsetX, offsetY + marginYTop, 0, 0);
            }
            else
            {
                actionImageRect = new Rectangle(offsetX + marginX, offsetY + marginYTop,
                                                UI.ScaleWidth(this.actionImage.Width), UI.ScaleHeight(this.actionImage.Height));

                Rectangle srcRect = new Rectangle(0, 0, this.actionImage.Width, this.actionImage.Height);

                if (enableDrawing)
                {
                    Image drawMe = Enabled ? this.actionImage : this.actionImageDisabled;
                    actionImageRect.Y += 2;
                    g.DrawImage(drawMe, actionImageRect, srcRect, GraphicsUnit.Pixel);
                    actionImageRect.Y -= 2;
                }
            }

            int actionTextX     = actionImageRect.Right + paddingX;
            int actionTextY     = actionImageRect.Top;
            int actionTextWidth = ClientSize.Width - actionTextX - marginX + offsetX;

            StringFormat stringFormat = (StringFormat)StringFormat.GenericTypographic.Clone();

            stringFormat.HotkeyPrefix = drawKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide;

            SizeF actionTextSize = g.MeasureString(this.actionText, this.actionTextFont, actionTextWidth, stringFormat);

            Rectangle actionTextRect = new Rectangle(actionTextX, actionTextY,
                                                     actionTextWidth, (int)Math.Ceiling(actionTextSize.Height));

            if (enableDrawing)
            {
                if (state == UI.ButtonState.Disabled)
                {
                    ControlPaint.DrawStringDisabled(g, this.actionText, this.actionTextFont, this.BackColor, actionTextRect, stringFormat);
                }
                else
                {
                    g.DrawString(this.actionText, this.actionTextFont, textBrush, actionTextRect, stringFormat);
                }
            }

            int descriptionTextX     = actionTextX;
            int descriptionTextY     = actionTextRect.Bottom + paddingY;
            int descriptionTextWidth = actionTextWidth;

            SizeF descriptionTextSize = g.MeasureString(this.explanationText, this.explanationTextFont,
                                                        descriptionTextWidth, stringFormat);

            Rectangle descriptionTextRect = new Rectangle(descriptionTextX, descriptionTextY,
                                                          descriptionTextWidth, (int)Math.Ceiling(descriptionTextSize.Height));

            if (enableDrawing)
            {
                if (state == UI.ButtonState.Disabled)
                {
                    ControlPaint.DrawStringDisabled(g, this.explanationText, this.explanationTextFont, this.BackColor, descriptionTextRect, stringFormat);
                }
                else
                {
                    g.DrawString(this.explanationText, this.explanationTextFont, textBrush, descriptionTextRect, stringFormat);
                }
            }

            if (enableDrawing)
            {
                if (drawFocusCues)
                {
                    ControlPaint.DrawFocusRectangle(g, new Rectangle(3, 3, ClientSize.Width - 5, ClientSize.Height - 5));
                }
            }

            if (textBrush != null)
            {
                textBrush.Dispose();
                textBrush = null;
            }

            stringFormat.Dispose();
            stringFormat = null;

            Size layoutSize = new Size(ClientSize.Width, descriptionTextRect.Bottom + marginYBottom);

            return(layoutSize);
        }