Exemplo n.º 1
0
        /// <summary>
        /// Custom Pen Drawing
        /// </summary>
        private static Drawing CreatePenDrawing(DrawingAttributes drawingAttributes, bool isHollow, bool isRightToLeft)
        {
            // Create a single point stroke.
            StylusPointCollection stylusPoints = new StylusPointCollection();
            stylusPoints.Add(new StylusPoint(0f, 0f));

            DrawingAttributes da = new DrawingAttributes();
            da.Color = drawingAttributes.Color;
            da.Width = drawingAttributes.Width;
            da.Height = drawingAttributes.Height;
            da.StylusTipTransform = drawingAttributes.StylusTipTransform;
            da.IsHighlighter = drawingAttributes.IsHighlighter;
            da.StylusTip = drawingAttributes.StylusTip;

            Stroke singleStroke = new Stroke(stylusPoints, da);
            // NTRAID#WINDOWS-1326403-2005/10/03-waynezen,
            // We should draw our cursor in the device unit since it's device dependent object.
            singleStroke.DrawingAttributes.Width = ConvertToPixel(singleStroke.DrawingAttributes.Width);
            singleStroke.DrawingAttributes.Height = ConvertToPixel(singleStroke.DrawingAttributes.Height);

            double maxLength = Math.Min(SystemParameters.PrimaryScreenWidth / 2, SystemParameters.PrimaryScreenHeight / 2);

            //
            // NOTE: there are two ways to set the width / height of a stroke
            // 1) using .Width and .Height
            // 2) using StylusTipTransform and specifying a scale
            // these two can multiply and we need to prevent the size from ever going
            // over maxLength or under 1.0.  The simplest way to check if we're too big
            // is by checking the bounds of the stroke, which takes both into account
            //
            Rect strokeBounds = singleStroke.GetBounds();
            bool outOfBounds = false;

            // Make sure that the cursor won't exceed the minimum or the maximum boundary.
            if ( DoubleUtil.LessThan(strokeBounds.Width, 1.0) )
            {
                singleStroke.DrawingAttributes.Width = 1.0;
                outOfBounds = true;
            }
            else if ( DoubleUtil.GreaterThan(strokeBounds.Width, maxLength) )
            {
                singleStroke.DrawingAttributes.Width = maxLength;
                outOfBounds = true;
            }

            if ( DoubleUtil.LessThan(strokeBounds.Height, 1.0) )
            {
                singleStroke.DrawingAttributes.Height = 1.0;
                outOfBounds = true;
            }
            else if ( DoubleUtil.GreaterThan(strokeBounds.Height, maxLength) )
            {
                singleStroke.DrawingAttributes.Height = maxLength;
                outOfBounds = true;
            }

            //drop the StylusTipTransform if we're out of bounds.  we might
            //consider trying to preserve any transform but this is such a rare
            //case (scaling over or under with a STT) that we don't care.
            if (outOfBounds)
            {
                singleStroke.DrawingAttributes.StylusTipTransform = Matrix.Identity;
            }

            if (isRightToLeft)
            {
                //reverse left to right to right to left
                Matrix xf = singleStroke.DrawingAttributes.StylusTipTransform;
                xf.Scale(-1, 1);

                //only set the xf if it has an inverse or the STT will throw
                if (xf.HasInverse)
                {
                    singleStroke.DrawingAttributes.StylusTipTransform = xf;
                }
            }

            DrawingGroup penDrawing = new DrawingGroup();
            DrawingContext dc = null;

            try
            {
                dc = penDrawing.Open();

                // Call the internal drawing method on Stroke to draw as hollow if isHollow == true
                if ( isHollow )
                {
                    singleStroke.DrawInternal(dc, singleStroke.DrawingAttributes, isHollow);
                }
                else
                {
                    // Invoke the public Draw method which will handle the Highlighter correctly.
                    singleStroke.Draw(dc, singleStroke.DrawingAttributes);
                }
            }
            finally
            {
                if ( dc != null )
                {
                    dc.Close();
                }
            }

            return penDrawing;
        }