博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
这是要逆天么,看我控制台程序玩Microsoft XPS Document 打印
阅读量:7079 次
发布时间:2019-06-28

本文共 4544 字,大约阅读时间需要 15 分钟。

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

using System;using System.Drawing;using System.Drawing.Printing;using System.Printing;using System.Runtime.InteropServices;namespace ConsoleApplication4{    ///     /// 控制台玩 Microsoft XPS Document 打印    ///     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() + "!");        }    }}

 

转载地址:http://oncml.baihongyu.com/

你可能感兴趣的文章
QQ等软件可以联网 网页打不开
查看>>
c++ 使用socket实现C/S端文件的下载传输
查看>>
JMF获取设备列表失败,获取视频设备失败?
查看>>
国内 Mono 相关文章汇总
查看>>
Python模块学习 ---- datetime
查看>>
MS SQL Server Quarter Function
查看>>
《你不知道的JavaScript》整理(三)——对象
查看>>
MySQL实现定时任务
查看>>
警告 “util.NativeCodeLoader: Unable to load native-hadoop library for your platform”
查看>>
ASP.NET 查询客户端请求IP地址
查看>>
使用echo命令清空tomcat日志文件
查看>>
Android开发怎么让自己的APP UI漂亮、大方(配色篇二)
查看>>
datetimerangepicker配置及默认时间段展示
查看>>
什么时候使用CountDownLatch
查看>>
InfluxDB部署
查看>>
Android 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突...
查看>>
windows平台下编辑的内容传到linux平台出现中文乱码的解决办法【转】
查看>>
js为元素动态添加css代码
查看>>
软件包管理 之 用apt+synaptic 在线安装或升级Fedora core 4.0 软件包── 为新手指南...
查看>>
DBGRIDEH保存"显示标题"
查看>>