簡體   English   中英

C#捕獲Microsoft打印到PDF對話框

[英]C# Capture Microsoft Print to PDF dialog

我想從非Office應用程序中捕獲並禁止使用Microsoft Print to PDF驅動程序時顯示的Savefile對話框。 然后以編程方式輸入文件路徑,可能使用System.Windows.Automation。

我似乎找不到SaveFileDialog的句柄。 我相信我可以處理Windows.Automation部分。

我想使用Microsoft驅動程序,因為所有Windows 10都附帶了該驅動程序。

還有其他捕獲/抑制此對話框的方法嗎? 我目前正在通過注冊表在本地計算機上使用另一個pdf驅動程序來執行此操作。 但是我想轉到Microsoft PDF,因為它是標准的。

線程: 如何使用Windows 10隨附的Microsoft Print To PDF打印機以編程方式打印到PDF文件而無需在C#中提示輸入文件名-不能解決我的問題,並且從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));
  }
}

我在自己的系統上進行了一些測試,看來枚舉頂級窗口將找到“保存文件”對話框。 我嘗試從多個程序打印到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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM