http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
Create an ActiveX using a Csharp Usercontrol Rating: none Environment: Visual Studio 2005 C#
This article is about creating ActiveX controls using a DotNet Usercontrol in Csharp. You can design all ActiveX features like: properties, methods and events.
(continued) <A HREF="http://63.236.18.118/RealMedia/ads/click_nx.ads/intm/webdev/www.codeguru.com/csharp/.net/net_general/comcom@120x60-1,125x125-1,125x600,125x800,468x60-1,468x60-2,accessunit,accessunit_one,accessunit_three,accessunit_two,ciu,cp1,cp10,cp11,cp12,cp13,cp14,cp2,cp3,cp4,cp5,cp6,cp7,cp8,cp9,fl1,fl2,fl3,fl4,fl5,flex,house_ribbon,marketplace01,marketplace02,marketplace03,marketplace04,marketplace05,marketplace06,sitetext-1!flex" > <IMG SRC="http://63.236.18.118/RealMedia/ads/adstream_nx.ads/intm/webdev/www.codeguru.com/csharp/.net/net_general/comcom@120x60-1,125x125-1,125x600,125x800,468x60-1,468x60-2,accessunit,accessunit_one,accessunit_three,accessunit_two,ciu,cp1,cp10,cp11,cp12,cp13,cp14,cp2,cp3,cp4,cp5,cp6,cp7,cp8,cp9,fl1,fl2,fl3,fl4,fl5,flex,house_ribbon,marketplace01,marketplace02,marketplace03,marketplace04,marketplace05,marketplace06,sitetext-1!flex" border=0> </a> <A HREF="http://63.236.18.118/RealMedia/ads/click_nx.ads/intm/webdev/www.codeguru.com/csharp/.net/net_general/comcom@120x60-1,125x125-1,125x600,125x800,468x60-1,468x60-2,accessunit,accessunit_one,accessunit_three,accessunit_two,ciu,cp1,cp10,cp11,cp12,cp13,cp14,cp2,cp3,cp4,cp5,cp6,cp7,cp8,cp9,fl1,fl2,fl3,fl4,fl5,flex,house_ribbon,marketplace01,marketplace02,marketplace03,marketplace04,marketplace05,marketplace06,sitetext-1!accessunit" > <IMG SRC="http://63.236.18.118/RealMedia/ads/adstream_nx.ads/intm/webdev/www.codeguru.com/csharp/.net/net_general/comcom@120x60-1,125x125-1,125x600,125x800,468x60-1,468x60-2,accessunit,accessunit_one,accessunit_three,accessunit_two,ciu,cp1,cp10,cp11,cp12,cp13,cp14,cp2,cp3,cp4,cp5,cp6,cp7,cp8,cp9,fl1,fl2,fl3,fl4,fl5,flex,house_ribbon,marketplace01,marketplace02,marketplace03,marketplace04,marketplace05,marketplace06,sitetext-1!accessunit" border=0> </a> Environment: Visual Studio(2005) Csharp
1. Create a Usercontrol using Visual Studio (C#):
(
Full Size Image )
2. Configure the project properties:
(Full Size Image )
3. Modify the usercontrol interface:
Note : Make sur you added the EVENTS class: [ ClassInterface ( ClassInterfaceType .AutoDual), ComSourceInterfaces ( typeof (UserControlEvents))] using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; // Add references using System.Runtime.InteropServices; using System.Reflection; using Microsoft.Win32; namespace CsharpWindowsActiveX {
[ ProgId ( "CsharpWindowsActiveX.ActiveXUserControl" )] [ ClassInterface ( ClassInterfaceType .AutoDual), ComSourceInterfaces ( typeof (UserControlEvents))]
public partial class ActiveXUserControl : UserControl {
public ActiveXUserControl() { InitializeComponent(); }
.....
4. Add the register/unregister section in the source code
// register COM ActiveX object [ ComRegisterFunction ()] public static void RegisterClass( string key) { StringBuilder skey = new StringBuilder (key); skey.Replace( @"HKEY_CLASSES_ROOT\" , "" ); RegistryKey regKey = Registry .ClassesRoot.OpenSubKey(skey.ToString(), true ); RegistryKey ctrl = regKey.CreateSubKey( "Control" ); ctrl.Close(); RegistryKey inprocServer32 = regKey.OpenSubKey( "InprocServer32" , true ); inprocServer32.SetValue( "CodeBase" , Assembly .GetExecutingAssembly().CodeBase); inprocServer32.Close(); regKey.Close(); }
[ ComUnregisterFunction ()] public static void UnregisterClass( string key) { StringBuilder skey = new StringBuilder (key); skey.Replace( @"HKEY_CLASSES_ROOT\" , "" ); RegistryKey regKey = Registry .ClassesRoot.OpenSubKey(skey.ToString(), true ); regKey.DeleteSubKey( "Control" , false ); RegistryKey inprocServer32 = regKey.OpenSubKey( "InprocServer32" , true ); regKey.DeleteSubKey( "CodeBase" , false ); regKey.Close(); }
5. Add an ActiveX property:
// ActiveX properties (Get/Set) ////////////////////////////////////////////////////////////////// private int ptextVal; public int TextVal { get { ptextVal = ( int )(numericUpDown1.Value); return ptextVal; } se t {
ptextVal = value ; numericUpDown1.Value = ptextVal; } }
6. Add an ActiveX method:
// ActiveX methods/functions ////////////////////////////////////////////////////////////////// public interface ICOMCallable
{ int GetTextBoxValue(); } public int GetTextBoxValue() { int i = ( int )(numericUpDown1.Value); MessageBox .Show( "ActiveX method: GetTextBoxValue " + i.ToString()); return (i); }
7. Add an ActiveX event:
// Eventhandler interface ////////////////////////////////////////////////////////////////// public delegate void ControlEventHandler (int NumVal); [Guid ("0A415E38-372F-45fb-813B-D9558C787EA0" )] [InterfaceType (ComInterfaceType .InterfaceIsIDispatch)]
public interface UserControlEvents { [DispId (0x60020001)] void OnButtonClick(int NumVal); }
public event ControlEventHandler OnButtonClick;
private void buttonOK_Click(object sender, EventArgs e) {
int NumVal; if (OnButtonClick != null ) { NumVal = (int )(numericUpDown1.Value); OnButtonClick(NumVal); } }
8. Register the ActiveX on your PC:
Register the new ActiveX on your computer using the command: RegAsm.exe CsharpWindowsActiveX.dll
9. Test your ActiveX:
Use the TSTCon32.exe tool from Visul Studio to test the ActiveX:
(Full Size Image )
(Full Size Image )
Downloads
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?