begin process at 2012 02 04 07:32:14
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > 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

Note :
Aucune note
Catégorie :.NET Source .NET ( DotNet ) Classé sous :Csharp, ActiveX, Com interop, UserControl Niveau :Débutant Date de création :12/07/2009 Date de mise à jour :16/07/2009 17:57:22 Vu / téléchargé :4 141 / 339

Auteur : AVerhamme

Ecrire un message privé
Site perso
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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);
            }
        }

                       
        // //////////////////////////////////////////////////////////////////



 
    }
}


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 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

Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONBANQUE par toutphp
Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONAPPLICATION par toutphp
Source avec Zip SOCKET CONNEXION CLIENT & SERVEUR par ziedto83
Source avec Zip Source .NET (Dotnet) FFMPEG.NET : WRAPPER .NET DE FFMPEG par MasterShadows
Source avec Zip Source .NET (Dotnet) ATTACHER, CRÉER ET SAUVEGARDER UNE BASE DE DONNÉES SQL SERVE... par Alvepinai

 Sources en rapport avec celle ci

Source avec Zip Source .NET (Dotnet) LISTER LES FICHIERS ET DOSSIER D'UN DOSSIER D'UN CLIC DROIT par D4rkTiger
Source avec Zip Source .NET (Dotnet) STRING AND DATE HELPERS par D4rkTiger
Source avec Zip Source .NET (Dotnet) [XNA] CULTURE ASTEROIDS par Chiheb2010
Source avec Zip Source avec une capture Source .NET (Dotnet) ZIP-UNZIP AVEC SHARPZIPLIB par buno
Source avec Zip Source avec une capture Source .NET (Dotnet) UN JEU DE SERPENT par badrsmimite

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


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 ]


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 3,229 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales