其他分享
首页 > 其他分享> > WPF 打印操作之PrintDialog

WPF 打印操作之PrintDialog

作者:互联网

一、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 2010</TextBlock>
            <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 如何滚动到光标所在位置、滚动条操作 

 

标签:PrintVisual,打印,richText,dialog,WPF,PrintDialog
来源: https://www.cnblogs.com/tianma3798/p/12814368.html