0

Basics: I have a Windows .NET Form application with an embedded WebBrowser control. It is triggering a download which opens the "Save Download" dialog. I would like to click on the "Save" button automagically.

I found this other StackOverflow question: Press save button of "File download dialog" of internet explorer via c# but it has no real answers.

The code I have so far doesn't reliably work when deployed to the actual workstation. Running it in debug/from Visual Studio, it works fine. On actual workstations, sometimes the Save button "highlights" or depresses like it's clicked, but nothing actually happens.

Here be the code:

Dim dialogHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "File Download")
Dim buttonTitle = "&Save"
Dim dialogButtonHandle = FindWindowEx(dialogHandle, IntPtr.Zero, "Button", buttonTitle)

SendMessage(dialogButtonHandle, BM_SETSTATE, 0, 0)
SendMessage(dialogButton, BM_CLICK, 1, 0)
SendMessage(dialogButtonHandle, BM_SETSTATE, 1, 0)

I have no idea if this is even the totally 100% correct way of clicking buttons in other windows, but it seems to somewhat work. Maybe I'm sending SendMessage's too fast? Too slow? Should I be using SendMessage with BM_CLICK, etc. or some other actions? I feel like I'm 90% of the way there.. any help would be appreciated.

Community
  • 1
  • 1
Nicholas Head
  • 3,716
  • 5
  • 27
  • 39
  • Have you tried adding a Thread.Sleep(100) to give a pause in the right places? Sometimes it could send the event at the wrong time... Just a thought. – Jared Updike Nov 03 '10 at 00:13
  • Yes, I have tried that, but unfortunately since the WebBrowser control is running under the same thread, it freezes IE (dialogs and all) as far as I can tell. I also tried doing a "fake sleep" (just waiting xxx milliseconds in a loop that calls Application.DoEvents()) and that didn't help. I don't see anywhere in documentation that says I need to wait anyway.. – Nicholas Head Nov 03 '10 at 01:30
  • Locking this; the cycle of delete/undelete is causing issues – Marc Gravell Nov 11 '10 at 10:23

1 Answers1

0

The BM_SETSTATE message simply makes the button look like its pressed or not, I'm guessing you don't particularly care about that and just want the actual action of pressing the button to occur.

Try changing

SendMessage(dialogButtonHandle, BM_SETSTATE, 0, 0)
SendMessage(dialogButton, BM_CLICK, 1, 0)
SendMessage(dialogButtonHandle, BM_SETSTATE, 1, 0)

To just

SendMessage(dialogButtonHandle, BM_CLICK, 1, 0)

Note the change to dialogButtonHandle for the BM_CLICK message.

Nick Thomson
  • 211
  • 3
  • 10