■90410 / inTopicNo.2) |
Re[1]: VB.NETのHTMLDocumentイベントについて |
□投稿者/ 魔界の仮面弁士 (2090回)-(2019/03/08(Fri) 18:19:12)
|
2019/03/08(Fri) 20:13:06 編集(投稿者)
■No90409 (TanuTanu さん) に返信 > VBAではHTMLDocumentのイベント処理は簡単に出来ましたが、 > VB.NETでは、Dim WithEvents doc As HTMLDocument ではイベント表示すらされません。
System.Windows.Forms.HtmlDocument クラスをお使いでしょうか。 その場合は、Click / Focusing/ MouseMove などの基本的なイベントを WithEvents や AddHandler で扱えるはずです。 (HtmlWindow クラスなら、Load / Resize / Scroll イベント等)
http://bbs.wankuma.com/index.cgi?mode=al2&namber=51775&KLOG=87
その他のイベントの場合は、AttachEventHandler メソッドを試してみてください。 (VBA 同様、ActiveX 版の HTMLDocument オブジェクトを利用することもできます)
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load WebBrowser1.Navigate("http://bbs.wankuma.com/index.cgi?mode=al2&namber=90409") End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted Dim doc = WebBrowser1.Document doc.AttachEventHandler("onmousemove", Sub(o, arg) document_onmousemove(doc)) doc.AttachEventHandler("onkeypress", Sub(o, arg) document_onkeypress(doc)) End Sub
Private Sub document_onmousemove(doc As HtmlDocument) Dim ev As New EventObject(doc.Window) Dim pt As New Point(ev.GetInt("x"), ev.GetInt("y")) Dim client As New Point(ev.GetInt("clientX"), ev.GetInt("clientY")) Dim offset As New Point(ev.GetInt("offsetX"), ev.GetInt("offsetY")) Dim src As Object = ev.Get("srcElement")
TextBox1.Text = $"{pt}, {client}, {offset}, <{CallByName(src, "tagName", CallType.Get)}>" End Sub
Private Sub document_onkeypress(doc As HtmlDocument) Dim ev As New EventObject(doc.Window) Dim altKey = CBool(ev.Get("altKey")) Dim ctrlKey = CBool(ev.Get("ctrlKey")) Dim shiftKey = CBool(ev.Get("shiftKey")) Dim keyCode As Char = ChrW(ev.GetInt("keyCode"))
Dim s As String = keyCode If altKey Then s &= " + Alt" If ctrlKey Then s &= " + Ctrl" If shiftKey Then s &= " + Shit"
TextBox2.Text = s End Sub Private Class EventObject Private ev As Object Public Sub New(win As HtmlWindow) ev = CallByName(win.DomWindow, "event", CallType.Get) End Sub Public Function [Get](memberName As String) As Object Return CallByName(ev, memberName, CallType.Get) End Function Public Function GetInt(memberName As String) As Integer Return CInt([Get](memberName)) End Function End Class End Class
|
|