begin process at 2010 02 10 07:20:13
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > [.NET3.5] EXTENSION METHODS: PROGRESSBAR ET VISTA

[.NET3.5] EXTENSION METHODS: PROGRESSBAR ET VISTA


 Information sur la source

Note :
Aucune note
Catégorie :.NET Source .NET ( DotNet ) Classé sous :Extension, Methods, ProgressBar, Vista, Barre progression Niveau :Débutant Date de création :21/10/2008 Vu / téléchargé :2 649 / 147

Auteur : Willi

Ecrire un message privé
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note


 Description

Cliquez pour voir la capture en taille normale
Je rappel pour ceux qui ne connaissent pas les "extensions methods" qu'il s'agit d'une nouveauté de c#3. Elles permettent d'étendre les méthodes sur des classes existantes. Ceci peut s'avérer pratique dans le cas ou l'on souhaite ajouter des méthodes sur une classe dont on a pas les sources et/ou on ne peut pas en hériter.

J'ai mis en application cette nouveauté sur le contrôle ProgressBar en y ajoutant des méthodes propres à des fonctionnalités de Vista. (voir la capture)

Le code est commenté bien que la source soit simple.

Source

  • using System;
  • using System.Runtime.InteropServices;
  • namespace ExtensionMethods
  • {
  • public static class Progressbar
  • {
  • #region "Native methods"
  • [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  • private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  • [DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)]
  • private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);
  • [DllImport("user32.dll", SetLastError = true)]
  • private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
  • [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
  • private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
  • [DllImport("user32.dll", SetLastError = true)]
  • private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  • const int WM_USER = 0x400;
  • const int GWL_STYLE = (-16);
  • const int PBM_SETPOS = (WM_USER + 2);
  • const int PBM_SETSTATE = (WM_USER + 16);
  • const int PBM_GETSTATE = (WM_USER + 17);
  • const int PBS_SMOOTH = 0x01;
  • const int PBS_SMOOTHREVERSE = 0x10;
  • #endregion
  • /// <summary>
  • /// Etat de la barre de progression.
  • /// </summary>
  • public enum PBM_STATE : int
  • {
  • NORMAL = 0x1,
  • ERROR,
  • PAUSE
  • }
  • /// <summary>
  • /// Obtient l'état de la barre de progression.
  • /// </summary>
  • /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
  • /// <returns>L'état <see cref="PBM_STATE"/>.</returns>
  • public static PBM_STATE GetState(this System.Windows.Forms.ProgressBar pb)
  • {
  • if (Environment.OSVersion.Version.Major >= 6)
  • {
  • IntPtr pValue = SendMessage(pb.Handle, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
  • int iVal = pValue.ToInt32();
  • return (PBM_STATE)iVal;
  • }
  • else
  • throw new Exception("Supported on Vista or later only !.");
  • }
  • /// <summary>
  • /// Définit l'état de la barre de progression.
  • /// </summary>
  • /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
  • /// <param name="state">Nouvel état <see cref="PBM_STATE"/>.</param>
  • public static void SetState(this System.Windows.Forms.ProgressBar pb, PBM_STATE state)
  • {
  • if (Environment.OSVersion.Version.Major >= 6)
  • {
  • SendMessage(pb.Handle, PBM_SETSTATE, new IntPtr((int)state), IntPtr.Zero);
  • }
  • else
  • throw new Exception("Supported on Vista or later only !.");
  • }
  • /// <summary>
  • /// Change la manière dont la décrémentation de la valeur de la progress bar se fait.
  • /// </summary>
  • /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
  • /// <param name="turnOn"><b>True</b> pour décrémentation fluide; <b>False</b> pour décrémentation direct.</param>
  • public static void SmoothReverse(this System.Windows.Forms.ProgressBar pb, bool turnOn)
  • {
  • if (Environment.OSVersion.Version.Major >= 6)
  • {
  • //Récupère le style.
  • IntPtr pStyle = GetWindowLong(pb.Handle, GWL_STYLE);
  • //Garde la valeur actuelle pour redéfinir celle de la progress bar une fois la modification de style effectué.
  • int iOldVal = pb.Value;
  • if (turnOn)
  • {
  • //Si pas activé?
  • //On active.
  • if ((pStyle.ToInt32() & PBS_SMOOTHREVERSE) != PBS_SMOOTHREVERSE)
  • {
  • SetWindowLong(pb.Handle, GWL_STYLE, new IntPtr((pStyle.ToInt32() | PBS_SMOOTHREVERSE)));
  • SendMessage(pb.Handle, PBM_SETPOS, new IntPtr(iOldVal), IntPtr.Zero);
  • }
  • }
  • else
  • {
  • //Si activé?
  • //On désactive.
  • if ((pStyle.ToInt32() & PBS_SMOOTHREVERSE) == PBS_SMOOTHREVERSE)
  • {
  • SetWindowLong(pb.Handle, GWL_STYLE, new IntPtr((pStyle.ToInt32() - PBS_SMOOTHREVERSE)));
  • SendMessage(pb.Handle, PBM_SETPOS, new IntPtr(iOldVal), IntPtr.Zero);
  • }
  • }
  • }
  • else
  • throw new Exception("Supported on Vista or later only !.");
  • }
  • /// <summary>
  • /// GetWindowLong avec support 32 et 64 bits.
  • /// </summary>
  • private static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
  • {
  • if (IntPtr.Size == 8)
  • return GetWindowLongPtr(hWnd, nIndex);
  • else
  • return GetWindowLong32(hWnd, nIndex);
  • }
  • /// <summary>
  • /// SetWindowLong avec support 32 et 64 bits.
  • /// </summary>
  • private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
  • {
  • if (IntPtr.Size == 8)
  • return SetWindowLongPtr(hWnd, nIndex, dwNewLong);
  • else
  • return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
  • }
  • }
  • }
using System;
using System.Runtime.InteropServices;

namespace ExtensionMethods
{
    public static class Progressbar
    {
        #region "Native methods"

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)]
        private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
        private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        const int WM_USER = 0x400;
        const int GWL_STYLE = (-16);
        const int PBM_SETPOS = (WM_USER + 2);
        const int PBM_SETSTATE = (WM_USER + 16);
        const int PBM_GETSTATE = (WM_USER + 17);
        const int PBS_SMOOTH = 0x01;
        const int PBS_SMOOTHREVERSE = 0x10;

        #endregion

        /// <summary>
        /// Etat de la barre de progression.
        /// </summary>
        public enum PBM_STATE : int
        {
            NORMAL = 0x1,
            ERROR,
            PAUSE
        }

        /// <summary>
        /// Obtient l'état de la barre de progression.
        /// </summary>
        /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
        /// <returns>L'état <see cref="PBM_STATE"/>.</returns>
        public static PBM_STATE GetState(this System.Windows.Forms.ProgressBar pb)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                IntPtr pValue = SendMessage(pb.Handle, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);

                int iVal = pValue.ToInt32();
                return (PBM_STATE)iVal;
            }
            else
                throw new Exception("Supported on Vista or later only !.");
        }

        /// <summary>
        /// Définit l'état de la barre de progression.
        /// </summary>
        /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
        /// <param name="state">Nouvel état <see cref="PBM_STATE"/>.</param>
        public static void SetState(this System.Windows.Forms.ProgressBar pb, PBM_STATE state)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                SendMessage(pb.Handle, PBM_SETSTATE, new IntPtr((int)state), IntPtr.Zero);
            }
            else
                throw new Exception("Supported on Vista or later only !.");
        }

        /// <summary>
        /// Change la manière dont la décrémentation de la valeur de la progress bar se fait.
        /// </summary>
        /// <param name="pb"><see cref="System.Windows.Forms.ProgressBar"/></param>
        /// <param name="turnOn"><b>True</b> pour décrémentation fluide; <b>False</b> pour décrémentation direct.</param>
        public static void SmoothReverse(this System.Windows.Forms.ProgressBar pb, bool turnOn)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                //Récupère le style.
                IntPtr pStyle = GetWindowLong(pb.Handle, GWL_STYLE);

                //Garde la valeur actuelle pour redéfinir celle de la progress bar une fois la modification de style effectué.
                int iOldVal = pb.Value;

                if (turnOn)
                {
                    //Si pas activé?
                    //On active.
                    if ((pStyle.ToInt32() & PBS_SMOOTHREVERSE) != PBS_SMOOTHREVERSE)
                    {
                        SetWindowLong(pb.Handle, GWL_STYLE, new IntPtr((pStyle.ToInt32() | PBS_SMOOTHREVERSE)));
                        SendMessage(pb.Handle, PBM_SETPOS, new IntPtr(iOldVal), IntPtr.Zero);
                    }
                }
                else
                {
                    //Si activé?
                    //On désactive.
                    if ((pStyle.ToInt32() & PBS_SMOOTHREVERSE) == PBS_SMOOTHREVERSE)
                    {
                        SetWindowLong(pb.Handle, GWL_STYLE, new IntPtr((pStyle.ToInt32() - PBS_SMOOTHREVERSE)));
                        SendMessage(pb.Handle, PBM_SETPOS, new IntPtr(iOldVal), IntPtr.Zero);
                    }
                }
            }
            else
                throw new Exception("Supported on Vista or later only !.");
        }

        /// <summary>
        /// GetWindowLong avec support 32 et 64 bits.
        /// </summary>
        private static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
        {
            if (IntPtr.Size == 8)
                return GetWindowLongPtr(hWnd, nIndex);
            else
                return GetWindowLong32(hWnd, nIndex);
        }

        /// <summary>
        /// SetWindowLong avec support 32 et 64 bits.
        /// </summary>
        private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            if (IntPtr.Size == 8)
                return SetWindowLongPtr(hWnd, nIndex, dwNewLong);
            else
                return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
        }
    }
}

 Conclusion

Bon dév à tous ^^

 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


 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) EXPLORER LA MFT D'UNE PARTITION NTFS
Source avec Zip Source avec une capture Source .NET (Dotnet) IPHELPER - PORTS TCP/UDP, TABLES DE ROUTAGE/ARP + FONCTIONS ...
Source avec Zip Source avec une capture Source .NET (Dotnet) PREVIEW HANDLER POUR OFFICE OUTLOOK 2007/10 SUR LES FICHIERS...
Source avec Zip Source avec une capture Source .NET (Dotnet) TEAM FOUNDATION SERVER - EXPLOITER LA PARTIE CLIENTE.
Source avec Zip Source avec une capture Source .NET (Dotnet) [.NET3.5] SYSTEM.IO.PIPES - UTILISATION D'UN CANAL NOMMÉ

 Sources de la même categorie

Source avec Zip CHAT SERVER-CLIENT par abderrahmenbilog
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMULATION DE CONSOLE POUR WINDOWS MOBILE par originalcompo
Source avec Zip Source .NET (Dotnet) BASE DE DONNÉES EN XML par DanMor498
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMPLECONV - APPLICATION DE CONVERSION MONÉTAIRE AVEC TAUX E... par Jeffrey_
Source avec Zip Source .NET (Dotnet) TRAITEUR D'IMAGE (MINI) par ycyril

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture EXTINCTION DES FEUX (PROGRAMMATEUR D'ARRET, REDEMARRAGE OU M... par kkuet12
Source avec Zip Source .NET (Dotnet) PROPRIÉTÉS D'EXTENSION AVEC C# 3.0 par mathmax
Source avec Zip Source avec une capture Source .NET (Dotnet) [VSTO 2005 SE] EXTENSION DU RIBBON / RUBAN par azra
Source avec Zip Source avec une capture Source .NET (Dotnet) LABEL WINFORMS AVEC EFFET GLASS (COMME VISTA) par SaumonAgile
Source avec Zip Source avec une capture Source .NET (Dotnet) THREAD ET PROGRESSBAR - EXEMPLE SIMPLE par MorpionMx

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

changer l'extension d'un fichier dans vista [ par volcom20 ] Bonjour, j'aimerais voir les extension de mais fichiers sous windows vista car je veux renommer un fichier .txt pour un .zif ou .css ou .html merci! exception non catchée : VS2005 ne break pas [ par leprov ] Bonjour,Je viens de passer sous vista 64, et depuis, visual studio 2005 ne break plus automatiquement sur les exceptions non catchées. Une simple entr Fichier, changement du nom du fichier [ par vagg ] Bonjour,Dans mon programme, l'utilisateur peut aller chercher un fichier texte grâce à un OpenFileDialog qui ensuite se charge en objet.J'aimerais que Zone rétractable façon Vista [ par Dodo299 ] Bonjour, Je développe une petite application en C# et cela fait maintenant un petit moment que je cherche, en vain, un moyen d'ajouter à mon applicati Vista x64 & AudioVideoPlayback = BadImageFormatException [ par fifrelin70 ] Bonjour à tous,je suis sous windows vista ultimate 64 bits et je souhaite réaliser un petit contrôle.net réveil avec lecture de fichiers mp3.J'ai donc Synthèse/Reconnaissance Vocale sous XP/Vista [ par chiqitoss ] Voila mon problème: Je dois faire une appli qui permet de translater le code en morse en français écrit, et translater le français en morse.Premièreme Probleme de navigateur avec asp.net [ par ddove53 ] Bonjour,j'ai fait des pages web avec asp.net et quand je lance une page avec le navigateur sur xp pro, ca marche bien. Mais avec vista, j'ai le messag Installation VS2008 sur windows Vista [ par YbenAli ] bonjour Est ce visual studio tourne correctement sur Windows Vista. merci... détecter click "long" bouton [ par bigger ] Bonjour à tous,J'ai deux boutons de chaque côté d'une progressbar continue, l'un pour incrémenter et l'autre pour décrémenter cette derniere (bouton + Deplacer des fichiers en fonction des extensions [ par damsdu64 ] Bonjour, Je voudrais déplacer des fichiers d'un endroit à un autre. Cependant je ne connais pas par avance le nom de ces fichiers ni leur nombre, la s


Nos sponsors


Sondage...

Comparez les prix


HTC Magic

Entre 429€ et 429€

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
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 : 0,811 sec (3)

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