gpt4 book ai didi

c# - 我怎么知道在发送打印命令(按下 "Ctrl+P"按钮)后我应该等待 PrintDialog 多长时间?

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:21 26 4
gpt4 key购买 nike

我正在使用 C# 编写一个控制台应用程序,我在其中通过代码触发打印作业。我的问题是我不知道在发送命令到 Print 后我应该等待 Print Dialog 多长时间。目前我使用的线程 sleep 时间为 1000 毫秒。

//Sending Commands to Print(to press "Ctrl+P" button).
SendKeys.SendWait("^(p)");

Thread.Sleep(1000);

//Sending Commands to Print(to press "Enter" Button).
SendKeys.SendWait("{ENTER}");

谁能帮我解决这个问题。任何帮助将不胜感激。

提前致谢

更新:

这是我的全部代码:

//Launch the file from the location specified in.
White.Core.Application application =White.Core.Application.Launch(@path);

Console.WriteLine("launch is done");

Thread.Sleep(_delayOfPrint);

//Sending Commands to Print(to press "Ctrl+P" button).
SendKeys.SendWait("^(p)");

Thread.Sleep(1000);

//Sending Commands to Print(to press "Enter" Button).
SendKeys.SendWait("{ENTER}");

//Get the current time as the document fired for print job.
_printedTime = DateTime.Now;

Thread.Sleep(1500);

//Closing the window.
SendKeys.SendWait("%{F4}");

最佳答案

我最多等待几秒钟,但在这段时间内我会定期使用 Win32 函数 FindWindowEx 来查看打印对话框是否真的出现,如果出现则继续。您可以使用如下代码获取进程的主窗口:

Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
}

然后您可以将找到的主窗口句柄传递给 FindWindowEx 以细读子窗口以检查打印对话框。 FindWindowEx 具有如下 PInvoke 签名:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

编辑 2: 由于 OP 似乎要求我提供一个完美的工作函数,我写了一个通用的函数来完成这个技巧并对其进行了测试。在这里,工作通用代码等待任何子窗口。在这种情况下调用 WaitForChildWindow("myApp", "Print", 5000):

/// <summary>
/// Wait for a child window of an application to appear
/// </summary>
/// <param name="appName">Application name to check (will check all instances)</param>
/// <param name="childWindowName">Name of child window to look for (titlebar)</param>
/// <param name="timeout">Maximum time, in milliseconds, to wait</param>
/// <returns>True if the window was found; false if it wasn't.</returns>
public static bool WaitForChildWindow(string appName, string childWindowName, int timeout)
{
int sleepTime = timeout;
while (sleepTime > 0)
{
Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pMainWindow = p.MainWindowHandle;
IntPtr pFoundWindow = FindWindowEx(pMainWindow, IntPtr.Zero, null, childWindowName);
if (pFoundWindow != IntPtr.Zero)
return true;
}
Thread.Sleep(100);
sleepTime -= 100;
}

// Timed out!
return false;
}

编辑 3:这是为没有子关系的仅拥有的窗口执行此操作的另一种方法:

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);

public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);

static bool EnumThreadCallback(IntPtr hWnd, IntPtr lParam)
{
StringBuilder text = new StringBuilder(500);
GetWindowText(hWnd, text, 500);
if (text.ToString() == "Print")
return false;
return true;
}

public static bool FindThreadPrintWindow(uint threadId)
{
return !EnumThreadWindows(threadId, EnumThreadCallback, IntPtr.Zero);
}

public static bool WaitForOwnedPrintWindow(string appName, int timeout)
{
int sleepTime = timeout;
while (sleepTime > 0)
{
Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pMainWindow = p.MainWindowHandle;
uint threadId = GetWindowThreadProcessId(pMainWindow, IntPtr.Zero);
if (FindThreadPrintWindow(threadId))
return true;
}
Thread.Sleep(100);
sleepTime -= 100;
}

// Timed out!
return false;
}

关于c# - 我怎么知道在发送打印命令(按下 "Ctrl+P"按钮)后我应该等待 PrintDialog 多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6869118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com