Exposing Windows Forms Controls as ActiveX controls

转:http://www.codeproject.com/cs/miscctrl/exposingdotnetcontrols.asp?df=100&forumid=2373&exp=0&select=1359005

  • Download demo project - 15 Kb

This article will describe how to utilise Windows Forms controls outside of .NET. In a recent MSDN magazine article on .NET Interop available here, various ways of exposing .NET objects to 'legacy' environments are discussed, including the exposure of Windows Forms controls as ActiveX controls.

The problem is that the goalposts have moved since the article was written as Beta 2 is now available, and unfortunately this support has been removed - see this posting on the .NET list at http://discuss.develop.com.

The following image shows a control, written purely within .NET, hosted within an ActiveX control container - in this instance tstcon32.exe.

As Beta1 supported this facility, and being somewhat inquisitive, I decided to see if I could find a way to expose controls anyway. The attached project creates the 'Prisoner' control, which won't set the world on fire but does show the main things you need to do in order to get a .NET control up & running within VB6.

CAVEAT: As this support has been dropped from Beta2 of .NET, don't blame me if it fries your PC or toasts the cat.

Now that's out of the way, how's it done?.

Writing the control

  1. Create a new control project from within Visual Studio - my examples are all in C# but VB.NET could also be used.

     

  2. Add controls etc to the form, put in the code etc.

     

  3. Add in the following using clauses...

     

    using System.Runtime.InteropServices;using System.Text;using System.Reflection;using Microsoft.Win32;
  4. Attribute your class so that it gets a ProgID. This isn't strictly necessary as one will be generated, but it's almost always best to be explicit.
    [ProgId("Prisoner.PrisonerControl")][ClassInterface(ClassInterfaceType.AutoDual)]

    This assigns the ProgID, and also defines that the interface exposed should be 'AutoDual' - this crufts up a default interface for you from all public, non-static members of the class. If this isn't what you want, use one of the other options.

  5. Update the project properties so that your assembly is registered for COM interop.

     

    If you're using VB.NET, you also need a strong named assembly. Curiously in C# you don't - and it seems to be a feature of the environment rather than a feature of the compiler or CLR.

  6. Add the following two methods into your class.

     

    Collapse
    [ComRegisterFunction()]public static void RegisterClass ( string key ){// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need itStringBuilder sb = new StringBuilder ( key ) ;sb.Replace(@"HKEY_CLASSES_ROOT\","") ;// Open the CLSID\{guid} key for write accessRegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);// And create the 'Control' key - this allows it to show up in // the ActiveX control container RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;ctrl.Close ( ) ;// Next create the CodeBase entry - needed if not string named and GACced.RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;inprocServer32.SetValue ( "CodeBase" , Assembly.GetExecutingAssembly().CodeBase ) ;inprocServer32.Close ( ) ;// Finally close the main keyk.Close ( ) ;}
    The RegisterClass function is attributed with ComRegisterFunction - this static method will be called when the assembly is registered for COM Interop. All I do here is add the 'Control' keyword to the registry, plus add in the CodeBase entry.

    CodeBase is interesting - not only for .NET controls. It defines a URL path to where the code can be found, which could be an assembly on disk as in this instance, or a remote assembly on a web server somewhere. When the runtime attempts to create the control, it will probe this URL and download the control as necessary. This is very useful when testing .NET components, as the usual caveat of residing in the same directory (etc) as the .EXE does not apply.

    [ComUnregisterFunction()]public static void UnregisterClass ( string key ){StringBuilder sb = new StringBuilder ( key ) ;sb.Replace(@"HKEY_CLASSES_ROOT\","") ;// Open HKCR\CLSID\{guid} for write accessRegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);// Delete the 'Control' key, but don't throw an exception if it does not existk.DeleteSubKey ( "Control" , false ) ;// Next open up InprocServer32RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;// And delete the CodeBase key, again not throwing if missing k.DeleteSubKey ( "CodeBase" , false ) ;// Finally close the main key k.Close ( ) ;}

    The second function will remove the registry entries added when (if) the class is unregistered - it's always a good suggestion to tidy up as you go.

Now you are ready to compile & test your control.

Testing the Control

For this example I have chosen tstcon32.exe, which is available with the installation of .NET. The main reason I've used this rather than VB6 is that I don't have VB6 anymore.

Inserting the Control

First up you need to insert your control, so to do that choose Edit -> Insert New Control, and choose your control from the dropdown...

This will result in a display as shown at the top of the article, if you're following along with my example code.

Testing Methods

The example control only includes one method, 'Question'. To test this within TstCon32, choose Control -> InvokeMethods from the menu, and select the method you want to call. Note that because I defined the interface as AutoDual, I get gazillions of methods. If you implement an interface and expose this as the default interface then the list of methods will be more manageable.

Click on the 'Invoke' button will execute the method, which in this instance displays the obligatory message box.

Wrap Up

Dropping support for creating ActiveX controls from Windows Forms controls is a pain, and one decision I wish Microsoft had not made.

This article presents one way of exposing .NET controls as ActiveX controls, and seems to work OK. Having said that, I've not exhaustively tested this and who knows what bugs might be lurking in there. I haven't delved into events yet, nor property change notifications, so there's some fun to be had there if you like that sort of thing.

The .NET framework truly is the best thing since sliced bread, but the lack of support for creating ActiveX controls from Windows Forms controls is inconvenient. There are many applications out there (ours included) which can be extended with ActiveX controls. It would be nice given the rest of the support in the framework to be able to expose Windows Forms controls to ActiveX containers, and maybe someday the support will be available.

About Morgan Skinner


Now working in Microsoft Developer Services, based in the UK.

I loved .NET so much I had to join the company!.

Click here to view Morgan Skinner's online profile.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/549765.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QT_C++

QT_C C 与 C 区别&#xff1a;  面向过程&#xff1a;吃&#xff08;狗&#xff0c;屎&#xff09; 面向对象&#xff1a;狗. 吃&#xff08;屎&#xff09; ^ . ^ 博客&#xff1a;https://www.runoob.com/cplusplus/cpp-tutorial.html 插入符&#xff1a;<< 控制符…

静态路由的实现

在路由器A上做如下配置&#xff1a;router(config)#hostname AA(config)#interface f0/0A(config-if)#ip address 192.168.1.1 255.255.255.0 A(config-if)#no shutdownA(config)#interface f0/1A(config-if)#ip address 192.168.2.1 255.255.255.0 A(config-if)#no shutdownA(…

2019-08-09 纪中NOIP模拟赛B组

T1 [JZOJ1035] 粉刷匠 题目描述 windy有N条木板需要被粉刷。 每条木板被分为M个格子。 每个格子要被刷成红色或蓝色。 windy每次粉刷&#xff0c;只能选择一条木板上一段连续的格子&#xff0c;然后涂上一种颜色。 每个格子最多只能被粉刷一次。 如果windy只能粉刷 T 次&#x…

vue3实现打字机的效果

前言&#xff1a; vue3项目中实现打字机的效果。 实现效果&#xff1a; 实现步骤&#xff1a; 1、安装插件 npm i vue3typed 2、main.js中配置 import vuetyped from vue3typedconst app createApp(App) // 挂载打字机的全局方法 app.use(vuetyped) 3、界面使用 <vuet…

hightopo学习系列:hightopo介绍(一)

起因 新的软件主管来公司以后&#xff0c;有整整2周的时间没有搭理前端开发。就在这周一快下班的时候&#xff0c;突然和我说话了。问了我一些以前用的图形库&#xff0c;并让我开始了解hightopo。甩给了我一个全拼&#xff0c;就拂袖而去&#xff0c;留下一脸懵逼的我。 没办…

Unity工程无代码化

目的 Unity默认是将代码放入工程&#xff0c;这样容易带来一些问题。1. 代码和资源混合&#xff0c;职能之间容易互相误改。2. 当代码量膨胀到一定程度后&#xff0c;代码的编译时间长到无法忍受。新版的unity支持通过asmdef来将代码分成多个dll工程&#xff0c;有所缓解。所以…

曾国藩传 读后感

转载于:https://www.cnblogs.com/eat-too-much/p/11335113.html

深入C#学习系列一:序列化(Serialize)、反序列化(Deserialize)

深入C#学习系列一&#xff1a;序列化(Serialize)、反序列化(Deserialize) 序列化又称串行化&#xff0c;是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化&#xff0c;或者将这种对象从一个地方传输到另一个地方。.NET框架提供了两…

十一月·飘·立冬

十一月的南粤叶依然青翠在枝头与秋风和舞落叶遍地的诗意画面在博客生活逝如流年 渐走渐淡回忆飘然而来又飘然而去秋的最后一天放下回忆 飘去天涯飘不要说也不要问目光交错的一瞬注定了今生缘分此情可以见真心春风急 秋风也狠乱乱纷纷 是红尘浮浮沉沉 似幻似真金枝玉叶的结…

Centos 系统安装NetCore SDK命令以及一系列操作(1)

17年买的jesse老师的课程&#xff0c;虽然说NetCore出来很久了&#xff0c;自己打入行的时候就奔它去的&#xff0c;但。。。。废话不说了&#xff0c;还是自己做了再说吧&#xff0c; 首先需要一个Centos系统来让我们开始玩&#xff0c;下载地址&#xff1a;https://www.cento…

如何高效地判断奇数和偶数

在我们日常的编程当中&#xff0c;常常会遇到判断某个整数属于奇数还是偶数的情况。 大家一般的处理做法是用这个整数和2取模。然后判断是等于1还是等于0。 这里&#xff0c;我要为大家介绍一种快速有效的判断做法&#xff0c;利用2进制进行判断。 大家都知道&#xff0c;奇数的…

Windows Azure Traffic Manager (6) 使用Traffic Manager,实现本地应用+云端应用的高可用...

《Windows Azure Platform 系列文章目录》 注意&#xff1a;本文介绍的是使用国内由世纪互联运维的Azure China服务。 以前的Traffic Manager&#xff0c;背后的Service Endpoint必须是Azure数据中心的Cloud Service。 现在最新的Traffic Manager&#xff0c;Endpoint不仅仅支持…

Windows Azure Cloud Service (17) Role Endpoint

《Windows Azure Platform 系列文章目录》 在Windows Azure平台中&#xff0c;用户最多可以对以个Role指定5个Endpoint。而一个Hosted Service最多允许包含5个Role,所以说在一个Hosted Service中用户最多能定义25个Endpoint。 而对于每一个Endpoint&#xff0c;使用者需要设定如…

sentry + vue实现错误日志监控

起因 项目采用vue全家桶开发&#xff0c;现在拟嵌入sentry&#xff0c;实现对于线上网站进行错误日志记录。其实上传日志很简单&#xff0c;基本配置就可以了&#xff0c;但是上传配套的sourcemap则颇为费劲。这里记录一下使用心得。 实施步骤 上传日志 sentry使用文档&…

OSPF单域实验报告

1.1 实验任务<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />(1) 配置Loopback地址作为路由器的ID。(2) 配置OSPF的进程并在相应的接口上启用。(3) OSPF起来后&#xff0c;更新计时器。1.2 实验环境和网络拓扑<?xm…

sql server 2005 COUNT_BIG (Transact-SQL)

返回组中的项数。COUNT_BIG 的用法与 COUNT 函数类似。两个函数唯一的差别是它们的返回值。COUNT_BIG 始终返回 bigint 数据类型值。COUNT 始终返回 int 数据类型值。后面可能跟随 OVER 子句。 Transact-SQL 语法约定 语法 COUNT_BIG ( { [ ALL | DISTINCT ] expression } | * …

《浏览器播放RTSP方案》之 VLC插件播放RTSP视频流

VLC插件播放RTSP视频流多版本chrome安装安装vlc软件开启浏览器的NPAPI设置编写测试页插件设置其他问题最后目前网页对于RTSP流直接播放不支持&#xff0c;目前有插件和转流两种方式&#xff0c;这里针对vlc插件播放做一个简单的梳理。 查看官网教程 vlcWebPlugin, 得知其浏览器…

通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行(转)

本文将讨论&#xff1a; • 缓存和 Forms 身份验证 • 视图状态和会话状态 • 配置文件属性序列化 • 线程池饱和 • 模拟和设置配置文件 本文使用了下列技术&#xff1a; .NET Framework、ASP.NET、Windows Server 2003 本页内容 LoadControl 和输出缓存会话和输出缓存Fo…

如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便)

.net 1.1中的解决方法(转)1建立一个DataSetHelper类(DataSetHelper.cs) publicclassDataSetHelper...{ public DataSet ds; public DataSetHelper(ref DataSet DataSet) ...{ ds DataSet; } public DataSetHelper() ...{ ds null; } p…

Centos 系统安装NetCore SDK命令以及一系列操作(2)

ok,接下来安装dotnetCore的SDK&#xff0c;访问&#xff1a;https://dotnet.microsoft.com/download&#xff0c; 如下图&#xff0c;选择Linux&#xff0c; 选择一下这个Linux的发行版&#xff0c;我们选择Centos就行&#xff0c;然后执行这些命令去安装SDK&#xff0c;地址如…