using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// Add references
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
namespace axGrid02
{
[ProgId("axGrid02.UserControl1")]
[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]
//[ComClass(UserControl1.ClassId,
UserControl1.InterfaceId, UserControl1.EventsId)]
public partial class UserControl1: UserControl
{
//--<
IDs GUID >--
//public
const String ClassId = "147DF0FB-83F3-4BCB-9D55-F36E29C221FE";
//public
const String InterfaceId = "E43550AD-D09C-48CD-9D6B-B07FDE61B45C";
public const String EventsId = "EC2581AC-1994-4C16-8172-6C360D07C3C0";
//--</
IDs GUID >--
#region Control
public UserControl1()
{
InitializeComponent();
}
#endregion /Control
#region Buttons
//--------------------<
region: Buttons >-----------------
private void btnTest_Click(object sender, EventArgs e)
{
MessageBox.Show("this is ActiveX
axGrid02");
}
private void btnSet_Click(object sender, EventArgs e)
{
}
private void btnGet_Click(object sender, EventArgs e)
{
}
private void buttonOK_Click(object sender, EventArgs e)
{
int NumVal;
if (OnButtonClick != null)
{
//NumVal = (int)(numericUpDown1.Value);
//OnButtonClick(NumVal);
}
}
//--------------------<
region: Buttons >-----------------
#endregion /Buttons
#region Controls
//----------<
Controls >-------------
private void ActiveXUserControl_Load(object sender, EventArgs e)
{
}
//----------</
Controls >-------------
#endregion /Controls
#region Sys: Register As COM ActiveX
//--------------------<
region: Sys:Register COM ActiveX >-----------------
//
register COM ActiveX object
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT\", "");
// récupérer GUID depuis
ProgID="CsharpWindowsActiveX.ActiveXUserControl"
Type myType1 =
Type.GetTypeFromProgID("axGrid02.UserControl1");
Console.WriteLine("ProgID=axGrid02.UserControl1
GUID={0}.",
myType1.GUID);
// ecrire GUID dans un fichier txt
TextWriter tw;
tw = File.CreateText("guid.txt");
tw.WriteLine(skey.ToString());
tw.WriteLine(myType1.GUID.ToString());
tw.Close();
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();
}
//
Unregister COM ActiveX object
[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();
}
//--------------------<
region: /Sys:Register COM ActiveX >-----------------
#endregion /Sys:Register As COM ActiveX
#region Interface Events
//--------------------<
region: Interface Events >-----------------
//*Eventhandler interface
public delegate void ControlEventHandler(int NumVal);
[Guid(EventsId)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface UserControlEvents
{
[DispId(0x60020001)]
void OnButtonClick(int NumVal);
}
//----------<
Events: Buttons >-------------
public event ControlEventHandler OnButtonClick;
public event ControlEventHandler OnBtnSetClick;
public event ControlEventHandler btnGetClick;
//----------</
Events: Buttons >-------------
//--------------------</
region: Interface Events >-----------------
#endregion /events
#region Interface Properties
//--------------------< region:
Properties >-----------------
//*Values
are exchanged between Hosting-App and ActiveX-Control as GET-/ SET- Property
public string Textbox1
{
get
{
// ptextVal =
(int)(numericUpDown1.Value);
return tbxText1.Text ;
}
set
{
tbxText1.Text = value;
//numericUpDown1.Value = ptextVal;
}
}
//--------------------</
region: Properties >-----------------
#endregion /Interface Properties
#region Interface Methods, Functions
//--------------------<
region: Methods, Functions >-----------------
public interface ICOMCallable
{
string axSet_Textbox1_Text();
}
public string axSet_Textbox1_Text()
{
//int i = (int)(numericUpDown1.Value);
//MessageBox.Show("ActiveX method:
GetTextBoxValue " + i.ToString());
//return (i);
tbxText1.Text = "set by extenal
function(..)";
return "set Textbox1";
}
//--------------------</
region: Methods, Functions >-----------------
#endregion /Interface Methods, Functions
}
}
//notes:
//Referenz: http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257/Create-an-ActiveX-using-a-Csharp-Usercontrol.htm
|