1. 程式人生 > >消息處理模擬點擊

消息處理模擬點擊

ace window inter message uid classname cli 按鈕 write

技術分享
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() + "!"); } } }
View Code

Microsoft XPS Document 打印時怎樣去掉那個“將打印輸出另存為”對話框

消息處理模擬點擊

消息處理模擬點擊