我有一个可以连续运行的应用程序。 用户可以打开诸如“选项”或“打印”之类的内容,但经常走开而使其不使用并保持打开状态。 因此,我现在所有内容都超时了,但是PrintDialog给我带来了悲伤。
Get modal dialog handle for PrintDialog处的线程 一直是一个巨大的帮助,它使我获得了PrintDialog的句柄,因此我可以取消它,但是它不能解决关闭诸如“首选项”等PrintDialog子项的问题。
尽管user32.dll GetActiveWindow()正确地为我提供了PrintDialog的句柄,但它似乎没有获得Preferences子项并返回值0。
public static IntPtr printDialogHandle;
[DllImport("user32.dll")]
static extern bool DestroyWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetActiveWindow();
private void btnPrint_Click(object sender, EventArgs e)
{
BeginInvoke(new MethodInvoker(TweakPrintDialog));
using (this.pdi = new PrintDialog())
{
this.pdi.PrinterSettings = Properties.Settings.Default.printSettings;
if (this.pdi.ShowDialog(this) == DialogResult.Cancel)
return;
//do the actual printing
printPages(false);
}
}
private void TweakPrintDialog()
{
// get handle for the just opened PrintDialog
printDialogHandle = GetActiveWindow();
// timeout timer
System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
tmr.Tick += new EventHandler(timer_Tick);
tmr.Interval = 60000;
tmr.Start();
}
private static void timer_Tick(object sender, EventArgs e)
{
System.Windows.Forms.Timer tmr = (System.Windows.Forms.Timer)sender;
tmr.Stop();
tmr.Dispose();
//any children? returns handles to heaps of unwanted stuff like controls
// System.Diagnostics.Process[] otherApps = System.Diagnostics.Process.GetProcessesByName("Printing Selected Papers");
// if (otherApps.Length == 0) return;
// if (otherApps[0] != null)
// {
// // var allChildWindows = new WindowHandleInfo(otherApps[0].MainWindowHandle).GetAllChildHandles();
// var allChildWindows = new WindowHandleInfo(printDialogHandle).GetAllChildHandles();
// }
// Works but unreliable. Other windows may be active by now
// SendKeys.Send("{ESC}");
// System.Threading.Thread.Sleep(50);
// SendKeys.Send("{ESC}");
// System.Threading.Thread.Sleep(50);
// SendKeys.Send("{ESC}");
// try and get handle to the PrintDialog Preferences form. Doesn't work, just returns 0
IntPtr child = GetActiveWindow();
if (child != printDialogHandle)
DestroyWindow(child);
// get rid of the PrintDialog
DestroyWindow(printDialogHandle);
}
我如何找到所有PrintDialog子句柄,以便在销毁PrintDialog本身之前通过user32.dll DestroyWindow()杀死它们?