Print Buttons : Print « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


    public class PrintaBunchaButtons : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new PrintaBunchaButtons());
        }
        public PrintaBunchaButtons()
        {
            SizeToContent = SizeToContent.WidthAndHeight;
            ResizeMode = ResizeMode.CanMinimize;

            Button btn = new Button();
            btn.Content = "Print ...";
            btn.Click += PrintOnClick;
            Content = btn;
        }
        void PrintOnClick(object sender, RoutedEventArgs args)
        {
            PrintDialog dlg = new PrintDialog();

            if ((bool)dlg.ShowDialog().GetValueOrDefault())
            {
                Grid grid = new Grid();
                for (int i = 0; i < 5; i++)
                {
                    ColumnDefinition coldef = new ColumnDefinition();
                    coldef.Width = GridLength.Auto;
                    grid.ColumnDefinitions.Add(coldef);

                    RowDefinition rowdef = new RowDefinition();
                    rowdef.Height = GridLength.Auto;
                    grid.RowDefinitions.Add(rowdef);
                }
                grid.Background =  new LinearGradientBrush(Colors.Gray, Colors.White,
                                            new Point(0, 0), new Point(1, 1));

                for (int i = 0; i < 5; i++)
                {
                    Button btn = new Button();
                    btn.Content = "Button No. " + (i + 1);
                    btn.HorizontalAlignment = HorizontalAlignment.Center;
                    btn.VerticalAlignment = VerticalAlignment.Center;
                    btn.Margin = new Thickness(6);
                    grid.Children.Add(btn);
                    Grid.SetRow(btn, i % 5);
                    Grid.SetColumn(btn, i / 5);
                }
                grid.Measure(new Size(Double.PositiveInfinity,
                                      Double.PositiveInfinity));

                Size sizeGrid = grid.DesiredSize;

                Point ptGrid = new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
                              (dlg.PrintableAreaHeight - sizeGrid.Height) / 2);

                grid.Arrange(new Rect(ptGrid, sizeGrid));
                dlg.PrintVisual(grid, Title);
            }
        }
    }








24.158.Print
24.158.1.Print Custom PagePrint Custom Page
24.158.2.Print Scaled VisualPrint Scaled Visual
24.158.3.Print a WPF VisualPrint a WPF Visual
24.158.4.Print Without User InterventionPrint Without User Intervention
24.158.5.Print Ellipse
24.158.6.Print Buttons