用HTML做软件UI用到的的一些技术
生活随笔
收集整理的這篇文章主要介紹了
用HTML做软件UI用到的的一些技术
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
做WEB開發的想把網頁做成應用程序的界面,開發應用程序的又想把程序界面做得和WEB一樣。本文介紹一下用HTML做軟件UI用到的的一些技術。
其實HTML?UI也不是什么新鮮事了,Norton?Antivirus從幾年前的版本就開始用了,vs.net2002中的開始頁也用了這個技術。
from:http://wuchang.cnblogs.com/archive/2006/06/12/423978.html
[方案一,適用于vs2002~2005(vb/delphi等類似)]
1、導入web?browser?COM控件
2、實現IDocHostUIHandler接口,MSDN中有介紹WebBrowser Customization。
IDocHostUIHandler接口有十幾個方法,這里我們只關心這個:
void IDocHostUIHandler.GetExternal(out object ppDispatch)
當在瀏覽器腳本中調用?window.external時就會調用這個方法,我們需要在返回一個對象。如:
public class Form1 : Form, IDocHostUIHandler { public Form1() { InitializeComponent(); object flags = 0; object targetFrame = String.Empty; object postData = String.Empty; object headers = String.Empty; this.WebBrowser.Navigate("about:blank", ref flags, ref targetFrame, ref postData, ref headers); ICustomDoc cDoc = (ICustomDoc)this.WebBrowser.Document; cDoc.SetUIHandler((IDocHostUIHandler)this); this.WebBrowser.Navigate(@".", ref flags, ref targetFrame, ref postData, ref headers); } void IDocHostUIHandler.GetExternal(out object ppDispatch) { ppDispatch = new Hello(); } }
添加Hello類的代碼,注意:此類一定要加上ComVisible=true特性,或是給整個程序集加上[assembly:?ComVisible(?true?)]。
[ComVisible(true)] public class Hello { public void Haha(string msg) { MessageBox.Show(msg); } }
這樣,就可以在瀏覽器中用腳本這樣調用
<script language="JavaScript" id="clientEventHandlersJS"> function callHostUI(msg) { window.external.Haha(msg); } callHostUI("hello wuChang"); </script>
[方案二,適用于vs2005]
VS2005提供的WebBrowser控件,已經實現了IDocHostUIHandler接口,使用起來就更簡單了。
WebBrowser提供了 public Object ObjectForScripting { get; set; }
屬性,只需要這樣用就行了。
webBrowser1.ObjectForScripting = new Hello ();
調用瀏覽器用的腳本可以這樣
webBrowser1.Document.InvokeScript("js函數名", new String[] { "參數列表 " });
更多的內容在.net?FW?SDK?(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/P_System_Windows_Forms_WebBrowser_ObjectForScripting.htm里有介紹。
.net2.0在System.Windows.Forms?name?spaces中提供HtmlDocument、HtmlElement等訪問HTML元素的控件,使用方法在SDK里有,這里就不介紹了。
其實HTML?UI也不是什么新鮮事了,Norton?Antivirus從幾年前的版本就開始用了,vs.net2002中的開始頁也用了這個技術。
from:http://wuchang.cnblogs.com/archive/2006/06/12/423978.html
[方案一,適用于vs2002~2005(vb/delphi等類似)]
1、導入web?browser?COM控件
2、實現IDocHostUIHandler接口,MSDN中有介紹WebBrowser Customization。
IDocHostUIHandler接口有十幾個方法,這里我們只關心這個:
void IDocHostUIHandler.GetExternal(out object ppDispatch)
當在瀏覽器腳本中調用?window.external時就會調用這個方法,我們需要在返回一個對象。如:
public class Form1 : Form, IDocHostUIHandler { public Form1() { InitializeComponent(); object flags = 0; object targetFrame = String.Empty; object postData = String.Empty; object headers = String.Empty; this.WebBrowser.Navigate("about:blank", ref flags, ref targetFrame, ref postData, ref headers); ICustomDoc cDoc = (ICustomDoc)this.WebBrowser.Document; cDoc.SetUIHandler((IDocHostUIHandler)this); this.WebBrowser.Navigate(@".", ref flags, ref targetFrame, ref postData, ref headers); } void IDocHostUIHandler.GetExternal(out object ppDispatch) { ppDispatch = new Hello(); } }
添加Hello類的代碼,注意:此類一定要加上ComVisible=true特性,或是給整個程序集加上[assembly:?ComVisible(?true?)]。
[ComVisible(true)] public class Hello { public void Haha(string msg) { MessageBox.Show(msg); } }
這樣,就可以在瀏覽器中用腳本這樣調用
<script language="JavaScript" id="clientEventHandlersJS"> function callHostUI(msg) { window.external.Haha(msg); } callHostUI("hello wuChang"); </script>
[方案二,適用于vs2005]
VS2005提供的WebBrowser控件,已經實現了IDocHostUIHandler接口,使用起來就更簡單了。
WebBrowser提供了 public Object ObjectForScripting { get; set; }
屬性,只需要這樣用就行了。
webBrowser1.ObjectForScripting = new Hello ();
調用瀏覽器用的腳本可以這樣
webBrowser1.Document.InvokeScript("js函數名", new String[] { "參數列表 " });
更多的內容在.net?FW?SDK?(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/P_System_Windows_Forms_WebBrowser_ObjectForScripting.htm里有介紹。
.net2.0在System.Windows.Forms?name?spaces中提供HtmlDocument、HtmlElement等訪問HTML元素的控件,使用方法在SDK里有,這里就不介紹了。
轉載于:https://www.cnblogs.com/MaxWoods/archive/2008/09/25/1298684.html
總結
以上是生活随笔為你收集整理的用HTML做软件UI用到的的一些技术的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hp1020打印机连接一直显示安装程序?
- 下一篇: html5 canvas获取坐标,HTM