zoukankan      html  css  js  c++  java
  • WPF实现打印功能

    WPF实现打印功能

    在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示。首先在VS中编辑一个图形(如下图所示)。

    Design

         将需要打印的内容放入同一个<Canvas>中,并起名为“printArea”,打印按键一般不是我们希望打印出来的内容,则将其放在<Canvas>外面。

    复制代码
    <Window x:Class="WpfPrint.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="500">
        <Grid>
            <Canvas x:Name="printArea">
                <Ellipse Canvas.Left="137" Canvas.Top="92" Height="100" 
                         Stroke="Black" Width="200">
                    <Ellipse.Fill>
                        <LinearGradientBrush>
                            <GradientStop Color="#FFAD0FC7" Offset="0" />
                            <GradientStop Color="#FF3359AD" Offset="1" />
                        </LinearGradientBrush>
                    </Ellipse.Fill>
                </Ellipse>
                <TextBlock FontSize="20" FontWeight="Bold" Foreground="White"
                           Canvas.Left="151" Canvas.Top="129" Height="33">
                    Visual Studio 2010</TextBlock>
                <Image Source="vs2010.jpg" Height="52" Width="90" 
                       Canvas.Left="388" Canvas.Top="0" />
            </Canvas>
            <Button Content="Print" Click="Button_Click" Height="23" 
                    Margin="195,268,190,20" />
        </Grid>
    </Window>
    复制代码

    接下来编写Button_Click 事件,由于我们要打印<Canvas>所包含的内容,所以要通过PrintVisual 打印Visual 对象。

    复制代码
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog dialog = new PrintDialog();
        if (dialog.ShowDialog() == true)
        { 
            dialog.PrintVisual(printArea, "Print Test"); 
        }
    }
    复制代码

    运行程序,点击“Print”按键,弹出打印设置窗口,打印到XPS看看效果。

    Print

    如下图所示,打印结果中只有<Canvas>中的内容。

    XPS

     
     
  • 相关阅读:
    InitializingBean
    线程池
    maven
    mysql主从库
    zookeeper
    分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    远程调试
    enum
    注解
    Shell错误[: missing `]'
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3262811.html
Copyright © 2011-2022 走看看