当前位置: 首页 > news >正文

这是要逆天么,看我控制台程序玩Microsoft XPS Document 打印

主要是想试试Microsoft XPS Document 打印时怎样去掉那个“将打印输出另存为”对话框

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Printing;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
    /// <summary>
    /// 控制台玩 Microsoft XPS Document 打印
    /// </summary>
    class Program
    {
        //Win32 Api定义
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfeter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, String lParam);
        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        //Win32消息定义
        const uint WM_SETTEXT = 0x000c;
        const uint WM_IME_KEYDOWN = 0x0290;
        const uint WM_LBUTTONDOWN = 0x0201;
        const uint WM_LBUTTONUP = 0x0202;
        static void Main(string[] args)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            pd.PrintController = new StandardPrintController();
            pd.BeginPrint += Pd_BeginPrint;
            pd.PrintPage += Pd_PrintPage;
            pd.EndPrint += Pd_EndPrint;
            Action printTask = () =>
            {
                System.Threading.Thread t = new System.Threading.Thread(() =>
                {
                    while (true)
                    {
                        IntPtr hWnd = FindWindow("#32770", "将打印输出另存为");
                        if (hWnd != IntPtr.Zero)
                        {
                            IntPtr hChild;
                            // 由于输入框被多个控件嵌套,因此需要一级一级的往控件内找到输入框
                            hChild = FindWindowEx(hWnd, IntPtr.Zero, "DUIViewWndClassName", String.Empty);
                            hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", String.Empty);
                            hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", String.Empty);
                            hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", String.Empty);
                            hChild = FindWindowEx(hChild, IntPtr.Zero, "Edit", String.Empty);
                            SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, AppDomain.CurrentDomain.BaseDirectory + Guid.NewGuid().ToString().Replace("-", "") + ".xps");
                            System.Threading.Thread.Sleep(100);
                            // 找到对话框内的保存按钮
                            hChild = IntPtr.Zero;
                            hChild = FindWindowEx(hWnd, IntPtr.Zero, "Button", "保存(&S)");
                            // 向保存按钮发送2个消息,以模拟click消息,借此来按下保存按钮
                            PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
                            PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);  
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                });
                t.Start();
                int index = 0;
                while (index < 10)
                {
                    pd.Print();
                    LocalPrintServer prtSrv = new LocalPrintServer();
                    PrintQueue queue = prtSrv.GetPrintQueue("Microsoft XPS Document Writer");
                    do
                    {
                        System.Threading.Thread.Sleep(1000);
                        queue.Refresh();
                    } while (queue.NumberOfJobs > 0);
                    Console.WriteLine(DateTime.Now +string.Format( "任务{0}打印完成。",index)); 
                    index++;
                }
            };
            printTask.BeginInvoke(null, null);            
            Console.ReadLine();
        }

        private static void Pd_EndPrint(object sender, PrintEventArgs e)
        {
            Console.WriteLine(DateTime.Now + e.PrintAction.ToString()+"!");
        }

        private static void Pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            var g = e.Graphics;
            g.DrawString("Just A Print Test." +
                        Environment.NewLine +
                        Guid.NewGuid().ToString().Replace("-", ""),new System.Drawing.Font("微软雅黑", 12F), new SolidBrush(Color.Black), new Point(2, 2));
        }

        private static void Pd_BeginPrint(object sender, PrintEventArgs e)
        {
            Console.WriteLine(DateTime.Now + e.PrintAction.ToString() + "!");
        }
    }
}

 

相关文章:

  • Spring Cloud中负载均衡器概览
  • 文件特殊权限:SUID,SGID,SBIT
  • 11.03 在外链接中用OR逻辑
  • 将文件名从1变为000001
  • 重构之美-跨越Web标准,触碰语义网[分离:通用也许是个美丽陷阱]
  • shell脚本(1)
  • 笔记-Kotlin学习
  • 【Unity Shader】五、Shader纹理映射,及纹理的缩放和偏移
  • img 样式单和属性
  • 统计学基于SPSS贾俊平 授课笔记 发布作业 spss19cn 软件下载地址及破解包spss19_10039 下载地址...
  • zabbix监控模板大全
  • 2017杭州云栖大会—移动云专场【赠票】
  • seaJS源码
  • apache 日志轮询三种方法
  • Django REST框架-基于类的视图
  • centos安装java运行环境jdk+tomcat
  • DataBase in Android
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • jquery ajax学习笔记
  • JSONP原理
  • tweak 支持第三方库
  • vagrant 添加本地 box 安装 laravel homestead
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 缓存与缓冲
  • 如何胜任知名企业的商业数据分析师?
  • linux 淘宝开源监控工具tsar
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • mysql面试题分组并合并列
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • # C++之functional库用法整理
  • #162 (Div. 2)
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (二)构建dubbo分布式平台-平台功能导图
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (一)VirtualBox安装增强功能
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)原始图像数据和PDF中的图像数据
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 使用 ILRepack 合并多个程序集(替代 ILMerge),避免引入额外的依赖
  • .NetCore项目nginx发布
  • .net程序集学习心得
  • .NET企业级应用架构设计系列之技术选型
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • :O)修改linux硬件时间
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • []error LNK2001: unresolved external symbol _m
  • [<事务专题>]
  • [3D基础]理解计算机3D图形学中的坐标系变换