Accueil > > > CRÉATION D'UN ACTIVEX EN UTILSANT UN USERCONTROL DOTNET CSHARP
CRÉATION D'UN ACTIVEX EN UTILSANT UN USERCONTROL DOTNET CSHARP
Information sur la source
Description
Cet article démontre la création d'un ActiveX utilsant un UserControl DotNet Csharp avec Visual Studio (2005). L'exemple contient l'utilisation des fonctionnalités ActiveX: Propriétés, méthodes et événements.
Source
- 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.IO;
- 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();
- }
-
- // 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("CsharpWindowsActiveX.ActiveXUserControl");
- Console.WriteLine("ProgID=CsharpWindowsActiveX.ActiveXUserControl 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();
- }
-
-
- // ActiveX properties (Get/Set) //////////////////////////////////////////////////////////////////
- private int ptextVal;
-
- public int TextVal
- {
- get
- {
- ptextVal = (int)(numericUpDown1.Value);
- return ptextVal;
- }
- set
- {
- ptextVal = value;
- numericUpDown1.Value = ptextVal;
- }
- }
-
-
-
-
- // 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);
- }
-
-
- // 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);
- }
- }
-
-
- // //////////////////////////////////////////////////////////////////
-
-
-
-
- }
- }
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.IO;
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();
}
// 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("CsharpWindowsActiveX.ActiveXUserControl");
Console.WriteLine("ProgID=CsharpWindowsActiveX.ActiveXUserControl 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();
}
// ActiveX properties (Get/Set) //////////////////////////////////////////////////////////////////
private int ptextVal;
public int TextVal
{
get
{
ptextVal = (int)(numericUpDown1.Value);
return ptextVal;
}
set
{
ptextVal = value;
numericUpDown1.Value = ptextVal;
}
}
// 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);
}
// 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);
}
}
// //////////////////////////////////////////////////////////////////
}
}
Historique
- 16 juillet 2009 17:57:42 :
- ajout création Active sur une page html (récupérer le GUID depuis le ProgID)
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Probleme évenement click sur un ActiveX dans un UserControl [ par fcolo ]
Bonjour,J'utilise l'ActiveX de VLC (logiciel de capture video) pour capturer le flux vidéo d'une caméra IP.Dans le logiciel que je réalise, je permet
Transformer un UserControl en ActiveX [ par maxnoe ]
Bonjour, Voila, j'ai prog un UserControl sous C#. Il fonctionne bien dans VisualStudio. Maitenant je voudrais le mettre sur mon site web. Donc je vo
Propriétés custom d'un activeX en C# [ par esopio ]
Bonjour, Aujourd'hui je suis capable de coder un objet activeX en C# par l'intermédiaire d'un userControl C#. Il suffit pour cela d'ajouter quelques l
ActiveX control [ par jojotn ]
bonjour, lorsque je met l'animation flash dans mon form....j'ai le message suivant: ActiveX control please ensure it is properly registered...comment
utilisation du presse papier dans une page web en csharp [ par phoenix7517 ]
Bonjourje débute en csharp, et je dois rajouter des petites choses à des pages web développée en csharp (en tout cas, il me semble que c'est dans ce l
[UserControl] Propriete - Enum Dynamique [ par TheBlackReverand ]
Bonjour,je developper actuellement un UserControl dans lequel je veut, en mode Designer(via la PropertyGrid) utiliser une Property pour selectionner u
Passer des valeurs d'une page aspx vers un usercontrol [ par boby32 ]
Bonjour, voilà j'ai un usercontrol qui contient des boutons qui sont visibles ou non en fonction de la valeur d'une variable.Dans ma page, j'affecte u
UserControl [ par xmox667 ]
Salut à tous,J'ai un UserControl qui contient une PictureBox et un Label.Lorsque je saisis la taille (88;71) du UserControl dans le designer de Visual
UserControl non visible [ par xmox667 ]
Salut, J'aimerais faire un UserControl qui n'est pas d'interface graphique comme le Contrôle Timer du framework .net Merci
Pb d'affichage de UserControl identique [ par ChrisBzh56 ]
|
Derniers Blogs
MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril SHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KITSHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KIT par Matthew
http://www.microsoft.com/download/en/details.aspx?id=28768&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MicrosoftDownloadCenter+(Microsoft+Download+Center) ...
Cliquez pour lire la suite de l'article par Matthew
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|