Encapsulate the information needed to draw text using the AccurateText class.
Inheritance: GlobalId, IDisposable
Esempio n. 1
0
        /// <summary>
        /// Pixel accurate drawing of the requested text memento information.
        /// </summary>
        /// <param name="g">Graphics object used for drawing.</param>
        /// <param name="brush">Brush for drawing text with.</param>
        /// <param name="rect">Rectangle to draw text inside.</param>
        /// <param name="rtl">Right to left setting for control.</param>
        /// <param name="orientation">Orientation for drawing text.</param>
        /// <param name="memento">Memento containing text context.</param>
        /// <param name="state">State of the source element.</param>
        /// <param name="composition">Should draw on a composition element.</param>
        /// <param name="glowing">When on composition draw with glowing.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns>True if draw succeeded; False is draw produced an error.</returns>
        public static bool DrawString(Graphics g,
                                      Brush brush,
                                      Rectangle rect,
                                      RightToLeft rtl,
                                      VisualOrientation orientation,
                                      bool composition,
                                      bool glowing,
                                      PaletteState state,
                                      AccurateTextMemento memento)
        {
            Debug.Assert(g != null);
            Debug.Assert(memento != null);

            // Cannot draw with a null graphics instance
            if (g == null)
            {
                throw new ArgumentNullException(nameof(g));
            }

            // Cannot draw with a null memento instance
            if (memento == null)
            {
                throw new ArgumentNullException(nameof(memento));
            }

            bool ret = true;

            // Is there a valid place to be drawn into
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                // Does the memento contain something to draw?
                if (!memento.IsEmpty)
                {
                    int   translateX = 0;
                    int   translateY = 0;
                    float rotation   = 0f;

                    // Perform any transformations needed for orientation
                    switch (orientation)
                    {
                    case VisualOrientation.Bottom:
                        // Translate to opposite side of origin, so the rotate can
                        // then bring it back to original position but mirror image
                        translateX = (rect.X * 2) + rect.Width;
                        translateY = (rect.Y * 2) + rect.Height;
                        rotation   = 180f;
                        break;

                    case VisualOrientation.Left:
                        // Invert the dimensions of the rectangle for drawing upwards
                        rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                        // Translate back from a quarter left turn to the original place
                        translateX = rect.X - rect.Y - 1;
                        translateY = rect.X + rect.Y + rect.Width;
                        rotation   = 270;
                        break;

                    case VisualOrientation.Right:
                        // Invert the dimensions of the rectangle for drawing upwards
                        rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                        // Translate back from a quarter right turn to the original place
                        translateX = rect.X + rect.Y + rect.Height + 1;
                        translateY = -(rect.X - rect.Y);
                        rotation   = 90f;
                        break;
                    }

                    // Apply the transforms if we have any to apply
                    if ((translateX != 0) || (translateY != 0))
                    {
                        g.TranslateTransform(translateX, translateY);
                    }

                    if (rotation != 0f)
                    {
                        g.RotateTransform(rotation);
                    }

                    try
                    {
                        if (Application.RenderWithVisualStyles && composition && glowing)
                        {
                            //DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                            //                           SystemColors.ActiveCaptionText, true);
                            if (Environment.OSVersion.Version.Major >= 10 &&
                                Environment.OSVersion.Version.Build >= 10586)
                            {
                                DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                                                           (state == PaletteState.Disabled)
                                        ? Color.FromArgb(170, 170, 170)
                                        : ContrastColor(AccentColorService.GetColorByTypeName("ImmersiveSystemAccent")),
                                                           true);
                            }
                            else
                            {
                                DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                                                           SystemColors.ActiveCaptionText, true);
                            }
                        }
                        else if (Application.RenderWithVisualStyles && composition)
                        {
                            //Check if correct in all cases
                            SolidBrush tmpBrush = brush as SolidBrush;
                            Color      tmpColor = tmpBrush?.Color ?? SystemColors.ActiveCaptionText;

                            DrawCompositionText(g, memento.Text, memento.Font, rect, state,
                                                tmpColor, true, memento.Format);
                        }
                        else
                        {
                            g.DrawString(memento.Text, memento.Font, brush, rect, memento.Format);
                        }
                    }
                    catch
                    {
                        // Ignore any error from the DrawString, usually because the display settings
                        // have changed causing Fonts to be invalid. Our controls will notice the change
                        // and refresh the fonts but sometimes the draw happens before the fonts are
                        // regenerated. Just ignore message and everything will sort itself out. Trust me!
                        ret = false;
                    }
                    finally
                    {
                        // Remove the applied transforms
                        if (rotation != 0f)
                        {
                            g.RotateTransform(-rotation);
                        }

                        if ((translateX != 0) || (translateY != 0))
                        {
                            g.TranslateTransform(-translateX, -translateY);
                        }
                    }
                }
            }

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Pixel accurate drawing of the requested text memento information.
        /// </summary>
        /// <param name="g">Graphics object used for drawing.</param>
        /// <param name="brush">Brush for drawing text with.</param>
        /// <param name="rect">Rectangle to draw text inside.</param>
        /// <param name="rtl">Right to left setting for control.</param>
        /// <param name="orientation">Orientation for drawing text.</param>
        /// <param name="memento">Memento containing text context.</param>
        /// <param name="state">State of the source element.</param>
        /// <param name="composition">Should draw on a composition element.</param>
        /// <returns>True if draw succeeded; False is draw produced an error.</returns>
        public static bool DrawString(Graphics g,
                                      Brush brush,
                                      Rectangle rect,
                                      RightToLeft rtl,
                                      VisualOrientation orientation,
                                      bool composition,
                                      PaletteState state,
                                      AccurateTextMemento memento)
        {
            Debug.Assert(g != null);
            Debug.Assert(memento != null);

            // Cannot draw with a null graphics instance
            if (g == null)
                throw new ArgumentNullException("g");

            // Cannot draw with a null memento instance
            if (memento == null)
                throw new ArgumentNullException("memento");

            bool ret = true;

            // Is there a valid place to be drawn into
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                // Does the memento contain something to draw?
                if (!memento.IsEmpty)
                {
                    int translateX = 0;
                    int translateY = 0;
                    float rotation = 0f;

                    // Perform any transformations needed for orientation
                    switch (orientation)
                    {
                        case VisualOrientation.Bottom:
                            // Translate to opposite side of origin, so the rotate can
                            // then bring it back to original position but mirror image
                            translateX = rect.X * 2 + rect.Width;
                            translateY = rect.Y * 2 + rect.Height;
                            rotation = 180f;
                            break;
                        case VisualOrientation.Left:
                            // Invert the dimensions of the rectangle for drawing upwards
                            rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                            // Translate back from a quater left turn to the original place
                            translateX = rect.X - rect.Y - 1;
                            translateY = rect.X + rect.Y + rect.Width;
                            rotation = 270;
                            break;
                        case VisualOrientation.Right:
                            // Invert the dimensions of the rectangle for drawing upwards
                            rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                            // Translate back from a quater right turn to the original place
                            translateX = rect.X + rect.Y + rect.Height + 1;
                            translateY = -(rect.X - rect.Y);
                            rotation = 90f;
                            break;
                    }

                    // Apply the transforms if we have any to apply
                    if ((translateX != 0) || (translateY != 0))
                        g.TranslateTransform(translateX, translateY);

                    if (rotation != 0f)
                        g.RotateTransform(rotation);

                    try
                    {
                        if (composition)
                            DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                                                       SystemColors.ActiveCaptionText, true);
                        else
                            g.DrawString(memento.Text, memento.Font, brush, rect, memento.Format);
                    }
                    catch
                    {
                        // Ignore any error from the DrawString, usually because the display settings
                        // have changed causing Fonts to be invalid. Our controls will notice the change
                        // and refresh the fonts but sometimes the draw happens before the fonts are
                        // regenerated. Just ignore message and everything will sort itself out. Trust me!
                        ret = false;
                    }
                    finally
                    {
                        // Remove the applied transforms
                        if (rotation != 0f)
                            g.RotateTransform(-rotation);

                        if ((translateX != 0) || (translateY != 0))
                            g.TranslateTransform(-translateX, -translateY);
                    }
                }
            }

            return ret;
        }
Esempio n. 3
0
            /// <summary>
            /// Dispose of resources.
            /// </summary>
            public void Dispose()
            {
                if (ShortTextMemento != null)
                {
                    ShortTextMemento.Dispose();
                    ShortTextMemento = null;
                }

                if (LongTextMemento != null)
                {
                    LongTextMemento.Dispose();
                    LongTextMemento = null;
                }
            }
Esempio n. 4
0
        /// <summary>
        /// Pixel accurate drawing of the requested text memento information.
        /// </summary>
        /// <param name="g">Graphics object used for drawing.</param>
        /// <param name="brush">Brush for drawing text with.</param>
        /// <param name="rect">Rectangle to draw text inside.</param>
        /// <param name="rtl">Right to left setting for control.</param>
        /// <param name="orientation">Orientation for drawing text.</param>
        /// <param name="memento">Memento containing text context.</param>
        /// <param name="state">State of the source element.</param>
        /// <param name="composition">Should draw on a composition element.</param>
        /// <returns>True if draw succeeded; False is draw produced an error.</returns>
        public static bool DrawString(Graphics g,
                                      Brush brush,
                                      Rectangle rect,
                                      RightToLeft rtl,
                                      VisualOrientation orientation,
                                      bool composition,
                                      PaletteState state,
                                      AccurateTextMemento memento)
        {
            Debug.Assert(g != null);
            Debug.Assert(memento != null);

            // Cannot draw with a null graphics instance
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }

            // Cannot draw with a null memento instance
            if (memento == null)
            {
                throw new ArgumentNullException("memento");
            }

            bool ret = true;

            // Is there a valid place to be drawn into
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                // Does the memento contain something to draw?
                if (!memento.IsEmpty)
                {
                    int   translateX = 0;
                    int   translateY = 0;
                    float rotation   = 0f;

                    // Perform any transformations needed for orientation
                    switch (orientation)
                    {
                    case VisualOrientation.Bottom:
                        // Translate to opposite side of origin, so the rotate can
                        // then bring it back to original position but mirror image
                        translateX = rect.X * 2 + rect.Width;
                        translateY = rect.Y * 2 + rect.Height;
                        rotation   = 180f;
                        break;

                    case VisualOrientation.Left:
                        // Invert the dimensions of the rectangle for drawing upwards
                        rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                        // Translate back from a quater left turn to the original place
                        translateX = rect.X - rect.Y - 1;
                        translateY = rect.X + rect.Y + rect.Width;
                        rotation   = 270;
                        break;

                    case VisualOrientation.Right:
                        // Invert the dimensions of the rectangle for drawing upwards
                        rect = new Rectangle(rect.X, rect.Y, rect.Height, rect.Width);

                        // Translate back from a quater right turn to the original place
                        translateX = rect.X + rect.Y + rect.Height + 1;
                        translateY = -(rect.X - rect.Y);
                        rotation   = 90f;
                        break;
                    }

                    // Apply the transforms if we have any to apply
                    if ((translateX != 0) || (translateY != 0))
                    {
                        g.TranslateTransform(translateX, translateY);
                    }

                    if (rotation != 0f)
                    {
                        g.RotateTransform(rotation);
                    }

                    try
                    {
                        if (composition)
                        {
                            DrawCompositionGlowingText(g, memento.Text, memento.Font, rect, state,
                                                       SystemColors.ActiveCaptionText, true);
                        }
                        else
                        {
                            g.DrawString(memento.Text, memento.Font, brush, rect, memento.Format);
                        }
                    }
                    catch
                    {
                        // Ignore any error from the DrawString, usually because the display settings
                        // have changed causing Fonts to be invalid. Our controls will notice the change
                        // and refresh the fonts but sometimes the draw happens before the fonts are
                        // regenerated. Just ignore message and everything will sort itself out. Trust me!
                        ret = false;
                    }
                    finally
                    {
                        // Remove the applied transforms
                        if (rotation != 0f)
                        {
                            g.RotateTransform(-rotation);
                        }

                        if ((translateX != 0) || (translateY != 0))
                        {
                            g.TranslateTransform(-translateX, -translateY);
                        }
                    }
                }
            }

            return(ret);
        }