日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

WinForm与脚本的交互

發(fā)布時(shí)間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WinForm与脚本的交互 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?這是去年學(xué)習(xí)SmartClient時(shí)寫(xiě)下的,有興趣可以看看
??? 將Winform Control嵌入IE,很多時(shí)候需要JS腳本與Control進(jìn)行交互。一方面是在腳本中使用控件的屬性,調(diào)用控件的方法,另外一方面是腳本中能夠響應(yīng)控件的事件。對(duì)于第一個(gè)問(wèn)題較為簡(jiǎn)單,我們還可以在腳本中使用控件屬性的值,也可以給屬性賦值,也可以調(diào)用控件的方法。

?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1、 腳本中傳參數(shù),使用控件的屬性,調(diào)用控件方法

1)在控件Test_JsUseCtrl.cs中,添加textBox1,定義屬性Str,如

???? private string str;

???? public string Str

???? {

???? ???? get{return str;}

???????? set{???? str = value;textBox1.Text = value;}

}

2)定義public方法用于顯示textBox1內(nèi)容如

???? public void ShowText()

???? {

???? ???? MessageBox.Show(textBox1.Text,"TextBox的內(nèi)容,MessageBoxButtons.OK,MessageBoxIcon.Information);

}

3)在頁(yè)面添加TextBox,Button等,點(diǎn)擊Button1可以將頁(yè)面輸入值賦給控件屬性,點(diǎn)擊Button2可以調(diào)用控件方法

function Button1_onclick() {

Test_JsUseCtrl.Str = Text1.value;

}

function Button2_onclick() {

Test_JsUseCtrl.ShowText();

}

?

上面部是點(diǎn)擊或者觸發(fā)頁(yè)面控件事件來(lái)獲得控件的屬性和方法,下面部分就是控件通過(guò)事件來(lái)調(diào)用腳本中的方法,即在腳本中響應(yīng)控件事件。

?

2JS腳本中響應(yīng)控件事件

1)在控件中添加接口ClickEvent

???? // Source interface for events to be exposed

???? // Add GuidAttribute to the source interface to supply an explicit System.Guid.

???? // Add InterfaceTypeAttribute to indicate that interface is the IDispatch interface.

???? [System.Runtime.InteropServices.GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]

???? [System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch)]

???? public interface ControlEvents

???????? // Add a DisIdAttribute to any members in the source interface to???????? // specify the COM DispId.

???? {

???? ???? [System.Runtime.InteropServices.DispIdAttribute(0x60020000)]

???????? void ClickEvent(int x, int y);

???? }

?

2、為控件添加屬性???

// Add a ComSourceInterfaces attribute to the control to identify??????? //the list of interfaces that are exposed as COM event sources.

???? [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None),System.Runtime.InteropServices.ComSourceInterfaces(typeof(ControlEvents))]

???? public class MyWindowControl : System.Windows.Forms.UserControl //, ComInteropControlInterface

2、? Button的Click事件中調(diào)用接口方法

???? if (ClickEvent != null)

???? {

???? ???? ClickEvent(0, 0);

}

3、? JS腳本中響應(yīng)接口事件

function ctrl::ClickEvent(a,b)

{

alert(String(a)+"+"+String(b));

}

腳本響應(yīng)控件的事件稍微復(fù)雜?

注:如果彈出關(guān)于安全方面的提示,把IE->安全->信任站點(diǎn)s->自定義級(jí)別下的,“對(duì)沒(méi)有標(biāo)記為安全的ActiveX控件進(jìn)行初始化和腳本運(yùn)行”,設(shè)為啟用。(上面的思路就是將Control作為ActiveX)

參考:

http://chs.gotdotnet.com/quickstart/winforms/doc/WinFormsIeSourcing.aspx

HOW TO: Sink Managed C# Events in Internet Explorer Script

PRB: Security Exception When You Use Event Handlers in Internet Explorer??? ???

轉(zhuǎn)載于:https://www.cnblogs.com/Sniper/archive/2004/08/09/31339.html

總結(jié)

以上是生活随笔為你收集整理的WinForm与脚本的交互的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。