htmlのinputとtextareaをクリックした際に、Windowsフォームを表示する。
ここではかなウェルというブラウザを例にしています。
かなウェル-> http://lsl.way-nifty.com/kanawel/
ブラウザコンポーネント:MyTabWebBrowser.cs
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using ABCS.Forms; namespace ABCS.Classes { public class MyTabWebBrowser : WebBrowserEx { protected bool isDocumentComplete_ = false; protected TabPage tab_ = null; protected int titleMaxLength_ = 5; protected Form parentForm_ = null; protected string title_ = ""; protected string beforeurl_ = null; protected TabControl parentTabControl_ = null; protected Panel parentPanel_ = null; protected frm50 frm50_ = null; public string SiteTitle { get { return this.title_; } } public void SetTitle2Form() { this.parentForm_.Text = this.title_; } public bool IsDocumentCompleted { get { return this.isDocumentComplete_; } } public MyTabWebBrowser(Form parentForm, TabControl parentTabControl ,TabPage tab, int titleMaxLength, frm50 Frm50, Panel parentPanel) : base() { this.frm50_ = Frm50; this.parentPanel_ = parentPanel; //this.DocumentCompleted += this.evDocumentCompleted; this.parentTabControl_ = parentTabControl; this.tab_ = tab; this.titleMaxLength_ = titleMaxLength; this.parentForm_ = parentForm; this.NewWindow2 += new WebBrowserNewWindow2EventHandler(webBrowser_NewWindow2); this.BeforeNewWindow += new EventHandler(OnMyBeforeNewWindow); } protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e) { base.OnDocumentCompleted(e); isDocumentComplete_ = true; this.title_ = this.DocumentTitle.Trim(); this.parentForm_.Text = this.title_; this.tab_.Text = this.title_.Substring(0,(this.titleMaxLength_< this.title_.Length ) ? this.titleMaxLength_ : this.title_.Length); this.ChainOnDocumentCompleted(e); } protected override void OnNavigating(WebBrowserNavigatingEventArgs e) { base.OnNavigating(e); isDocumentComplete_ = false; } protected void OnMyBeforeNewWindow(object sender, EventArgs e) { WebBrowserExtendedNavigatingEventArgs we = e as WebBrowserExtendedNavigatingEventArgs; if (we != null) { this.beforeurl_ = we.Url; } } // 新しくウィンドウを開かれようとするときに発生する void webBrowser_NewWindow2(object sender, WebBrowserNewWindow2EventArgs e) { MyTabPageWithWeb tab = new MyTabPageWithWeb(this.parentForm_,this.parentTabControl_, this.parentPanel_,this.beforeurl_, this.frm50_); // 新しい WebBrowser の初期化 //WebBrowserEx newBrowser = new WebBrowserEx(); //newBrowser.Dock = DockStyle.Fill; // 新しい WebBrowser のコンテナ(下記はタブの場合) //var tabPage = new TabPage(); //tab.Controls.Add(newBrowser); this.parentTabControl_.TabPages.Add(tab); // 新しい WebBrowser に表示させる設定 //e.ppDisp = newBrowser.Application; //newBrowser.RegisterAsBrowser = true; e.ppDisp = tab.ChildWebBrowser.Application; tab.ChildWebBrowser.RegisterAsBrowser = true; // 新しい WebBrowser からさらにウィンドウを開かれるときも同じようにする //newBrowser.NewWindow2 += webBrowser_NewWindow2; tab.ChildWebBrowser.NewWindow2 += webBrowser_NewWindow2; //newBrowser.Navigate(this.beforeurl_); //tab.ChildWebBrowser.Navigate(this.beforeurl_); this.parentTabControl_.SelectTab(tab); e.Cancel = true; } #region ソフトキーボード機能 ///Clickイベントを処理するメソッドを表します。 public delegate void MyHtmlElementEventHandler(object sender, MyHtmlElementEventArgs e); [Category("アクション"), Description("クリックした時に発生します。")] public event MyHtmlElementEventHandler MyCustomClick; //#region//Tabマウスイベント void MyTabWebBrowser_MyCustomClick(object sender, System.Windows.Forms.HtmlElementEventArgs e) { try { MyHtmlElementEventArgs E = new MyHtmlElementEventArgs(e, this); this.MyCustomClick(sender, E); } catch (NullReferenceException) { } } protected void ChainOnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e) { //base.OnDocumentCompleted(e); HtmlElementCollection ecol = this.Document.Body.GetElementsByTagName("input"); foreach (HtmlElement el in ecol) { if (el.GetAttribute("type").ToString().ToLower() == "text" || el.GetAttribute("type").ToString().ToLower() == "password") { el.Click += new HtmlElementEventHandler(MyTabWebBrowser_MyCustomClick); } } HtmlElementCollection ecol2 = this.Document.Body.GetElementsByTagName("textarea"); foreach (HtmlElement el in ecol2) { el.Click += new HtmlElementEventHandler(MyTabWebBrowser_MyCustomClick); } } public int CurPosX = 0; public int CurPosY = 0; public void setCurPos() { // ブラウザ上のマウスの位置を取得 Point p = new Point(); p = this.PointToClient(MousePosition); this.CurPosX = p.X; this.CurPosY = p.Y; // 複数のWebBrowserで共有するなら・・・ // ((WebBroser)sender).PointToClient(MousePosition); // マウスの位置にあるエレメントを取得 //this.Document.GetElementFromPoint(p); } #endregion } ///TabMouseClickイベントのデータ public class MyHtmlElementEventArgs { private MyTabWebBrowser web_ = null; private System.Windows.Forms.HtmlElementEventArgs e_ = null; /// クリックされたタブに対応したタブのインデックス public MyHtmlElementEventArgs(System.Windows.Forms.HtmlElementEventArgs e, MyTabWebBrowser web) { this.e_ = e; this.web_ = web; } ///クリックされたタブに対応したタブページ public MyTabWebBrowser MyWeb { get { return this.web_; } } public System.Windows.Forms.HtmlElementEventArgs E { get { return this.e_; } } } }
クリックイベントを実装するクラス:TabPageの派生
void softkbd_clickがイベントハンドラ
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ABCS.Interfaces; using ABCS.Forms; namespace ABCS.Classes { public class MyTabPageWithWeb : TabPage { private MyTabWebBrowser web_ = null; private MyTabControlWidthRadio childTabControl_ = null; private TabControl ParentTabControl_ = null; private Panel ParentPanel_ = null; private frm50 Frm50_ = null; private Form parentForm_ = null; public MyTabWebBrowser ChildWebBrowser { get { return this.web_; } } public MyTabControlWidthRadio ChildTabControlWithRadio { get { return this.childTabControl_; } } public MyTabPageWithWeb(Form parentForm, TabControl parentTabControl_, Panel parentPanel,string url, frm50 Frm50) : base() { this.Frm50_ = Frm50; this.parentForm_ = parentForm; this.ParentTabControl_ = parentTabControl_; this.ParentPanel_ = parentPanel; //this.AllowDrop = true; this.web_ = new MyTabWebBrowser(parentForm, parentTabControl_, this, 10, this.Frm50_,this.ParentPanel_); this.web_.MyCustomClick += this.softkbd_click; //this.childTabControl_ = new MyTabControlWidthRadio(); /* //?Dock は後続の方が優先される(スタック形式?) ctabctrl.Dock = DockStyle.Top; tab.Controls.Add(ctabctrl); */ //Web表示 this.web_.Dock = DockStyle.Fill; this.Controls.Add(this.web_); // //?Dock は後続の方が優先される(スタック形式?) // //そのため、ctabctrlのaddをwebのaddより後方に移した。 // //(?を?に移した) //this.childTabControl_.Dock = DockStyle.Top; //this.Controls.Add(this.childTabControl_); //web.DocumentCompleted += webDocumentCompleted; this.web_.Navigate(url); } private void softkbd_click(object sender, MyHtmlElementEventArgs e) { //MessageBox.Show("focus"); HtmlElement el = sender as HtmlElement; if (el == null) return; MyTabWebBrowser web = e.MyWeb; web.setCurPos(); int x = web.CurPosX; int y = web.CurPosY; //MessageBox.Show("x=" + x + ",y=" + y); if ( ( this.Frm50_.Height < web.Height) && (y + this.Frm50_.Height > web.Height) ) { y = y - this.Frm50_.Height - 72; if (y < 0) y = 0; } if (x + this.Frm50_.Width > this.ParentPanel_.Width) { x = this.ParentPanel_.Width - this.Frm50_.Width; } int top = this.parentForm_.Top + this.ParentPanel_.Top+ y + 72; int left = this.parentForm_.Left+ this.ParentPanel_.Location.X + x; this.Frm50_.origText = el.InnerText; this.Frm50_.Top = top; this.Frm50_.Left = left; this.Frm50_.htmlElement = el; this.Frm50_.Show(); this.Frm50_.Focus(); } } }