65.9K
CodeProject 正在变化。 阅读更多。
Home

无需用户干预即可使用免费的 CutePDF Writer

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (3投票s)

2012年5月3日

CPOL
viewsIcon

18959

这是“无需用户干预即可使用免费的 CutePDF Writer”的替代方案。

引言

免费的CutePDF工具不允许您与其交互,以下文章将解释如何克服此限制。

使用代码 

以下代码片段将演示如何让主线程在显示PDF保存文件对话框时按下回车键。

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
private static void ConfirmPDFDialog()
{
    bool found = false; //bool to hold if I have found the process yet
    while (!found)
    {
        Process[] processes = Process.GetProcessesByName("CPWSave"); //Grab the PDF Dialog process
        if (processes.Length > 0) //length will be greater than 0 when it has spawned
        {
            if(processes[0].MainWindowHandle != IntPtr.Zero) //If the IntPtr == Zero, the window hasn't been displayed
            {
                IntPtr pFoundWindow = processes[0].MainWindowHandle; //Grab a handle to the window
                PostMessage(pFoundWindow, 0x100, (int)Keys.Enter, 0); //Press the enter key and send the message to the window
                found = true; //Window has been found so exit the while
            }
        }
    }
    //give it some room to breath so that the files are actually created
    Thread.Sleep(500);
}  

© . All rights reserved.