Example #1
0
 public Size UpdateAndMeasure(FrameworkElement element)
 {
     element.LayoutTransform = ChildRenderTransform;
     element.UpdateLayout();
     element.Measure(new Size(BodySize.Width, double.PositiveInfinity));
     return element.DesiredSize;
 }
Example #2
0
        public void CenterScrollViewer(FrameworkElement controlToCenter = null)
        {
            if (controlToCenter == null)
            {
                HorizontalOffset = lessonContainerComponent.ScrollableWidth/2.0;
                VerticalOffset = lessonContainerComponent.ScrollableWidth/2.0;
                return;
            }

            controlToCenter.UpdateLayout();

            if (controlToCenter.ActualWidth < lessonContainerComponent.ViewportWidth)
            {
                // Horizontal center the content of the scrollviewer
                HorizontalOffset = (lessonContainerComponent.ScrollableWidth - controlToCenter.Margin.Right)/2.0;
            }
            else
            {
                // Left center the content of the scrollviewer (with 25px left-margin)
                HorizontalOffset = ((drawingAreaComponent.ActualWidth - controlToCenter.ActualWidth -
                                     controlToCenter.Margin.Right)/2.0) - 25;
            }

            if (controlToCenter.ActualHeight < lessonContainerComponent.ViewportHeight)
            {
                // Vertical center the content of the scrollviewer
                VerticalOffset = (lessonContainerComponent.ScrollableHeight - controlToCenter.Margin.Right)/2.0;
            }
            else
            {
                // Top center the content of the scrollviewer (with 25px top-margin)
                VerticalOffset = ((drawingAreaComponent.ActualHeight - controlToCenter.ActualHeight -
                                   controlToCenter.Margin.Right)/2.0) - 25;
            }
        }
Example #3
0
		public static void RealizeFrameworkElement(FrameworkElement fe) {
			var size = new Size(double.MaxValue, double.MaxValue);
			if (fe.Width > 0 && fe.Height > 0) size = new Size(fe.Width, fe.Height);
			fe.Measure(size);
			fe.Arrange(new Rect(new Point(), fe.DesiredSize));
			fe.UpdateLayout();
		}
Example #4
0
 private static void ForceRenderIfElementIsInsideAHiddenListView(FrameworkElement element)
 {
     //need this condition to force rendering for items that are non visual that we've been swapping
     //in and out of the hidden ListView.
     if (element is ListViewItem)
     {
         if (element.ActualWidth == 0.0 || element.ActualHeight == 0.0)
         {
             element.UpdateLayout();
         }
     }
 }
Example #5
0
        public static BitmapSource SaveImageSource(FrameworkElement obj, int width, int height)
        {
            // Save current canvas transform
            obj.LayoutTransform = null;
            obj.Width = width;
            obj.Height = height;
            obj.UpdateLayout();
            obj.UpdateLayout();
            // fix margin offset as well
            Thickness margin = obj.Margin;
            obj.Margin = new Thickness(0, 0,
                 margin.Right - margin.Left, margin.Bottom - margin.Top);

            // Get the size of canvas
            Size size = new Size(width, height);

            // force control to Update
            obj.Measure(size);
            obj.Arrange(new Rect(size));
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                width, height, 96, 96, PixelFormats.Pbgra32);

            bmp.Render(obj);

            //// return values as they were before
            //obj.LayoutTransform = transform;
            //obj.Margin = margin;
            obj = null;
            return bmp;
        }
Example #6
0
        public static void OpenParentRibbonGroupDropDownSync(FrameworkElement fe, bool templateApplied)
        {
            if (!templateApplied)
            {
                // Apply template if not yet applied.
                fe.ApplyTemplate();
            }

            // Get the Parent RibbonGroup and open its dropdown if needed.
            RibbonGroup ribbonGroup = TreeHelper.FindAncestor(fe, delegate(DependencyObject element) { return (element is RibbonGroup); }) as RibbonGroup;
            if (ribbonGroup == null)
            {
                ribbonGroup = TreeHelper.FindLogicalAncestor<RibbonGroup>(fe);
            }
            if (ribbonGroup != null &&
                ribbonGroup.IsCollapsed &&
                !ribbonGroup.IsDropDownOpen)
            {
                ribbonGroup.IsDropDownOpen = true;
                fe.UpdateLayout();
            }
        }
        public static Stream CopyFrameworkElementToStream(FrameworkElement copyTarget,
                                                          double width, double height, BitmapEncoder enc,
                                                          Stream outStream, double? dpi)
        {
            if (copyTarget == null)
                return outStream;

            // Store the Frameworks current layout transform, as this will be restored later
            Transform storedTransform = copyTarget.LayoutTransform;

            // Set the layout transform to unity to get the nominal width and height
            copyTarget.LayoutTransform = new ScaleTransform(1, 1);
            copyTarget.UpdateLayout();

            double baseHeight = copyTarget.ActualHeight;
            double baseWidth = copyTarget.ActualWidth;

            // Now scale the layout to fit the bitmap
            copyTarget.LayoutTransform =
                new ScaleTransform(baseWidth/width, baseHeight/height);
            copyTarget.UpdateLayout();

            // Render to a Bitmap Source, the DPI is changed for the 
            // render target as a way of scaling the FrameworkElement
            var rtb = new RenderTargetBitmap(
                (int) width,
                (int) height,
                96d*width/baseWidth,
                96d*height/baseHeight,
                PixelFormats.Default);

            rtb.Render(copyTarget);

            // Convert from a WinFX Bitmap Source to a Win32 Bitamp

            if (dpi == null)
            {
                enc.Frames.Add(BitmapFrame.Create(rtb));
            }
            else
            {
                // Change the DPI

                var brush = new ImageBrush(BitmapFrame.Create(rtb));

                var rtbDpi = new RenderTargetBitmap(
                    (int) width, // PixelWidth
                    (int) height, // PixelHeight
                    (double) dpi, // DpiX
                    (double) dpi, // DpiY
                    PixelFormats.Default);

                var drawVisual = new DrawingVisual();
                using (DrawingContext dc = drawVisual.RenderOpen())
                {
                    dc.DrawRectangle(brush, null,
                                     new Rect(0, 0, rtbDpi.Width, rtbDpi.Height));
                }

                rtbDpi.Render(drawVisual);

                enc.Frames.Add(BitmapFrame.Create(rtbDpi));
            }
            enc.Save(outStream);
            // Restore the Framework Element to it's previous state
            copyTarget.LayoutTransform = storedTransform;
            copyTarget.UpdateLayout();

            return outStream;
        }
    /// <summary>
    /// Note RenderTargetBitmap has a huge memory leak that was fixed in .NET 4.0
    /// but only if you reference Winforms.
    /// </summary>
    /// <param name="copyTarget"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="enc"></param>
    /// <param name="outStream"></param>
    /// <param name="dpi"></param>
    /// <returns></returns>
    public static Stream CopyFrameworkElementToStreamNew(FrameworkElement copyTarget,
      double width, double height, BitmapEncoder enc, Stream outStream, double dpi) {

      // Store the Frameworks current layout transform, as this will be restored later
      Transform storedLayoutTransform = copyTarget.LayoutTransform;
      Transform storedRenderTransform = copyTarget.RenderTransform;

      double scale = dpi / 96.0;
      width *= scale;
      height *= scale;
      copyTarget.RenderTransform = new ScaleTransform(scale, scale);

      // Set the layout transform to unity to get the nominal width and height
      copyTarget.LayoutTransform = new ScaleTransform(1, 1);
      copyTarget.UpdateLayout();

      RenderTargetBitmap rtb;

      copyTarget.LayoutTransform =
        new ScaleTransform(1.0 / scale, 1.0 / scale);
      copyTarget.UpdateLayout();
      // Render to a Bitmap Source, note that the DPI is changed for the 
      // render target as a way of scaling the FrameworkElement
      rtb = new RenderTargetBitmap(
        (int)width,
        (int)height,
        dpi,
        dpi,
        PixelFormats.Default);

      rtb.Render(copyTarget);

      // Convert from a WinFX Bitmap Source to a Win32 Bitamp

      enc.Frames.Add(BitmapFrame.Create(rtb));

      enc.Save(outStream);
      // Restore the Framework Element to it's previous state
      copyTarget.LayoutTransform = storedLayoutTransform;
      copyTarget.RenderTransform = storedRenderTransform;

      copyTarget.UpdateLayout();

      return outStream;
    }
Example #9
0
		/// <summary>
		/// Copies a framework element to a bitmap stored in a memory stream
		/// </summary>
		/// <param name="copyTarget"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
        /// <param name="enc"></param>
        /// <param name="str"></param>
		/// <returns></returns>
		public static void CopyFrameworkElementToStream(FrameworkElement copyTarget, double width, double height, BitmapEncoder enc, Stream str)
		{
			// Store the Frameworks current layout transform, as this will be restored later
			Transform storedTransform = copyTarget.LayoutTransform;

			// Set the layout transform to unity to get the nominal width and height
			copyTarget.LayoutTransform = new ScaleTransform(1, 1);
			copyTarget.UpdateLayout();

			double baseHeight = copyTarget.ActualHeight;
			double baseWidth = copyTarget.ActualWidth;

			// Now scale the layout to fit the bitmap
			copyTarget.LayoutTransform =
				new ScaleTransform(baseWidth / width, baseHeight / height);
			copyTarget.UpdateLayout();

			// Render to a Bitmap Source, note that the DPI is changed for the 
			// render target as a way of scaling the FrameworkElement
			RenderTargetBitmap rtb = new RenderTargetBitmap(
				(int)width,
				(int)height,
				96d * width / baseWidth,
				96d * height / baseHeight,
				PixelFormats.Default);

			rtb.Render(copyTarget);

			// Convert from a WPF Bitmap Source to a Win32 Bitamp
			enc.Frames.Add(BitmapFrame.Create(rtb));
			enc.Save(str);
			// Restore the Framework Element to it's previous state
			copyTarget.LayoutTransform = storedTransform;
			copyTarget.UpdateLayout();
		}
        private void AttachTreeControlToParent(FrameworkElement control, FrameworkElement parent)
        {
            if(parent != null)
            {
                Line line = new Line() { StrokeThickness = 2.5, Stroke = Brushes.Black, SnapsToDevicePixels = true };
                line.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
                m_canvas_messageTree.Children.Add(line);

                parent.LayoutUpdated += (x, y) =>
                    {
                        line.Y1 = Canvas.GetTop(parent) + parent.ActualHeight;
                        line.X1 = Canvas.GetLeft(parent) + (parent.ActualWidth / 2);
                    };
                parent.UpdateLayout();

                control.LayoutUpdated += (x, y) =>
                {
                    line.Y2 = Canvas.GetTop(control);
                    line.X2 = Canvas.GetLeft(control) + (control.ActualWidth / 2);
                };
                control.UpdateLayout();
            }
        }
Example #11
0
 private static void PrepareForExport(FrameworkElement element)
 {
     if (element.ActualWidth == 0 && element.ActualHeight == 0)
     {
         double width = element.Width > 0 ? element.Width : 500;
         double height = element.Height > 0 ? element.Height : 300;
         element.Measure(Size.Empty);
         element.Measure(new Size(width, height));
         element.Arrange(new Rect(0, 0, width, height));
         element.UpdateLayout();
     }
 }
Example #12
0
 static void SetBlackImage(FrameworkElement element)
 {
     if (element is Panel)
     {
         var shapes = ((Panel)element).Children.OfType<System.Windows.Shapes.Shape>();
         var blackBrush = new SolidColorBrush(Colors.Black);
         foreach (var shape in shapes)
         {
             shape.Fill = blackBrush;
         }
         element.UpdateLayout();
     }
 }
Example #13
0
        public static BitmapImage GenerateImage(FrameworkElement pFE, int pWidth, int pHeight)
        {;
            pFE.Width = pWidth;
            pFE.Height = pHeight;
            System.Windows.Size theTargetSize = new System.Windows.Size(pWidth, pHeight);
            pFE.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
            pFE.Arrange(new Rect(theTargetSize));
            // to affect the changes in the UI, you must call this method at the end to apply the new changes

            pFE.UpdateLayout();
            pFE.InvalidateMeasure();
            pFE.InvalidateArrange();
            pFE.InvalidateVisual();
            var imgEncoder = new PngBitmapEncoder();
            imgEncoder.Interlace = PngInterlaceOption.Off;
            RenderTargetBitmap bmpSource = new RenderTargetBitmap((int)pWidth, (int)pHeight, 96, 96, PixelFormats.Pbgra32);
            bmpSource.Render(pFE);
            imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
            BitmapImage result = new BitmapImage();
            using (var stream = new MemoryStream())
            {
                imgEncoder.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);

                
                
                result.BeginInit();
                // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
                // Force the bitmap to load right now so we can dispose the stream.
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
              //  result.Freeze();
                
            }

            return result;


        }
    private bool BringIntoViewScrollingCell( Cell cell, FrameworkElement container, RequestBringIntoViewEventArgs e )
    {
      Debug.Assert( cell != null );
      Debug.Assert( container != null );

      // The cell must be measured/arranged for the BringIntoView to correctly reacts
      if( this.ForceScrollingCellToLayout( cell ) )
      {
        container.UpdateLayout();
      }

      var fixedWidth = this.GetFixedWidth();
      var scrollingPanel = this.ScrollingCellsDecorator;
      var scrollingArea = new Rect( scrollingPanel.TranslatePoint( FixedCellPanel.EmptyPoint, this ),
                                    new Size( this.ParentScrollViewer.ViewportWidth - fixedWidth, scrollingPanel.ActualHeight ) );
      var cellArea = new Rect( cell.TranslatePoint( FixedCellPanel.EmptyPoint, this ),
                               new Size( cell.ParentColumn.ActualWidth, cell.ActualHeight ) );

      // The cell is larger than the scrolling area.
      if( cellArea.Width > scrollingArea.Width )
      {
        var targetObject = e.TargetObject as UIElement;
        var targetRect = e.TargetRect;

        // Try to narrow the area within the cell that we clearly want to bring into view.
        if( ( targetObject != null ) && ( targetObject != cell || !targetRect.IsEmpty ) )
        {
          Debug.Assert( targetObject.IsDescendantOf( cell ) );

          if( targetRect.IsEmpty )
          {
            targetRect = new Rect( new Point( 0d, 0d ), targetObject.RenderSize );
          }

          if( targetRect.Width <= scrollingArea.Width )
          {
            var offset = targetObject.TranslatePoint( FixedCellPanel.EmptyPoint, cell );
            var location = cellArea.Location;
            location.Offset( offset.X, offset.Y );

            cellArea.Location = location;
            cellArea.Size = targetRect.Size;
          }
        }
      }

      if( ( cellArea.Left <= scrollingArea.Left ) && ( cellArea.Right >= scrollingArea.Right ) )
      {
        // Ensure to bring the container into view vertically.
        container.BringIntoView();
      }
      else if( cellArea.Left < scrollingArea.Left )
      {
        // The ScrollViewer's extent width includes the fixed section.  We must offset the target area or the cell
        // will come into view under the fixed panel.
        cellArea.X -= fixedWidth;

        this.BringIntoView( cellArea );
      }
      else if( cellArea.Right > scrollingArea.Right )
      {
        this.BringIntoView( cellArea );
      }
      // The cell is fully visible.
      else
      {
        // Ensure to bring the container into view vertically.
        container.BringIntoView();
      }

      return true;
    }
Example #15
0
 public void CallUpdateLayout(FrameworkElement el)
 {
     el.UpdateLayout();
 }
        private FrameworkElement SetInitialPosition(FrameworkElement control)
        {
            FrameworkElement lastControl = m_canvas_messageTree.Children.OfType<UserControl>().LastOrDefault();
            double top = 0.0,
                   left = 0.0;
            if(lastControl != null)
            {
                lastControl.UpdateLayout();
                top = Canvas.GetTop(lastControl) + lastControl.ActualHeight + 50.0;
                left = Canvas.GetLeft(lastControl);
            }
            Canvas.SetTop(control, top);
            Canvas.SetLeft(control, left);
            control.UpdateLayout();

            return control;
        }
Example #17
0
        internal void BeginEdit(FrameworkElement editingElement, RoutedEventArgs e)
        {
            // This call is to ensure that the tree and its bindings have resolved
            // before we proceed to code that relies on the tree being ready.
            if (editingElement != null)
            {
                editingElement.UpdateLayout();

                object originalValue = PrepareCellForEdit(editingElement, e);
                SetOriginalValue(editingElement, originalValue);
            }
        }
Example #18
0
        void animate(FrameworkElement fm, double ms)
        {
            TranslateTransform trans = new TranslateTransform();
            trans.X = 0;
            double maxHeight = 0;
            if (PlacementTarget != null)
            {
                OverlayAdornerDecorator overlayDecorator = OverlayAdornerDecorator.FindOverlayAdornerDecorator(PlacementTarget);
                if (overlayDecorator!= null)
                    maxHeight = overlayDecorator.ActualHeight;
            }
            if (!Double.IsNaN(MaxHeight) && !Double.IsInfinity(MaxHeight))
            {
                maxHeight = MaxHeight;
            }

            if (Placement == PlacementMode.Top)
            {
                trans.Y = maxHeight;
            }
            else
            {
                trans.Y = -maxHeight;
            }

            DoubleAnimation openAnimation = new DoubleAnimation();
            openAnimation.Duration = TimeSpan.FromMilliseconds(ms * 0.5);

            openAnimation.To = 0;
            openAnimation.DecelerationRatio = 0.3;
            openAnimation.FillBehavior = FillBehavior.HoldEnd;
            // need to update because the glass Control needs the background before we start the animation

            fm.UpdateLayout();
            fm.RenderTransform = trans;
            this.UpdatePlacement(fm.DesiredSize);

            trans.BeginAnimation(TranslateTransform.YProperty, openAnimation);
        }