单击WebBrowser控件中的按钮调用C#函数

本文关键字:调用 函数 按钮 WebBrowser 控件 单击 | 更新日期: 2023-09-27 18:24:43

我正在做一个项目。我在C#Windows窗体中使用CEFSharp(Chromium Embedded Framework)。我的解决方案中还有一个HTML网页。我正在将WebBrowser控件导航到该HTML页面。HTML页面包含一个按钮。当用户单击HTML页面上的按钮时,我如何调用C#函数?

单击WebBrowser控件中的按钮调用C#函数

我从CEFSharp wiki中获取了这段代码;希望这对你有帮助。

public class BoundObject
{
   public void OnFrameLoadEnd (object sender, FrameLoadEndEventArgs e)
    {
        if(e.Frame.IsMain)
        {
            browser.ExecuteScriptAsync(@"
             document.body.onmouseup = function()
             {
               bound.onSelected(window.getSelection().toString());
             }
            ");
        }
    }
    public void OnSelected(string selected)
    {
        MessageBox.Show("The user selected some text [" + selected + "]");
    }
}

初始化CEF对象后,注册JavaScript对象:

var obj = new BoundObject();
browser.RegisterJsObject("bound", obj);
browser.FrameLoadEnd += obj.OnFrameLoadEnd;

首先创建这个类;

    public class BoundObject
    {
        public delegate void ItemClickedEventHandler(object sender, HtmlItemClickedEventArgs e);
        public event ItemClickedEventHandler HtmlItemClicked;
        public delegate void ItemResponseEventHandler(object sender, GetResponseEventArgs e);
        public event ItemResponseEventHandler ItemResponse;

        private ChromiumWebBrowser browser;
        public BoundObject(ChromiumWebBrowser br) { browser = br; }

        public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            if (e.Frame.IsMain)
            {
                ////browser.ExecuteScriptAsync(@"
                ////    document.body.onmouseup = function()
                ////    {
                ////        bound.onSelected(window.getSelection().toString());
                ////    }
                ////");
                browser.EvaluateScriptAsync(@"window.onclick = function(e) { e.preventDefault(); bound.onClicked(e.target.outerHTML); }");
            }
        }
        public void OnSelected(string selected)
        {
            MessageBox.Show("The user selected some text [" + selected + "]");
        }
        public void OnClicked(string id)
        {
            if (HtmlItemClicked != null)
            {
                HtmlItemClicked(this, new HtmlItemClickedEventArgs() { Id = id });
            }
        }
        public void OnResponse(string projectId, string automationTaskId, string responseText)
        {
            if (ItemResponse != null)
            {
                ItemResponse(this, new GetResponseEventArgs() { AutomationTaskId = automationTaskId, ProjectId = projectId, ResponseText = responseText });
            }
        }
    }
    public class HtmlItemClickedEventArgs : EventArgs
    {
        public string Id { get; set; }
    }
    public class GetResponseEventArgs : EventArgs
    {
        public string ProjectId { get; set; }
        public string AutomationTaskId { get; set; }
        public string ResponseText { get; set; }
    }

那么;

       public frmProjectDetail()
    {
        InitializeComponent();
        browser = new ChromiumWebBrowser("http://www.google.com.tr")
        {
            Dock = DockStyle.Fill,
        };
        //browser.RegisterJsObject("bound", bound);
        splitContainerControl2.Panel1.Controls.Add(browser);
        browser.LoadingStateChanged += OnLoadingStateChanged;
        browser.ConsoleMessage += OnBrowserConsoleMessage;
        browser.StatusMessage += OnBrowserStatusMessage;
        browser.TitleChanged += OnBrowserTitleChanged;
        browser.AddressChanged += OnBrowserAddressChanged;
        browser.MouseClick += Browser_MouseClick;
        var obj = new BoundObject(browser);
        obj.HtmlItemClicked += Obj_HtmlItemClicked;
        obj.ItemResponse += Obj_ItemResponse;
        browser.RegisterJsObject("bound", obj);
        browser.FrameLoadEnd += obj.OnFrameLoadEnd;
        browser.DialogHandler = new SilentDialogHandler();
    }

    private void Obj_HtmlItemClicked(object sender, HtmlItemClickedEventArgs e)
    {
        this.InvokeOnUiThreadIfRequired(() => setMemoText("Tıklanan Element: " + e.Id));
    }

另外,不要忘记实现代码中声明的其他浏览器事件。当用户点击任何html元素私有void Obj_HtmlItemClicked时,触发