互联网浏览器的钩子事件有时不起作用

时间:2018-02-26 08:28:58

标签: c# winforms internet-explorer events

我正在尝试从Internet Explorer注册事件。以下是代码及其正常工作。

public void Start()
        {
            shellWindows.WindowRegistered -= new SHDocVw.DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            shellWindows.WindowRegistered += new SHDocVw.DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            shellWindows.WindowRevoked -= new SHDocVw.DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);
            shellWindows.WindowRevoked += new SHDocVw.DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);

            "Listening to web events".AppendToTextFile(logFilePath);
            StartListening();
        }

上面显示的StartListening()函数绑定了Internet Explorer选项卡的文档事件。绑定功能也在下面定义。

 private void BindDocumentEvents(InternetExplorer ie)
        {
            try
            {
                HTMLDocument doc = (HTMLDocument)ie.Document;
                HTMLDocumentEvents2_Event docEvents = doc as HTMLDocumentEvents2_Event;
                docEvents.onclick += new HTMLDocumentEvents2_onclickEventHandler(htmlDoc_Click);
                docEvents.onkeyup += new HTMLDocumentEvents2_onkeyupEventHandler(htmlDoc_KeyUp);
                //docEvents.onkeypress += new HTMLDocumentEvents2_onkeypressEventHandler(htmlDoc_KeyPress);
                docEvents.onselectionchange += new HTMLDocumentEvents2_onselectionchangeEventHandler(htmlDoc_Selectionchange);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                Console.WriteLine("Exception code: {0}", e.ErrorCode);
                Console.WriteLine("Exception description: {0}", e.InnerException.Message);
            }

        }

上面显示的绑定事件及其绑定函数(如'htmlDoc_Click')仅适用于有时和绑定中断。

0 个答案:

没有答案