• WPF打印控件内容


    当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现

    界面如下:

    XAML代码如下

     1  <Grid>
     2         <Grid.ColumnDefinitions>
     3             <ColumnDefinition/>
     4             <ColumnDefinition Width="300"/>
     5         </Grid.ColumnDefinitions>
     6         <Grid Grid.Column="0">
     7 
     8             <Grid Width="800" Name="grid1">
     9                 <TextBlock TextWrapping="Wrap" FontSize="15">
    10                 Control authors who want to customize the arrange pass of layout processing should override this method. The implementation pattern should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each visible child element, and pass the final desired size for each child element as the finalRect parameter. Parent elements should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each child, otherwise the child elements will not be rendered.
    11 Many derived classes offer implementations of this method. Prominent ones include: M:System.Windows.Window.ArrangeOverride(System.Windows.Size), M:System.Windows.Controls.Page.ArrangeOverride(System.Windows.Size) and M:System.Windows.Controls.Control.ArrangeOverride(System.Windows.Size).
    12                 </TextBlock>
    13             </Grid>
    14         </Grid>
    15       
    16         <Grid Grid.Column="1">
    17             <Button Height="30" VerticalAlignment="Top" Click="Button_Click">打印</Button>
    18         </Grid>
    19     </Grid>

    当我们点击按钮时,进行打印

    按钮事件:

               PrintDialog pd = new PrintDialog();
                if (pd.ShowDialog() == true)
                {
                    pd.PrintVisual(this.grid1, "");
                }

    这时我们会发现,虽然 打印的内容是指定的,但打印的大小却是整个窗体的大小,而不仅仅是指定的区域大小。

    然后我们就需要用到UIElement的Arrange 方法

    MSDN上的解释是

    定位子元素,并确定 UIElement 的大小。 父元素从它们的 ArrangeCore 实现(或者是 WPF 框架级别等效项)调用此方法,以便形成递归布局更新。此方法产生第二次布局更新。

    修改后的代码如下:

    1 if (pd.ShowDialog() == true)
    2             {                
    3                 this.grid1.Arrange(new Rect(new Size(grid1.ActualWidth, grid1.ActualHeight)));
    4                 pd.PrintVisual(this.grid1, "");               
    5             }

    这样操作以后,打印的大小不再是整 个窗体的大小了,但打印完之后 ,控件 的位置却发生了变化 ,这时候我们只需要再调用一次Arrange方法,将它放回原来的位置就行了

    1 if (pd.ShowDialog() == true)
    2             {               
    3                 Window window = Window.GetWindow(grid1);
    4                 Point point = grid1.TransformToAncestor(window).Transform(new Point(0, 0));//获取当前控件 的坐标
    5                 this.grid1.Measure(new Size(grid1.ActualWidth,grid1.ActualHeight));
    6                 this.grid1.Arrange(new Rect(new Size(grid1.ActualWidth, grid1.ActualHeight)));
    7                 pd.PrintVisual(this.grid1, "");
    8                 this.grid1.Arrange(new Rect(point.X, point.Y, grid1.ActualWidth, grid1.ActualHeight));//设置为原来的位置
    9             }

    这样就可以打印控件 内容了。

    如果 想对打印机进行设置,可以查找 WPF PrintDialog的使用方法,下面是简单的设置

    1                 PrintTicket pt = new PrintTicket();
    2                 PageMediaSize p = new PageMediaSize(PageMediaSizeName.ISOA4);
    3                 //pt.PageBorderless = PageBorderless.Unknown;
    4                 pt.PageMediaSize = p;
    5                 //pt.PageOrientation = PageOrientation.Portrait;
    6                 pd.PrintTicket = pt;

     希望能帮助到有需要的人。

  • 相关阅读:
    关于dubbo创建服务和引用服务时,会报错:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 问题的解决
    解决 Maven工程运行报错Failed to clean project: Failed to delete
    Linux学习_004_使用CentOS 7.5卸载自带jdk安装自己的JDK1.8
    Linux下的JDK和OpenJDK有什么具体的区别
    常用的大数据技术有哪些?
    如何隐藏你的 Linux 的命令行历史
    Securi-Pi:使用树莓派作为安全跳板
    在 Linux 上管理加密密钥的最佳体验
    在 Linux 下使用任务管理器
    在 Linux 中安装 Lighttpd Web 服务器
  • 原文地址:https://www.cnblogs.com/zhaotianff/p/7340554.html
Copyright © 2020-2023  润新知