/// <summary>
        /// Draw the text and the focus rectangle on a CheckBox/RadioButon control.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="e"></param>
        /// <param name="textToDisplay"></param>
        public static void DrawTextAndFocusRect(ButtonBase control, PaintEventArgs e, String textToDisplay, Rectangle rectangle, int textOffset)
        {
            bool             isMultiLine = ControlUtils.GetIsMultiLine(control);
            Rectangle        textRect    = new Rectangle();
            Size             textSize    = new Size();
            ContentAlignment NewTextAli  = ContentAlignment.MiddleCenter;
            FontDescription  font        = new FontDescription(control.Font);

            if (!String.IsNullOrEmpty(textToDisplay))
            {
                textRect   = rectangle;
                NewTextAli = ControlUtils.GetOrgContentAligment(control.RightToLeft, control.TextAlign);
                textSize   = Utils.GetTextExt(control.Font, textToDisplay, control);

                ControlUtils.AlignmentInfo AlignmentInfo = ControlUtils.GetAlignmentInfo(control.TextAlign);
                int Offset = (int)((float)(BOX_WIDTH + textOffset) * Utils.GetDpiScaleRatioX(control));

                switch (AlignmentInfo.HorAlign)
                {
                case AlignmentTypeHori.Left:
                    if (control.RightToLeft == RightToLeft.No)
                    {
                        textRect.X     += Offset;
                        textRect.Width -= Offset;
                    }
                    if (control.RightToLeft == RightToLeft.Yes)
                    {
                        textRect.X -= Offset;
                    }
                    break;

                case AlignmentTypeHori.Right:
                    if (isMultiLine && control.RightToLeft == RightToLeft.No)
                    {
                        textRect.X     += Offset;
                        textRect.Width -= Offset;
                    }
                    break;

                case AlignmentTypeHori.Center:
                    if (control.RightToLeft == RightToLeft.No)
                    {
                        textRect.X     += Offset;
                        textRect.Width -= Offset;
                        // if the text is bigger then the display rect.
                        if (!isMultiLine)
                        {
                            if (textSize.Width > textRect.Width)
                            {
                                textRect.X    += (int)(((textRect.Width - textSize.Width) / 2));
                                textRect.Width = Math.Max(textRect.Width, textSize.Width);
                            }
                        }
                    }
                    else if (control.RightToLeft == RightToLeft.Yes)
                    {
                        textRect.X     -= Offset;
                        textRect.Width += Offset;
                    }
                    break;
                }

                ControlRenderer.PrintText(e.Graphics, textRect, control.ForeColor, font, textToDisplay, isMultiLine, NewTextAli,
                                          control.Enabled, true, false, true, control.RightToLeft == RightToLeft.Yes, true);
            }

            // display the focus on the text of the control
            if (control.Focused)
            {
                if (!String.IsNullOrEmpty(textToDisplay))
                {
                    if (isMultiLine)
                    {
                        textSize = GetTextSize(e, textToDisplay, font, ref textRect, isMultiLine);
                    }

                    // get the display the focus on the text of the control
                    textRect = ControlUtils.GetFocusRect(control, textRect, NewTextAli, textSize);

                    ControlPaint.DrawFocusRectangle(e.Graphics, textRect);
                }
                else
                {
                    //For CheckBox, if the text is not available, focus rect should be drawn on the Glyph.
                    if (control is MgCheckBox)
                    {
                        DrawFocusRectOnCheckBoxGlyph((MgCheckBox)control, e.Graphics);
                    }
                }
            }
        }
示例#2
0
        /// <summary>paint the push button control
        /// Can be called from :
        /// 1. push control with property 'ButtonStyle' = button
        /// 2. CheckBox control with property appearance = button
        /// 3. Radio control with property appearance = button
        /// </summary>
        /// <param name="mgButton"></param>
        /// <param name="e"></param>
        private static void DrawText(Control control, PaintEventArgs e)
        {
            Debug.Assert(control.Visible);
            bool             restoreClip = false;
            Region           r           = e.Graphics.Clip;
            bool             isImage     = false;
            ContentAlignment textAlign   = ContentAlignment.MiddleLeft;
            RightToLeft      rightToLeft = RightToLeft.No;

            String Text = ((IDisplayInfo)control).TextToDisplay;

            if (Text == null)
            {
                return;
            }

            Rectangle displayRect = control.ClientRectangle;

#if PocketPC
            if (control is MgButtonBase)
            {
                isImage     = ((MgButtonBase)control).BackgroundImage != null;
                textAlign   = ((MgButtonBase)control).TextAlign;
                rightToLeft = ((MgButtonBase)control).RightToLeft;
            }
            else if (control is MgCheckBox)
            {
                isImage     = ((MgCheckBox)control).Image != null;
                textAlign   = ((MgCheckBox)control).TextAlign;
                rightToLeft = ((MgCheckBox)control).RightToLeft;
            }
            else
            {
                Debug.Assert(false);
            }
#else
            if (control is ButtonBase)
            {
                if (control is IImageProperty)
                {
                    isImage = ((IImageProperty)control).Image != null;
                }
                else
                {
                    isImage = ((ButtonBase)control).BackgroundImage != null;
                }

                textAlign   = ((ButtonBase)control).TextAlign;
                rightToLeft = ((ButtonBase)control).RightToLeft;
            }
            else
            {
                Debug.Assert(false);
            }
#endif

            // The runtime engine sends the alignment in reverse order if rightToLeft=Yes.
            // This is because the .net framework need it to be in the reverse order when rightToLeft=Yes.
            // So, when we paint the control, we should convert it back to the original alignment.
            textAlign = ControlUtils.GetOrgContentAligment(rightToLeft, textAlign);

            if (!isImage)
            {
                //2. get the display rect
                displayRect = GetTextRect(control);
                if (control is MgPushButton)
                {
                    restoreClip = true;
                    using (Region TextRegion = new Region(displayRect))
                    {
                        e.Graphics.Clip = TextRegion;
                    }
                }
            }

            bool isMultiLine = ControlUtils.GetIsMultiLine(control);
            bool RTL         = rightToLeft == RightToLeft.Yes ? true : false;
            //4. display the text of the control

            FontDescription font = new FontDescription(control.Font);
            ControlRenderer.PrintText(e.Graphics, displayRect, control.ForeColor, font, Text, isMultiLine,
                                      textAlign, control.Enabled, false, false, false, RTL, true);
            if (restoreClip)
            {
                e.Graphics.Clip = r;
            }

            //focus rect should not be drawn for image button.
            if (!ControlUtils.IsImageButton(control))
            {
                if (control.Focused)
                {
                    displayRect.Inflate(-1, -1);
                    ControlPaint.DrawFocusRectangle(e.Graphics, displayRect);
                }
            }
        }