Esempio n. 1
0
File: Canvas.cs Progetto: ash2005/z
        //-------------------------------------------------------------------
        //
        //  Public Properties + Dependency Properties's
        //
        //-------------------------------------------------------------------

        #region Public Properties

        //having this invalidate callback allows to host UIElements in Canvas and still
        //receive invalidations when Left/Top/Bottom/Right properties change -
        //registering the attached properties with AffectsParentArrange flag would be a mistake
        //because those flags only work for FrameworkElements
        private static void OnPositioningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UIElement uie = d as UIElement;

            if (uie != null)
            {
                Canvas p = VisualTreeHelper.GetParent(uie) as Canvas;
                if (p != null)
                {
                    p.InvalidateArrange();
                }
            }
        }
        /// <summary>
        /// Prints a FrameworkElement in landscape, automatically rotating element if necessary
        /// </summary>
        /// <param name="DocumentName"></param>
        /// <param name="PrintableElement"></param>
        /// <param name="footerText"></param>
        public static void PrintElement(string DocumentName, FrameworkElement PrintableElement, string footerText)
        {
            PrintDocument p = new PrintDocument();
            Panel         printableParent = PrintableElement.Parent as Panel;
            bool          okToPrint       = false;

            System.Windows.Controls.Panel tmp = new System.Windows.Controls.Canvas();

            p.BeginPrint += (object sender, BeginPrintEventArgs e) =>
            {
                ((Panel)PrintableElement.Parent).Children.Remove(PrintableElement);
                tmp.Children.Add(PrintableElement);
                tmp.Children.Add(new TextBlock
                {
                    VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
                    Text     = footerText,
                    FontSize = 8
                });
            };

            p.EndPrint += (s2, e2) =>
            {
                tmp.Children.Remove(PrintableElement);
                printableParent.Children.Add(PrintableElement);
                PrintableElement.HorizontalAlignment = HorizontalAlignment.Stretch;
                PrintableElement.VerticalAlignment   = VerticalAlignment.Stretch;
                PrintableElement.Margin = new Thickness(0);
            };


            p.PrintPage += (object s, PrintPageEventArgs e) =>
            {
                // Rotate to landscape if necessary
                if (e.PrintableArea.Height > e.PrintableArea.Width)
                {
                    double scale = e.PrintableArea.Height / e.PrintableArea.Width;
                    scale = 1.0;

                    tmp.Width  = e.PrintableArea.Height;
                    tmp.Height = e.PrintableArea.Width * scale;

                    CompositeTransform transform = new CompositeTransform
                    {
                        Rotation   = 90,
                        TranslateX = tmp.Height * scale,
                        ScaleX     = scale,
                        ScaleY     = scale
                    };
                    tmp.RenderTransform       = transform;
                    tmp.RenderTransformOrigin = new Point(0.5, 0.5);
                    PrintableElement.Margin   = new Thickness(48 / scale, 96 / scale, 48 / scale, 0);
                    PrintableElement.Width    = tmp.Width - 96 / scale;
                    PrintableElement.Height   = tmp.Height - 96 / scale;
                }
                else
                {
                    tmp.Width  = e.PrintableArea.Width;
                    tmp.Height = e.PrintableArea.Height;
                    PrintableElement.Margin = new Thickness(48, 96, 0, 0);
                    PrintableElement.Width  = tmp.Width - 96;
                    PrintableElement.Height = tmp.Height - 96;
                }
                PrintableElement.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                PrintableElement.VerticalAlignment   = System.Windows.VerticalAlignment.Stretch;
                tmp.InvalidateArrange();
                tmp.InvalidateMeasure();
                okToPrint = true;

                //tmp.SizeChanged += (s2, e2) => { okToPrint = true; };
                //PrintableElement.SizeChanged += (s2, e2) => { okToPrint = true; };

                if (okToPrint)
                {
                    e.PageVisual   = tmp;
                    e.HasMorePages = false;
                }
                else
                {
                    e.PageVisual   = null;
                    e.HasMorePages = true;
                }
            };
            p.Print(DocumentName);
        }
Esempio n. 3
0
		public void CanvasCallsLayoutTest ()
		{
			var parent = new Canvas ();
			LayoutPoker c = new LayoutPoker ();
			parent.Children.Add (c);

			c.Width = 50;
			c.Height = 50;
			
			int measure_called = 0;
			int arrange_called = 0;
			c.Measured += (Size real) => { 
				c.MeasureResult = real; 
				measure_called++;
			};
			c.Arranged += (Size real) => {
				c.ArrangeResult = real;
				arrange_called++;
			};

			parent.Measure (new Size (100, 100));
			Assert.AreEqual (0, arrange_called, "arrange called 0");
			Assert.AreEqual (1, measure_called, "measure called 0");

			Assert.AreEqual (new Size (0,0), c.DesiredSize);
			Assert.AreEqual (new Size (0,0), parent.DesiredSize);

			parent.Arrange (new Rect (0, 0, 100, 100));

			Assert.AreEqual (1, arrange_called, "arrange called 1");
			Assert.AreEqual (1, measure_called, "measure called 1");

			c.InvalidateMeasure ();
			c.InvalidateArrange ();
			parent.InvalidateMeasure ();
			parent.InvalidateArrange ();
			parent.Arrange (new Rect (0, 0, 100, 100));

			Assert.AreEqual (2, arrange_called, "arrange called 2");
			Assert.AreEqual (2, measure_called, "measure called 2");
		}