0

I have a C# program that I've been running in windows 7 fine but now that I upgraded to windows 10 with a newer browser, it has stopped working correctly.

The issue is with handling of dialog boxes. When it pops up, I just want to close the box. I used the below code. If it pops up again, the code executes again to close it.

        IntPtr hwnd = FindWindow("#32770", "Message from webpage");
        if (hwnd != IntPtr.Zero)
        {
            hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
            SendMessage(hwnd, 0xf5, IntPtr.Zero, IntPtr.Zero);

Now, when more than one box pops up, there will be a checkbox that says "Don’t let this page create more messages". The above code is unable to close this box. I'm not sure what I am missing.

I'm not even sure why this error pops up in the first place. If I intervene and manually click the button on the webpage, I don't get the popup usually. But that is another issue.

eng3
  • 431
  • 3
  • 18
  • The easiest way is to fix webpage code – Selvin Apr 02 '19 at 14:29
  • I have no control over the webpage code – eng3 Apr 02 '19 at 14:36
  • The web browser control used the web engine where it is run. Perhaps the Win7 and Win10 do behave differently, you could try to setup the control to use a specific browser/compatibility mode. – Cleptus Apr 02 '19 at 14:42
  • Possible duplicate of [Use latest version of Internet Explorer in the webbrowser control](https://stackoverflow.com/questions/17922308/use-latest-version-of-internet-explorer-in-the-webbrowser-control) – Cleptus Apr 02 '19 at 14:42
  • Yes I am aware of the browser emulation setting and that is something I may end up doing. However, eventually that will become an issue as webpages get newer. For now, I'm looking to see if there is a way to update my code so it can just handle the new type of window. – eng3 Apr 02 '19 at 14:45
  • If you want to follow the same approach then use `Spy++`, find class of that new window and then just replace `FindWindow("#32770", "Message from webpage")` to the appropriate `FindWindow(, )`. – Sergey Shevchenko Apr 02 '19 at 15:09
  • I should clarify. The stuff later in the code runs. Therefore, I believe FindWindow works as I assume the program would crash if hwnd was null. Therefore, I am assuming the SendMessage line is not working as expected. – eng3 Apr 02 '19 at 15:13
  • Can you attach the picture of the window with checkbox? I don't have Win10 near me right now to check it myself. – Sergey Shevchenko Apr 02 '19 at 15:16
  • @eng3 if `FindWindow` works fine, does the `FindWindowEx` work fine as well? Can you check if `hwnd != IntPtr.Zero` before the `SendMessage`? – Sergey Shevchenko Apr 02 '19 at 15:26
  • Here is an example of the checkbox: https://www.brokenbrowser.com/wp-content/uploads/2017/02/01_alert_with_checkbox.png I'll check and report back – eng3 Apr 02 '19 at 15:29

1 Answers1

1

Try just send WM_CLOSE message to the window instead of emulating click on the OK button:

IntPtr hwnd = FindWindow("#32770", "Message from webpage");
if (hwnd != IntPtr.Zero)
{
    SendMessage(hwnd, 0x10, IntPtr.Zero, IntPtr.Zero);
}