Based on this answer, I created a custom DialogHandlerJavascript to handle warning boxes that appear from a WebBrowser control.
Handlertied to a legacy WatiN browser IEcalled ExtendedIeBrowser.
For some unknown reason, watin DialogHandler intervenes in Winforms SaveFiledialogs. SaveFileDialogcloses automatically, returning DialogResult.Cancel. It is strange that a Handle()custom handler is never called. It CanHandle()is only called (twice) and returns false, so the dialog should not be processed at all, therefore it should remain open.
Is there anything I can do to change this weird behavior?
This is the source ExtendedIeBrowser:
public class ExtendedIeBrowser : IE
{
private IntPtr hwnd;
public ExtendedIeBrowser(WebBrowser webBrowserControl) : base(webBrowserControl.ActiveXInstance, false)
{
}
public void Initialize(WebBrowser webBrowserControl)
{
hwnd = webBrowserControl.FindForm().Handle;
StartDialogWatcher();
}
public override IntPtr hWnd { get { return hwnd; } }
protected override void Dispose(bool disposing)
{
hwnd = IntPtr.Zero;
base.Dispose(disposing);
}
}
After the source CustomPopupDialogHandler:
class CustomPopupDialogHandler : ReturnDialogHandler
{
protected static Logger _logger = LogManager.GetCurrentClassLogger();
public override bool HandleDialog(Window window)
{
bool handled = false;
try
{
var button = GetWantedButton(window);
if (button != null)
{
button.Click();
}
handled = true;
}
catch (Exception ex)
{
_logger.ErrorException("HandleDialog", ex);
}
return handled;
}
public override bool CanHandleDialog(Window window)
{
bool canHandle = false;
try
{
canHandle = GetWantedButton(window) != null;
}
catch (Exception ex)
{
_logger.ErrorException("CanHandleDialog", ex);
}
return canHandle;
}
private WinButton GetWantedButton(Window window)
{
WinButton button = null;
try
{
if (window.Title.Contains("Windows Internet Explorer") || window.Title.Contains("Message from webpage"))
{
var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && (new WinButton(w.Hwnd).Title.Contains("Leave") || new WinButton(w.Hwnd).Title.Contains("OK")).FirstOrDefault();
if (windowButton != null)
{
string s = windowButton.Title;
button = new WinButton(windowButton.Hwnd);
}
}
}
catch (Exception ex)
{
_logger.ErrorException("GetWantedButton", ex);
}
return button;
}
}
source
share