So let us 反义疑问句start giv...

南倭瓜 的BLOG
用户名:南倭瓜
文章数:122
访问量:12460
注册日期:
阅读量:5863
阅读量:12276
阅读量:350583
阅读量:1049289
51CTO推荐博文
By , 650) this.width=650;" src="/script/Ratings/Images/stars-fill-lg.png" style="margin:0padding:0border:0" alt="stars-fill-lg.png" />650) this.width=650;" src="/script/Ratings/Images/stars-empty-lg.png" style="margin:0padding:0border:0" alt="stars-empty-lg.png" />4.47 (50 votes)Rate:0IntroductionAt my job, I have had a project where I needed to deliver a solution for creating, distributing and running ActiveXcontrols written in .NET. This article summarizes all of it. Everything that is in this article can be found on the Web, but believe me).Using the Code - Contains .NET source code for the ActiveX - Contains example of the *.ini file needed to create CAB- Example of the HTML file that embeds ActiveX objectSo Let Us Start...1) Creating .NET ActiveXThe first step you have to do is to create the ActiveX control650) this.width=650;" src="/script/Forums/Images/smiley_smile.gif" align="top" alt="Smile | :)" style="margin:0padding:0border:0height:" /> .You can find a good introduction to this subject .This article describes how to create and expose Windows Forms on the Web site. Since there is no such support in the .NET Framework, WinForms are wrapped into ActiveX which is exactly what we need. In summary, what we need is to create a class that will be marked with those attributes:650) this.width=650;" id="preimg0" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | [ProgId("MyClassName")]
[Guid("MyGUID")] sdf
[ComVisible(true)] Where:ProgId is the unique name of the class that will be exposed as COM objectClassInterface is the type of the COM interface that will wrap our .NET classGuid is the unique GUID that will expose our class to be used as COM object. To create a new GUID, you can use the tool in Visual Studio Tools -& Create GUIDComVisible tells that our class can be used as COM objectRegistering COM object means that you need to make some entries in the registry. You can do this manually or you can write methods in your ActiveX that will do that for you when you register it with tools like regasm:650) this.width=650;" id="preimg1" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | The registry key of the control[ComRegisterFunction()]
publicstaticvoid RegisterClass ( string key )
The registry key[ComUnregisterFunction()]
publicstaticvoid UnregisterClass ( string key)
}Having those methods implemented, you can now use command: regasm /codebase MyAssemblie.dll which will run the RegisterClass method and regasm /u MyAssemblie.dll which will run the UnregisterClassmethod.To expose .NET methods and properties in your class to be available as COM methods and properties, you have to add markup [ComVisible(true)] to them, i.e.:650) this.width=650;" id="preimg2" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | [ComVisible(true)]
publicvoid Open()
System.Windows.Forms.MessageBox.Show(MyParam);
[ComVisible(true)]
publicstring MyParam
return myP
} You have to remember that your assembly must be signed using strong name key, this way you will have the ability to use more complicated controls, assemblies and features (like passing events back to the browser). To create new SNK, you can use tool: sn.exe (you can find it in the .NET SDK):650) this.width=650;" id="preimg3" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | sn Ck Kosmala.Michal.ActiveXReport.snkOnce you create a strong name key, copy it into your project path and put path to it in the AssemblyInfo.cs file in:650) this.width=650;" id="preimg4" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | [assembly:AssemblyKeyFile("../../Kosmala.Michal.ActiveXReport.snk")] Once you have your class registered as COM, you can now use it in your Web page.2) Using ActiveX on the HTML PageTo start using your ActiveX in the HTML file, you just have to put one tag:650) this.width=650;" id="preimg5" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | &OBJECTid="OurActiveX"name=”OurActiveX"classid="clsid:MyGuid"VIEWASTEXTcodebase="OurActiveX.cab"&Where:id and name are values that will be used to access our ActiveX from JavaScriptclassidis the GUID of our ActiveX that we have put in [Guid("MyGUID")] section of our C# codeCodebaseis the path to the file which should be launched when ActiveX with our GUID is not found. This is the place where the installation program (wrapped in CAB) should be placed.Let us now forget for a moment about the codebase section since we are doing everything on our computer and we can register ActiveX with using regasm.Now that our Web page knows what kind of ActiveX we want to use, we can actually start using it.To do that, we just need to write some JavaScript code on the page. This code can look something like this:650) this.width=650;" id="preimg6" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | &script language="javascript"&
function OpenActiveX()
document.OurActiveX.MyParam = "Hi I am here."
document.OurActiveX.Open();
catch(Err)
alert(Err.description);
OpenActiveX();
&/script&This script will assign value to MyParam and invoke the Open() method from our C# code which will return openMessageBox with “I am here” text. Open is just a name of the method, it can be named anything that you can think of.Assigning variables works both ways. You can also read from ActiveX field using JavaScript.Remember to set proper security values in your Internet Explorer that will allow running ActiveX.3) Creating .cab ComponentNow it is time to create the CAB file that will be launched when the browser won't find our ActiveX registered on the system. First of all, you have to create a setup program that will either be MSI or it will wrap MSI into setup.exefile. I have done this by using InstallShield application because this is what my company standard was, but you can use Visual Studio to do that (although I have never tested it). The important thing is that your setup has to register your ActiveX on a target computer. This might require having administration privileges.Once you have your setup, you have to create CAB. To do that you can use cabsdk which you can find .Now, the important thing about our CAB is to attach not only our setup file but also *.ini file that will describe what CAB contains and what file should be launched. Our OurActiveX.ini can look something like this:650) this.width=650;" id="preimg7" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | [version]
signature="$&place w:st="""""on""""" /&&city w:st="""""on""""" /&CHICAGO&/city /&&/place /&$"
AdvancedINF=2.0
[Add.Code]
setup.exe=setup.exe
[setup.exe]
file-win32-x86=thiscab
clsid={MyGuid}Where:[Add Code] section describes what sections of the file are mapped to what file. In our case, there is only onesetup.exe file that is mapped by section with the same name.[setup.exe] section is the one that is defined in the previous section. It is saying that file that it is mapped to should be launched when accessed with proper clsid. In this case, this is the MyGuid that is defined in our C# code and also in &OBJECT& tag.Now that we have all files that we need, we can create CAB file using cabsdk by using the command:650) this.width=650;" id="preimg8" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | cabarc.exe n OurActiveX.cab OurActiveX.inf setup.exeVoila. Our basic ActiveX with distribution property is ready.Now for something more sophisticated650) this.width=650;" src="/script/Forums/Images/smiley_smile.gif" align="top" alt="Smile | :)" style="margin:0padding:0border:0height:" /> .4) Invoking JavaScript Methods from ActiveXIt might be necessary to send an event from ActiveX that JavaScript will catch, i.e. when you want to control redirection of the Web page when you close ActiveX.To do that, you have to update your C# code. You have to add a new interface and a new delegate:650) this.width=650;" id="preimg9" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | publicdelegatevoid ControlEventHandler(string redirectUrl);
[Guid(NewGuid)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
publicinterface ControlEvents
[DispId(0x)]
void OnClose(string redirectUrl);
} This code creates an interface wrapper that will be visible outside as COM object. Every new event that will be available for COM must be marked DispId attribute.(Creating .NET to COM objects /archives/archive281.html)Now we have to inherit our new interface in ActiveX control:650) this.width=650;" id="preimg10" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | [ClassInterface(ClassInterfaceType.AutoDual),
ComSourceInterfaces(typeof(ControlEvents))]Since we are implementing ControlEvents interface, now we have to create OnClose event in our class which we can raise in our ActiveX:650) this.width=650;" id="preimg11" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | publicevent ControlEventHandler OnCAfter compilation of this code, we have to register our ActiveX control (regasm) with additional parameter “/tlb” which will extract tlb COM library from our DLL and it will register it on our system:650) this.width=650;" id="preimg12" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | regasm /codebase /tlb MyAssemblie.dllHaving this kind of code, we can add handling of this event in JavaScript:650) this.width=650;" id="preimg13" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | &script language="javascript"&
function OurActiveX::OnClose(redirectionUrl)
window.location = hostUrl + "/" + redirectionU
&/script&Event is being attached only after the whole page has been loaded, so to use it we have to change the way of invoking JavaScript function: OpenActiveX. Now we have two ways of doing it:We can attach our function to i.e. button and our ActiveX will run when we will click on it:650) this.width=650;" id="preimg14" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | &input type=button onclick=javascript:OpenActiveX()&If we want that it will be started automatically after the page will be loaded, we have to attach our function to body event “onload”:650) this.width=650;" id="preimg15" src="/images/minus.gif" height="9" width="9" style="margin:0padding:0border:0height:" alt="minus.gif" /> Collapse | &body onload=OpenActivex()&You have to remember that your ActiveX assembly has to be signed and registered with additional parameter “/tlb”.This is a very simple ActiveX but it can call any WinForms control or application that your user has rights to.Points of InterestAs you can see, it is very easy to create and distribute ActiveX in .NET, but it is very difficult to find right answers on the Web probably because not so many people are using ActiveX nowadays.I hope that this article will cut your search time as much as possible.History4th March, 2008: Initial postLicenseThis article, along with any associated source code and files, is licensed under
了这篇文章
类别:┆阅读(0)┆评论(0)英文励志名言_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
英文励志名言
上传于||文档简介
&&英​文​励​志​名​言
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢}

我要回帖

更多关于 let us 反义疑问句 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信