WPF 打印操作之PrintDialog

2021-03-07 22:28

阅读:749

标签:soft   显示   oca   代码   out   orm   ora   span   directed   

一、WPF 打印操作之PrintDialog

    在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能。

PrintDialog可以直接打印Wpf控件或者页面,将呈现结果使用Windows系统默认打印机,执行打印。

            PrintDialog dialog = new PrintDialog();
            //显示打印框,选择份数和打印机
            if (dialog.ShowDialog() == true)
            {
                //  dialog.PrintVisual(printArea, "Print Test");
                dialog.PrintVisual(richText, "测试");
            }

            //直接打印
            // dialog.PrintVisual(richText, "测试");

关于PrintDialog的更多用法请参考:

https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.controls.printdialog?redirectedfrom=MSDN&view=netcore-3.1

 

二、代码示例

1.wxml

Window x:Class="CoreWpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CoreWpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    Grid>
        Canvas x:Name="printArea">
            Ellipse Canvas.Left="192" Canvas.Top="24" 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="198" Canvas.Top="63" Height="33">
                Visual Studio 2010TextBlock>
            Image Source="vs2010.jpg" Height="52" Width="90" 
                   Canvas.Left="388" Canvas.Top="0" />
            RichTextBox x:Name="richText" Height="127" Width="315" Canvas.Left="433" Canvas.Top="103" TextChanged="RichTextBox_TextChanged">
                FlowDocument>
                    Paragraph>
                        Run Text="RichTextBox"/>
                    Paragraph>
                    Paragraph>
                        Run Text="RichTextBox"/>
                    Paragraph>
                    Paragraph>
                        Run Text="RichTextBox"/>
                    Paragraph>
                FlowDocument>
            RichTextBox>
            Button Content="Button" Canvas.Left="207" Canvas.Top="230" Height="52" Width="137" Click="Button_Click_1"/>
        Canvas>
        Button Content="打印测试" HorizontalAlignment="Left" Margin="196,143,0,0" VerticalAlignment="Top" Height="60" Width="132" Click="Button_Click"/>
    Grid>
Window>

 

2.C#

private void Button_Click(object sender, RoutedEventArgs e)
{
    PrintDialog dialog = new PrintDialog();
    //显示打印框,选择份数和打印机
    if (dialog.ShowDialog() == true)
    {
        //  dialog.PrintVisual(printArea, "Print Test");
        dialog.PrintVisual(richText, "测试");
    }

    //直接打印
    // dialog.PrintVisual(richText, "测试");
}

 

技术图片

 

 

更多:

WPF 皮肤之MathApps.Metro UI库   

WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例 

WPF RichTextBox 如何滚动到光标所在位置、滚动条操作 

 

WPF 打印操作之PrintDialog

标签:soft   显示   oca   代码   out   orm   ora   span   directed   

原文地址:https://www.cnblogs.com/tianma3798/p/12814368.html


评论


亲,登录后才可以留言!