0

I have winapi c# to save webpage as pdf. Application uses control + P on webpage and hit enter. My default printer is "nuance pdf" and I want to save the file as pdf. My code looks as below:

static class Program
{
    [DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);

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

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [STAThread]
    static void Main()
    {
        Boolean Check = true;
        string text = "MyFileName";

        while (Check)
        {
            Process[] processes = Process.GetProcessesByName("iexplore");
            foreach (Process proc in processes)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait("^(p)");
                SendKeys.SendWait("{ENTER}");
                var handle = FindWindow(null, "Save As");
                Console.WriteLine("handle {0}", handle);
                //SendMessage(handle, 0x000C, IntPtr.Zero, text);
                SendKeys.SendWait("{ENTER}");
            }
            Check = false;
        }
    }
}

This code saves the file name as "https___www.google.pdf". I would like to change the file name (highlighted).

enter image description here

How do I do that? Any pointers? Thanks for helping out.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ProgSky
  • 2,530
  • 8
  • 39
  • 65
  • 2
    This sounds like a job for `System.Windows.Automation`. – Raymond Chen Jul 24 '17 at 22:39
  • Thanks Raymond. Do you have any examples ? – ProgSky Jul 24 '17 at 22:42
  • Can't you just send `Alt+n` keys to the dialog to select the file name box and then send the new file name? – Rufus L Jul 25 '17 at 00:10
  • 1
    There are many examples of using the Automation namespace on this site. [Here's one](https://stackoverflow.com/questions/14108742/manipulating-the-simple-windows-calculator-using-win32-api-in-c) that uses it to click buttons. [Here's one](https://stackoverflow.com/questions/10720162/set-text-on-textfield-textbox-with-the-automation-framework-and-get-the-change) that uses `ValuePattern` to change the text in an edit control. – Raymond Chen Jul 25 '17 at 01:29
  • Stack Overflow is not a substitute for websearch and basic research. When somebody suggests that you use UI Automation, do the basic research before asking for examples. An essential requirement of a competent programmer is the diligence and dedication to perform such research. – David Heffernan Jul 25 '17 at 13:03
  • Rufus L . I will try your suggestion as well. – ProgSky Jul 25 '17 at 13:14
  • David Heffernan - Understood ! – ProgSky Jul 25 '17 at 13:23
  • @RufusL: Worst possible solution. This completely ignores, that there is no API to send keyboard input to a particular window, or thread. Besides, how well would this work in a localized version of the dialog, where the keyboard shortcuts are different? This being suggested by a [Software Engineer at Microsoft](https://stackoverflow.com/users/story/2052655) makes me want to cry, really. Have a look at another Microsoft employee's [blog](https://blogs.msdn.microsoft.com/oldnewthing/) to learn, that recommendations like this come back to haunt someone else at your workplace. – IInspectable Jul 25 '17 at 14:35
  • @IInspectable I would not normally suggest using `SendKeys`, but the question is already using it to get this far. For a quick and dirty personal-use program, it should work fine on the user's computer. And it already relies on the locale settings and expects the default printer to be a PDF writer. The dialog already has focus, so that's not an issue unless the user intervenes (which will already be an issue). Seemed like the simplest solution to this particular question. I did not put it as an answer for the exact reason you mentioned. Hope you feel better soon. – Rufus L Jul 25 '17 at 15:42
  • @RufusL: Stack Overflow is **not** about the asker. It's about creating a professional grade database of programming problems and their solutions. While a quick and dirty hack may help the OP solve their immediate problem, it will also make its way into commercial software. Because, you know, the author consulted Stack Overflow, assuming that it offers professional grade solutions. Besides, you are still ignoring, that there is no API to send keyboard input to any particular window or thread. – IInspectable Jul 25 '17 at 17:09

1 Answers1

0

Given your current approach, and given that the text is highlighted by default, why not just send keys with the file name you want?

var handle = FindWindow(null, "Save As");
Console.WriteLine("handle {0}", handle);
SendKeys.SendWait(text);
SendKeys.SendWait("{ENTER}");

You may need to highlight the text first. Which could be a double click, or access the control directly, e.g. var filename = FindEdit(handle, "File Name:") to access it in case it's not highlighted by default.

AndrewP
  • 1,598
  • 13
  • 24
  • That'll work **really** great on a localized version of the OS... Please do not up-vote or even accept this hack as an answer. – IInspectable Jul 25 '17 at 14:36
  • @IInspectable, I did say "given [the] current approach". Solution is far from optimal but well within the bounds of existing solution. I just modified an existing line for entering the text, which will be fine on any system that the original code works. Simply providing a workable *solution* to the presented *problem and existing code* by OP – AndrewP Jul 26 '17 at 00:49
  • So, essentially, you are just lending someone a hand, who is trying to shoot their own foot. Seriously, if this were the only solution to the problem, it would be somewhat ok-ish. But when there is a [reliable solution](https://stackoverflow.com/questions/45291099/send-text-to-save-as-dialog/45291588#comment77544724_45291099), and you aren't even mentioning it, it's hard to not call this answer grossly negligent. – IInspectable Jul 26 '17 at 01:39
  • @IInspectable "how do I use what I've got to do ..." is a very different question to "how should I go about doing ...". Given the provided solution by OP, this is not grossly negligent in any way. It is a simple tool to solve a simple problem. Not everyone is writing enterprise applications – AndrewP Jul 26 '17 at 03:48
  • This isn't a forum. This is a Q&A site for professional software developers. An answer should be correct and valuable to anyone visiting this site, not just the OP. In this case, the OP asked for X, but really needed to solve Y. This is known as the [XY Problem](http://xyproblem.info/). A useful answer *must* acknowledge this. Since it doesn't, *"this answer is not useful"*. This necessitates a down-vote, I'm afraid. – IInspectable Jul 26 '17 at 11:56
  • I ended up using webControl.Print() to save pages to pdf. I will keep AndrewP answer as accepted since his answer helped me with what I asked in the question. – ProgSky Jul 26 '17 at 17:52