zoukankan      html  css  js  c++  java
  • WPF 简单打印

    <Window x:Class="_096基本打印.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="259" Width="282">
        <Grid Margin="5" Name="grid">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Canvas Name="canvas">
                <TextBlock Canvas.Top="50" Canvas.Left="20" FontSize="25" FontWeight="Bold">Hello there</TextBlock>
                <Path Fill="Yellow" Stroke="Blue" Margin="5" Canvas.Left="10">
                    <Path.Data>
                        <GeometryGroup>
                            <RectangleGeometry Rect="0 0 100 100"></RectangleGeometry>
                            <EllipseGeometry Center="50 50" RadiusX="35" RadiusY="25"></EllipseGeometry>
                        </GeometryGroup>
                    </Path.Data>
                </Path>
            </Canvas>
            <Button Name="btnClick" Grid.Row="1" Click="btnClick_Click_1">Click</Button>
            
        </Grid>

    </Window>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Printing;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;


    namespace _096基本打印
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }




            private void btnClick_Click_1(object sender, RoutedEventArgs e)
            {
                PrintDialog dialog = new PrintDialog();
                var printers = new LocalPrintServer().GetPrintQueues();
                //选择一个打印机
                var selectedPrinter = printers.FirstOrDefault(p => p.Name == "EPSON LQ-300K+ /II ESC/P 2");


                if (selectedPrinter == null)
                {
                    MessageBox.Show("没有找到EPSON LQ-300K+ /II ESC/P 2打印机");
                    return;
                }



                //设置打印机
                dialog.PrintQueue = selectedPrinter;
                dialog.PrintVisual(canvas, "A Simple Drawing");
                //if (dialog.ShowDialog()==true)
                //{
                //    grid.Visibility = System.Windows.Visibility.Hidden;
                //    canvas.LayoutTransform = new ScaleTransform(5, 5);
                //    int pageMagin = 5;
                //    Size pageSize = new Size(dialog.PrintableAreaWidth - pageMagin * 2, dialog.PrintableAreaHeight - 20);
                //    canvas.Measure(pageSize);
                //    canvas.Arrange(new Rect(pageMagin,pageMagin,pageSize.Width,pageSize.Height));
                //    dialog.PrintVisual(canvas, "A Simple Drawing");
                //    canvas.LayoutTransform = null;
                //    grid.Visibility = System.Windows.Visibility.Visible;
                //}
                
            }
        }
    }

  • 相关阅读:
    Access sql语句创建表及字段类型
    30条HTML代码编写指南 for入门者
    21 个HTML网页转RSS Feeds的工具
    51 个漂亮的电子商务网站设计分享
    如何更改列表项前的New标记的天数设置(daystoshownewicon )
    如何使Layouts里的页面应用站点母板页
    SPCAMLEditor使用系列(2)利用SPCAMLEditor,实现列表顺序号。
    在SharePoint中使用自定义的服务器控件(Web Control)
    开发支持三级目录的导航菜单
    CAML查询时用户类型字段的处理
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434783.html
Copyright © 2011-2022 走看看