打印在wpf中动态生成的网格

发布于 2024-11-08 03:18:26 字数 1500 浏览 0 评论 0原文

我想打印动态生成的网格。

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

这是我的代码,

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");
        }
    }

该代码给我打印空白页。

我怎样才能做到这一点?

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.

here is my code,

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.

how can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-11-15 03:18:26

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

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);
        }
    }

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);
        }
    }
深居我梦 2024-11-15 03:18:26

PrintVisual 打印 Visual 对象。这意味着,通过使用 PrintVisual 方法,我们可以打印视觉树中的任何控件、容器、窗口或用户控件。您无法打印不在视觉树中的控件

The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control that is in the visualtree.You cannot print a control that is not in the visualtree

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文