C#Capture Microsoft Print to PDF对话框

问题描述 投票:0回答:1

我想捕获并禁止从非办公室应用程序使用Microsoft Print to PDF驱动程序时显示的Savefiledialog。然后以编程方式输入文件路径,可能使用System.Windows.Automation。

当它显示时,我似乎无法找到SaveFileDialog的句柄。我相信我可以处理Windows.Automation部分。

我想使用Microsoft驱动程序,因为它随所有Windows 10一起提供。

是否有其他方法来捕获/禁止此对话框?我目前通过注册表在本地计算机上使用另一个pdf驱动程序执行此操作。但我想转向Microsoft PDF,因为它是标准的。

线程:How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10 - 不能解决我的问题,并在从Revit API运行时打印空白页面。 Autodesk Revit必须启动打印(并通过其api完成)。

尝试从user32.dll查找对话框的代码

public static List<IntPtr>GetChildWindows( IntPtr parent) {
  List<IntPtr>result=new List<IntPtr>();
  GCHandle listHandle=GCHandle.Alloc(result);
  try {
    EnumWindowProc childProc=new EnumWindowProc(EnumWindow);
    EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
  }
  finally {
    if (listHandle.IsAllocated) listHandle.Free();
  }
  return result;
}

public static string GetText(IntPtr hWnd) {
  int length=GetWindowTextLength(hWnd);
  StringBuilder sb=new StringBuilder(length + 1);
  GetWindowText(hWnd, sb, sb.Capacity);
  return sb.ToString();
}

private void Test() {
  Process[] revits=Process.GetProcessesByName( "Revit");
  List<IntPtr>children=GetChildWindows( revits[0].MainWindowHandle);
  var names=new List<string>();
  foreach (var child in children) {
    names.Add(GetText(child));
  }
}
c# pdf revit-api
1个回答
1
投票

我在自己的系统上运行了一些测试,看起来枚举顶级窗口会找到保存文件对话框。我尝试从多个程序打印到MS PDF打印机,结果都是一样的。下面是一些改编自MS Docs窗口枚举示例的代码。我在代码中添加了获取进程ID,以便您可以检查以确保它是您的窗口。

// P/Invoke declarations
protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll")]
protected static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

// Callback for examining the window
protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
{
    int size = GetWindowTextLength(hWnd);
    if (size++ > 0 && IsWindowVisible(hWnd))
    {
        StringBuilder sb = new StringBuilder(size);
        GetWindowText(hWnd, sb, size);
        if (sb.ToString().Equals("Save Print Output As", StringComparison.Ordinal))
        {
            uint procId = 0;
            GetWindowThreadProcessId(hWnd, out procId);
            Console.WriteLine($"Found it! ProcID: {procId}");
        }
    }
    return true;
}

void Main()
{
   EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.