公用ActiveX控件
阿凡提 2017-04-15 07:48:14 准备写一个公用ActiveX控件(如Button,CheckBox,TextBox等)供其它语言使用,
有些问题向各位请教:
1,各版本windows都有comctl32.dll,其中包含5.x的公用控件;
comctl32.ocx包含6.0的公用控件,但windows 7以后没有这个文件,是否windows 7以后都没有6.0的公用控件?
2,System.Windows.Forms.dll中的公用控件是dotNET的公用控件,
如果使用System.Windows.Forms.dll中的公用控件,是否必须安装dotNET运行库?
3,C#语言容易编写公用控件的wrapper代码,例如:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;
namespace DotNetControl
{
[ProgId("DotNetControl.MyUserControl")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("FBD9A300-A0D2-4E68-8A6C-EB3B378F5E2D")]
[ComVisible(true)]
public partial class MyUserControl : System.Windows.Forms.UserControl
{
public MyUserControl()
{
InitializeComponent();
}
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");
// Open the CLSID\{guid} key for write access
RegistryKey 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 key
k.Close();
}
[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");
// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
// Delete the 'Control' key, but don't throw an exception if it does not exist
k.DeleteSubKey("Control", false);
// Next open up InprocServer32
RegistryKey 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();
}
}
}
经测试可以正常使用。
C/C++语言能否出类似的wrapper代码?或者参考示例?
4,网上有一个VB的CommonControls (Replacement of the MS common controls)项目,
能否参考该项目用VC重写?
大家有什么建议、看法,欢迎指教。。。