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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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() + "!");
        }
    }
}

 

posted @   数据酷软件  阅读(804)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示