概要
次のような構造を持つオブジェクト内のFrameのDocumentの内容(★印)を取得したいです。
wbm Control { Sytem.Windows.Forms.Control } currentDocument { System.Windows.Forms.HtmlDocument } Document { System.Windows.Forms.HtmlDocument } Window { System.Windows.Forms.HtmlWindow } Frames { System.Windows.Forms.HtmlWindowCollection } htmlFramesCollection2 Document Body InnerHtml m_MRE { System.Threading.ManualResetEvent } WebBrowser { WxtendedWebBrowser } base { Hoge.ExtendedWebBrowser } Document { System.Windows.Forms.HtmlDocument } Body { System.Windows.Forms.HtmlElement } ★InnerHtml <FRAMESET (構造は下記)> Application (COMオブジェクト) Document { mshtml.HTMLDocumentClass } frames { mshtml.HTMLHtmlElementClass }
htmlFramesCollection2内にFrameがある場合は
currentDocument.Window.Frames[name].Document
のような形で取得できているのですが、新規にIEで開いてしまうウィンドウをWebBrowserで開く下記のような形で拡張しているため、目的のFrameはWebBrowser内にあります。
lang
1 public class ExtendedWebBrowser : WebBrowser 2 { 3 private AxHost.ConnectionPointCookie cookie; 4 private WebBrowser2EventHelper helper; 5 [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)] 6 [DispIdAttribute(200)] 7 8 public object Application 9 { 10 get 11 { 12 if (this.ActiveXInstance == null) 13 { 14 throw new AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet); 15 } 16 return this.ActiveXInstance.GetType().InvokeMember("Application", System.Reflection.BindingFlags.GetProperty, null, this.ActiveXInstance, null); 17 18 19 } 20 } 21 22 [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)] 23 [DispIdAttribute(552)] 24 public bool RegisterAsBrowser 25 { 26 get 27 { 28 if (this.ActiveXInstance == null) 29 { 30 throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet); 31 } 32 return (bool)this.ActiveXInstance.GetType().InvokeMember("RegisterAsBrowser", System.Reflection.BindingFlags.GetProperty, null, this.ActiveXInstance, null); 33 34 35 } 36 set 37 { 38 if (this.ActiveXInstance == null) 39 { 40 throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertySet); 41 } 42 this.ActiveXInstance.GetType().InvokeMember("RegisterAsBrowser", System.Reflection.BindingFlags.SetProperty, null, this.ActiveXInstance, new object[] { value }); 43 } 44 } 45 46 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] 47 protected override void CreateSink() 48 { 49 base.CreateSink(); 50 helper = new WebBrowser2EventHelper(this); 51 cookie = new AxHost.ConnectionPointCookie(this.ActiveXInstance, helper, typeof(DWebBrowserEvents2)); 52 } 53 54 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] 55 protected override void DetachSink() 56 { 57 if (cookie != null) 58 { 59 cookie.Disconnect(); 60 cookie = null; 61 } 62 base.DetachSink(); 63 } 64 65 public event WebBrowserNewWindow2EventHandler NewWindow2 = (o, e) => { }; 66 67 protected virtual void OnNewWindow2(WebBrowserNewWindow2EventArgs e) 68 { 69 NewWindow2(this, e); 70 } 71 72 private class WebBrowser2EventHelper : StandardOleMarshalObject, DWebBrowserEvents2 73 { 74 private ExtendedWebBrowser parent; 75 76 public WebBrowser2EventHelper(ExtendedWebBrowser parent) 77 { 78 this.parent = parent; 79 } 80 81 public void NewWindow2(ref object ppDisp, ref bool cancel) 82 { 83 var e = new WebBrowserNewWindow2EventArgs(ppDisp); 84 this.parent.OnNewWindow2(e); 85 ppDisp = e.ppDisp; 86 cancel = e.Cancel; 87 } 88 } 89 } 90 91 public delegate void WebBrowserNewWindow2EventHandler(object sender, WebBrowserNewWindow2EventArgs e); 92 93 public class WebBrowserNewWindow2EventArgs : CancelEventArgs 94 { 95 public object ppDisp { get; set; } 96 97 public WebBrowserNewWindow2EventArgs(object ppDisp) 98 { 99 this.ppDisp = ppDisp; 100 } 101 } 102 103 [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")] 104 [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 105 [TypeLibType(TypeLibTypeFlags.FHidden)] 106 public interface DWebBrowserEvents2 107 { 108 [DispId(251)] 109 void NewWindow2([InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp, [InAttribute(), OutAttribute()] ref bool cancel); 110 }
下記は使用する側のコードです。
lang
1 private void WebBrowser_NewWindow2(object sender, WebBrowserNewWindow2EventArgs e) 2 { 3 // 新しい WebBrowser の初期化 4 WebBrowser = new ExtendedWebBrowser(); 5 WebBrowser.Dock = DockStyle.Fill; 6 Control.Controls.Add(WebBrowser); 7 8 var tabPage = new TabPage(); 9 10 // 新しい WebBrowser に表示させる設定 11 e.ppDisp = this.WebBrowser.Application; 12 WebBrowser.RegisterAsBrowser = true; 13 WebBrowser.NewWindow2 += WebBrowser_NewWindow2; 14 }
MSDNに
An HTML document may contain frames, which are different windows inside of the WebBrowser control. Each frame displays its own HTML page. The Frames collection is available through the Window property.
という記述があり、Window下にないとアクセスできないのではないかと思うのですが、WebBrowser内にWindowプロパティがなく扱えていません…
Frameの構造
<FRAMESET id=FRAMESET2 onload=SetLoadFlg() border=0 frameBorder=0 rows=300px,*,40px><FRAME noResize src="/hoge/html/hogehoge.html" name=M1 scrolling=no><FRAME noResize src="/web21/execJ/servlet/ACH99OMCL_WEBCNTL?_W_SubFname=M2" name=M2><FRAME noResize src="/moge/html/mogemoge.html" name=M3 ></FRAMESET>動作環境
・.NET Frame Work 4.5
・Visual Studio2013
目的
サイト上のFrame内のDocumentを取得した上で、その中のリンク要素を拾ってサイト内の遷移を勧めたいです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー