获取PrintDialog的模态对话框句柄

时间:2014-03-12 18:08:14

标签: c# .net printing

我在.net 2.0上有一个Windows应用程序。在Form1,我打开PrintDialog。如何从我的代码处理该对话框?

我尝试过很多win32函数:EnumWindowsEnumChildWindowsFindWindowFindWindowEx但找不到我的PrintDialog。我所能找到的只是Form1,它的孩子就是它的控件。我无法让PrintDialog's处理。

我尝试过的一些代码:

导入win32:

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

调用win32函数:

using (PrintDialog dlg = new PrintDialog
                                 {
                                     AllowCurrentPage = false,
                                     AllowSomePages = true,
                                     AllowSelection = false
                                 })
{    
      IntPtr printHandle = CustomPrintDialog.FindWindow("#32770", "Print");
      // some logic with printHandle go here
      if (dlg.ShowDialog(this)==DialogResult.OK){
          // some logic go here
      }
}

我已经查过Spy ++,但仍然有一个PrintDialog窗口。 PrintDialog窗口的父窗口句柄与Form1's句柄完全相同。

是否有人可以帮助我从其父窗口获取PrintDialog's句柄?

1 个答案:

答案 0 :(得分:2)

问题是PrintDialog的基础窗口是在执行ShowDialog方法期间创建的。在此方法调用之前不存在,这就是您无法找到窗口的原因。 因此,您必须在PrintDialog内的ShowDialog句柄上注入您的工作。这可以通过Control.BeginInvoke方法实现:

public partial class Form1 : Form
{
    ...

    private ShowPrintDialog()
    {
        using (var pd = new PrintDialog())
        {
            BeginInvoke(new MethodInvoker(TweakPrintDialog));
            if (pd.ShowDialog(this) == DialogResult.OK)
            {
                // printing
            }
        }
    }

    private void TweakPrintDialog()
    {
        var printDialogHandle = GetActiveWindow();
        // do your tweaking here
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr GetActiveWindow();
}

另一个问题是找到PrintDialog窗口。 GetActiveWindow实际上是一种直接的方法,因为在ShowDialog处于运行状态时,对话框应该处于活动状态。 更可靠的解决方案可能包括枚举顶级窗口并分析其所有者和/或其他道具。这是一种可能的方法,假设打印对话框是表单所拥有的唯一窗口。上一个代码段中的TweakPrintDialog方法已修改:

    private void TweakPrintDialog()
    {
        uint processId;
        var threadId = GetWindowThreadProcessId(this.Handle, out processId);
        EnumThreadWindows(threadId, FindPrintDialog, IntPtr.Zero);
        // printDialogHandle field now contains the found handle
        // do your tweaking here
    }

    private IntPtr printDialogHandle;

    private bool FindPrintDialog(IntPtr handle, IntPtr lParam)
    {
        if (GetWindow(handle, GW_OWNER) == this.Handle)
        {
            printDialogHandle = handle;
            return false;
        }
        else
        {
            return true;
        }
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    private delegate bool EnumWindowProc(IntPtr handle, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EnumThreadWindows(uint threadId, EnumWindowProc enumProc, IntPtr lParam);

    private const uint GW_OWNER = 4;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr GetWindow(IntPtr handle, uint cmd);