中动态生成的打印网格

中动态生成的打印网格

本文介绍了在 wpf 中动态生成的打印网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个动态生成的网格.

i want to print a grid which is generated dynamically.

意味着,在打印按钮的单击事件中,我正在生成一个网格,然后我想打印该网格.

Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.

这是我的代码,

private void btnPrint_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
        if (Objprint.ShowDialog() == true)
        {

            System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.PrintTicket);

            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);

            #region "Make a grid For Printing"
            Grid objgrid = new Grid();
            objgrid.Name = "GridForPrinting";
            objgrid.Width = 1000;
            objgrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objgrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;

            objgrid.RowDefinitions.Add(new RowDefinition());

            TextBlock objtext = new TextBlock();
            objtext.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objtext.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            objtext.Text = "SUPERIOR COURT OF CALIF COUNTY OF SAN BERNARDINO";
            Grid.SetRow(objtext, 0);
            objgrid.Children.Add(objtext);

            #endregion

            Objprint.PrintVisual(objgrid, "Case Summary");
        }
    }

这段代码给我空白页打印.

this code give me blank page to print.

我该怎么做?

推荐答案

我在这里得到答案,从下面的代码中我得到了我想要做的......

here i get answer,from the below code i get what i want to do...

void PrintOnClick(object sender, RoutedEventArgs args)
    {
        PrintDialog dlg = new PrintDialog();

        if ((bool)dlg.ShowDialog().GetValueOrDefault())
        {
            // Create Grid panel.
            Grid grid = new Grid();

            // Define 5 auto-sized rows and columns.
            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);
            }

            // Give the Grid a gradient brush.
            grid.Background =
                new LinearGradientBrush(Colors.Black, Colors.White,
                                        new Point(0, 0), new Point(1, 1));

            // Every program needs some randomness.
            Random rand = new Random();

            // Fill the Grid with 25 buttons.
            for (int i = 0; i < 25; i++)
            {
                Button btn = new Button();
                btn.FontSize = 12 + rand.Next(8);
                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);
            }

            // Size the Grid.
            grid.Measure(new Size(Double.PositiveInfinity,
                                  Double.PositiveInfinity));

            Size sizeGrid = grid.DesiredSize;

            // Determine point for centering Grid on page.
            Point ptGrid =
                new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
                          (dlg.PrintableAreaHeight - sizeGrid.Height) / 2);

            // Layout pass.
            grid.Arrange(new Rect(ptGrid, sizeGrid));

            // Now print it.
            dlg.PrintVisual(grid, Title);
        }
    }

这篇关于在 wpf 中动态生成的打印网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:55