begin process at 2010 02 10 06:14:14
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > UNE CLASSE POUR LES WALLPAPERS

UNE CLASSE POUR LES WALLPAPERS


 Information sur la source

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Divers Source .NET ( DotNet ) Classé sous :wallpaper, fond, ecran Niveau :Initié Date de création :10/01/2005 Date de mise à jour :10/01/2005 15:27:44 Vu / téléchargé :16 697 / 243

Auteur : gulix

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

 Description

Voici une classe qui vous permettra de gérer des affichages de fond d'écran facilement.

Source

  • // Wallpaper.cs - Une classe pour afficher un fond d'écran sous Windows
  • // Conçue et réalisée par Gulix
  • // Contact : gulix33xp@yahoo.fr - http://gulix.free.fr
  • // Pour plus d'informations, lire le fichier d'informations fourni avec la classe
  • using System;
  • using System.Drawing;
  • using System.Collections;
  • using System.ComponentModel;
  • using System.Windows.Forms;
  • using System.Runtime.InteropServices;
  • using System.IO;
  • using System.Data;
  • using Microsoft.Win32;
  • public class Wallpaper
  • {
  • // Variables et Fonctions nécessaires à l'affichage du Wallpaper
  • const int SPI_SETDESKWALLPAPER = 20;
  • const int SPIF_UPDATEINIFILE = 0x01;
  • const int SPIF_SENDWININICHANGE = 0x02;
  • [DllImport("user32.dll", CharSet = CharSet.Auto)]
  • static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
  • [DllImport("user32.dll")]
  • public static extern void SetSysColors(int elementCount, int[] colorNames, int[] colorValues);
  • // Enumération permettant de fixer le style d'affichage
  • public enum Affichage
  • {
  • centrer,
  • mosaique,
  • etirer,
  • ajuster
  • }
  • // On déclare les membres
  • private string nomfichier;
  • private Affichage affichage; // Style d'affichage
  • private System.Drawing.Color couleurFond; // Couleur de fond du bureau
  • // Constructeur de la classe Wallpaper
  • public Wallpaper(string fichier, Affichage affich, Color fond)
  • {
  • this.nomfichier = fichier;
  • this.affichage = affich;
  • this.couleurFond = fond;
  • }
  • // GetNomCourt
  • // permet de retrouver le nom du fichier, sans son chemin complet
  • public string GetNomCourt()
  • {
  • string[] split;
  • char[] separateur ={ '\\' };
  • int max;
  • split = this.nomfichier.Split(separateur);
  • max = split.GetUpperBound(0);
  • return split[max];
  • }
  • // GetRepertoire
  • // permet de retrouver le repertoire contenant l'image
  • public string GetRepertoire()
  • {
  • string[] split;
  • string[] join;
  • char[] separateur ={ '\\' };
  • int max;
  • split = this.nomfichier.Split(separateur);
  • max = split.GetUpperBound(0);
  • join = new string[max];
  • for (int i = 0; i < (max); i++)
  • {
  • join[i] = split[i];
  • }
  • return String.Join("\\", join) + "\\";
  • }
  • // GetImageHauteur
  • // permet d'obtenir la hauteur du fichier image, en pixels
  • public int GetImageHauteur()
  • {
  • int retour = 0;
  • try
  • {
  • Image image = new Bitmap(this.nomfichier);
  • retour = image.Height;
  • }
  • catch
  • {
  • retour = -1;
  • }
  • return retour;
  • }
  • // GetImageLargeur
  • // permet d'obtenir la largeur du fichier image, en pixels
  • public int GetImageLargeur()
  • {
  • int retour = 0;
  • try
  • {
  • Image image = new Bitmap(this.nomfichier);
  • retour = image.Width;
  • }
  • catch
  • {
  • retour = -1;
  • }
  • return retour;
  • }
  • // Afficher
  • // Affiche le fond d'écran suivant les paramètres fournis
  • public void Afficher()
  • {
  • try
  • {
  • // On recopie l'image dans les fichiers temporaires au format bitmap
  • System.Drawing.Image image;
  • if (this.affichage == Affichage.ajuster)
  • {
  • image = System.Drawing.Image.FromFile(this.Ajustement());
  • }
  • else
  • {
  • image = System.Drawing.Image.FromFile(this.GetRepertoire() + this.GetNomCourt());
  • }
  • string fichierTemporaire = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
  • image.Save(fichierTemporaire, System.Drawing.Imaging.ImageFormat.Bmp);
  • // On modifie le style d'affichage dans la base de registre
  • RegistryKey cle = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
  • if (this.affichage == Affichage.etirer)
  • {
  • cle.SetValue(@"WallpaperStyle", 2.ToString());
  • cle.SetValue(@"TileWallpaper", 0.ToString());
  • }
  • if (this.affichage == Affichage.centrer)
  • {
  • cle.SetValue(@"WallpaperStyle", 1.ToString());
  • cle.SetValue(@"TileWallpaper", 0.ToString());
  • }
  • if (this.affichage == Affichage.mosaique)
  • {
  • cle.SetValue(@"WallpaperStyle", 1.ToString());
  • cle.SetValue(@"TileWallpaper", 1.ToString());
  • }
  • if (this.affichage == Affichage.ajuster)
  • {
  • cle.SetValue(@"WallpaperStyle", 1.ToString());
  • cle.SetValue(@"TileWallpaper", 0.ToString());
  • }
  • // On utilise les fonctions de la DLL user32 pour afficher le wallpaper
  • SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fichierTemporaire, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
  • int[] elementArray = { 1 };
  • int[] elementValues = { ColorTranslator.ToWin32(this.GetCouleurFond()) };
  • // et modifier la couleur de fond du bureau
  • SetSysColors(1, elementArray, elementValues);
  • }
  • catch
  • {
  • MessageBox.Show("Une erreur s'est produite lors de l'affichage du Wallpaper");
  • }
  • }
  • // GetEcranLargeur
  • // permet d'obtenir la largeur de l'écran
  • public static int GetEcranLargeur()
  • {
  • return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
  • }
  • // GetEcranHauteur
  • // permet d'obtenir la hauteur de l'écran
  • public static int GetEcranHauteur()
  • {
  • return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
  • }
  • // Ajustement
  • // Réalise l'ajustement de l'image si nécessaire
  • // retourne le chemin vers le fichier ajusté
  • private string Ajustement()
  • {
  • string fichierRetour;
  • if ((this.GetImageHauteur() <= GetEcranHauteur()) && (this.GetImageLargeur() <= GetEcranLargeur()))
  • {
  • // Pas d'ajustement nécessaire, on retourne le nom du fichier original
  • fichierRetour = this.nomfichier;
  • }
  • else
  • {
  • // On calcule les nouvelles dimensions
  • double ratio = ((double)GetEcranHauteur()) / ((double)this.GetImageHauteur());
  • if (ratio > (((double)GetEcranLargeur()) / ((double)this.GetImageLargeur())))
  • {
  • ratio = ((double)GetEcranLargeur()) / ((double)this.GetImageLargeur());
  • }
  • int nouvelleLargeur = (int)(((double)this.GetImageLargeur()) * ratio);
  • int nouvelleHauteur = (int)(((double)this.GetImageHauteur()) * ratio);
  • // On crée le support de la nouvelle image
  • System.Drawing.Size tailleAjuster = new Size(nouvelleLargeur, nouvelleHauteur);
  • System.Drawing.Image imageAjuster = null;
  • // On crée la nouvelle image à partir de l'original, et de la nouvelle taille
  • imageAjuster = new Bitmap(Image.FromFile(this.nomfichier), tailleAjuster);
  • imageAjuster.Save(Path.Combine(Path.GetTempPath(), "ajuster.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);
  • imageAjuster.Dispose();
  • fichierRetour = Path.Combine(Path.GetTempPath(), "ajuster.bmp");
  • }
  • return fichierRetour;
  • }
  • // Méthodes d'accès aux paramètres
  • // permet de limiter l'accès aux paramètres
  • public Affichage GetAffichage()
  • {
  • return this.affichage;
  • }
  • public void SetAffichage(Affichage aff)
  • {
  • this.affichage = aff;
  • }
  • public Color GetCouleurFond()
  • {
  • return this.couleurFond;
  • }
  • public void SetCouleurFond(Color col)
  • {
  • this.couleurFond = col;
  • }
  • public override string ToString()
  • {
  • return this.GetNomCourt();
  • }
  • }
// Wallpaper.cs - Une classe pour afficher un fond d'écran sous Windows
//                Conçue et réalisée par Gulix 
// Contact : gulix33xp@yahoo.fr - http://gulix.free.fr
// Pour plus d'informations, lire le fichier d'informations fourni avec la classe


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Data;
using Microsoft.Win32;

public class Wallpaper
    {
        // Variables et Fonctions nécessaires à l'affichage du Wallpaper
        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
        [DllImport("user32.dll")]
        public static extern void SetSysColors(int elementCount, int[] colorNames, int[] colorValues);

        // Enumération permettant de fixer le style d'affichage
        public enum Affichage
        {
            centrer,
            mosaique,
            etirer,
            ajuster
        }

        // On déclare les membres
        private string nomfichier;
        private Affichage affichage; // Style d'affichage
        private System.Drawing.Color couleurFond; // Couleur de fond du bureau

        // Constructeur de la classe Wallpaper
        public Wallpaper(string fichier, Affichage affich, Color fond)
        {
            this.nomfichier = fichier;
            this.affichage = affich;
            this.couleurFond = fond;
        }

        // GetNomCourt
        // permet de retrouver le nom du fichier, sans son chemin complet
        public string GetNomCourt()
        {
            string[] split;
            char[] separateur ={ '\\' };
            int max;
            split = this.nomfichier.Split(separateur);
            max = split.GetUpperBound(0);
            return split[max];
        }

        // GetRepertoire
        // permet de retrouver le repertoire contenant l'image
        public string GetRepertoire()
        {
            string[] split;
            string[] join;
            char[] separateur ={ '\\' };
            int max;
            split = this.nomfichier.Split(separateur);
            max = split.GetUpperBound(0);
            join = new string[max];
            for (int i = 0; i < (max); i++)
            {
                join[i] = split[i];
            }
            return String.Join("\\", join) + "\\";
        }


        // GetImageHauteur
        // permet d'obtenir la hauteur du fichier image, en pixels
        public int GetImageHauteur()
        {
            int retour = 0;
            try
            {
                Image image = new Bitmap(this.nomfichier);
                retour = image.Height;
            }
            catch
            {
                retour = -1;
            }
            return retour;
        }

        // GetImageLargeur
        // permet d'obtenir la largeur du fichier image, en pixels
        public int GetImageLargeur()
        {
            int retour = 0;
            try
            {
                Image image = new Bitmap(this.nomfichier);
                retour = image.Width;
            }
            catch
            {
                retour = -1;
            }
            return retour;
        }

        // Afficher
        // Affiche le fond d'écran suivant les paramètres fournis
        public void Afficher()
        {
            try
            {
                // On recopie l'image dans les fichiers temporaires au format bitmap
                System.Drawing.Image image;
                if (this.affichage == Affichage.ajuster)
                {
                    image = System.Drawing.Image.FromFile(this.Ajustement());
                }
                else
                {
                    image = System.Drawing.Image.FromFile(this.GetRepertoire() + this.GetNomCourt());
                }
                string fichierTemporaire = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
                image.Save(fichierTemporaire, System.Drawing.Imaging.ImageFormat.Bmp);

                // On modifie le style d'affichage dans la base de registre
                RegistryKey cle = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
                if (this.affichage == Affichage.etirer)
                {
                    cle.SetValue(@"WallpaperStyle", 2.ToString());
                    cle.SetValue(@"TileWallpaper", 0.ToString());
                }

                if (this.affichage == Affichage.centrer)
                {
                    cle.SetValue(@"WallpaperStyle", 1.ToString());
                    cle.SetValue(@"TileWallpaper", 0.ToString());
                }

                if (this.affichage == Affichage.mosaique)
                {
                    cle.SetValue(@"WallpaperStyle", 1.ToString());
                    cle.SetValue(@"TileWallpaper", 1.ToString());
                }

                if (this.affichage == Affichage.ajuster)
                {
                    cle.SetValue(@"WallpaperStyle", 1.ToString());
                    cle.SetValue(@"TileWallpaper", 0.ToString());
                }

                // On utilise les fonctions de la DLL user32 pour afficher le wallpaper
                SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fichierTemporaire, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
                int[] elementArray = { 1 };
                int[] elementValues = { ColorTranslator.ToWin32(this.GetCouleurFond()) };
                // et modifier la couleur de fond du bureau
                SetSysColors(1, elementArray, elementValues);
            }
            catch
            {
                MessageBox.Show("Une erreur s'est produite lors de l'affichage du Wallpaper");
            }
        }

        // GetEcranLargeur
        // permet d'obtenir la largeur de l'écran
        public static int GetEcranLargeur()
        {
            return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
        }

        // GetEcranHauteur
        // permet d'obtenir la hauteur de l'écran
        public static int GetEcranHauteur()
        {
            return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
        }

        // Ajustement
        // Réalise l'ajustement de l'image si nécessaire
        // retourne le chemin vers le fichier ajusté
        private string Ajustement()
        {
            string fichierRetour;
            if ((this.GetImageHauteur() <= GetEcranHauteur()) && (this.GetImageLargeur() <= GetEcranLargeur()))
            {
                // Pas d'ajustement nécessaire, on retourne le nom du fichier original
                fichierRetour = this.nomfichier;
            }
            else
            {
                // On calcule les nouvelles dimensions
                double ratio = ((double)GetEcranHauteur()) / ((double)this.GetImageHauteur());
                if (ratio > (((double)GetEcranLargeur()) / ((double)this.GetImageLargeur())))
                {
                    ratio = ((double)GetEcranLargeur()) / ((double)this.GetImageLargeur());
                }
                int nouvelleLargeur = (int)(((double)this.GetImageLargeur()) * ratio);
                int nouvelleHauteur = (int)(((double)this.GetImageHauteur()) * ratio);

                // On crée le support de la nouvelle image
                System.Drawing.Size tailleAjuster = new Size(nouvelleLargeur, nouvelleHauteur);
                System.Drawing.Image imageAjuster = null;

                // On crée la nouvelle image à partir de l'original, et de la nouvelle taille
                imageAjuster = new Bitmap(Image.FromFile(this.nomfichier), tailleAjuster);
                imageAjuster.Save(Path.Combine(Path.GetTempPath(), "ajuster.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);
                imageAjuster.Dispose();
                fichierRetour = Path.Combine(Path.GetTempPath(), "ajuster.bmp");
            }
            return fichierRetour;
        }

        // Méthodes d'accès aux paramètres
        // permet de limiter l'accès aux paramètres
        public Affichage GetAffichage()
        {
            return this.affichage;
        }
        public void SetAffichage(Affichage aff)
        {
            this.affichage = aff;
        }
        public Color GetCouleurFond()
        {
            return this.couleurFond;
        }
        public void SetCouleurFond(Color col)
        {
            this.couleurFond = col;
        }

        public override string ToString()
        {
            return this.GetNomCourt();
        }

    }

 Conclusion

Dans le zip, vous trouverez, en plus de la classe, un fichier txt qui explique rapidement comment utiliser la classe.
Dans le courant de la semaine, je devrais vous présenter une application assez complète utilisant cette classe.

 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

10 janvier 2005 15:27:45 :

 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) WALLÉATOIRE, GESTIONNAIRE DE FONDS D'ÉCRAN

 Sources de la même categorie

Source avec une capture TOOLTIP TEXT POUR LA LISTE DÉROULANTE D'UN COMBOBOX par whismeril
Source avec Zip Source avec une capture Source .NET (Dotnet) LOGIN (XML) par DanMor498
Source .NET (Dotnet) WEBTESTPLUGIN - IGNORER DES URLS LORS D'UN TEST WEB VISUAL S... par jesusonline
Source avec Zip Source avec une capture Source .NET (Dotnet) EXERCICE DE CALCUL MENTAL par Sat7121
Source avec Zip Source avec une capture Source .NET (Dotnet) TRADUCTEUR ALGO VISUAL BASIC .NET/C# par rhonin33

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) DIRECTX OVERLAY - CHANGE WALLPAPER ON DESKTOP WITH DIRECTX par youpiyoyo
Source avec Zip Source avec une capture Source .NET (Dotnet) YTREWQ - POUR MODIFIER RÉGULIÈREMENT VOTRE FOND D'ÉCRAN par ANTHIBUG
Source .NET (Dotnet) CHANGER LA RESOLUTION DE VOTRE ECRAN, UTILISATION DES API WI... par mechtaly
Source avec Zip Source avec une capture Source .NET (Dotnet) WALLÉATOIRE, GESTIONNAIRE DE FONDS D'ÉCRAN par gulix
Source avec Zip Source .NET (Dotnet) CHANGER DE FOND D'ÉCRAN par Caryl

Commentaires et avis

Commentaire de winny68 le 19/06/2008 10:39:32

C'est une très bonne idée d'avoir fait une class pour cela. Car le site ne manque pas d'application à ce sujet, mais il n'y a pas plus simple qu'une class.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

"fond d'ecran" [ par cudenetf ] bonjour,j'aimerais si possible avoir un logo en fond  d'ecran quelque soit le formulaire (ou control qui passe dessus)en fait j'aimerais que ce soit t [Classes] Attributs [ par stailer ] Bonjour tout le monde, dans une classe on peut définir des attributs très simplement comme ceci : [Description("Fond d'écran")] private Image fo Capture d'Ecran [ par fdouieb ] Bonjour,a l'adresse suivante :http://www.csharpfr.com/forum.v2.aspx?ID=260557il y a la possibilité de faire des captures d'ecran.cela fonctionne bien Treeview C#(mettre une image en fond) [ par trioy ] Hello,Petite question, après avoir cherché, sans résultat... y'a t il qelqu'un qui sait comment on fait pour mettre une image en arrière plan dans un [C#] Modifier la couleur d'arrière fond de ComboBox et checkBox [ par bibicool ] Bonjour &#224; tous, J'aimerais modifier la couleur d'arri&#232;re fond de ComboBox et de CheckBox lorsque ceux-ci sont Disabled (Enabled = false). Dimension de l'ecran [ par manou2005 ] Bonjour tout le monde, je voudrais savoir comment faire pour avoir les dimensions de l'&#233;cran en C#.Et merci. Dimension de mon image de fond de ma frame [ par oxboz ] Bonjour, Voila j'ai une image &#224; mettre en image de fond de toutes mes frames. Probleme: Celles ci sont de dimensions diff&#233;rentes. Comment f listbox et image de fond [C#] [ par emachede ] bonjour je voudrais simplement placer une image jpg dans le fond de ma listbox (vu que j'arrive pas &#224; la rendre transparente, je ruse) j'ai essa DirectX (D3D) transparence [ par clemox ] Bonsoir :) &nbsp;Ceux qui fond du directX ont pu se rendre compte que directdraw va bient&#244;t dispara&#238;tre du SDK ... C'est pourtant bien prat Double Buffering [ par emmanuel9 ] Bonjour &#224; tous, En faite j'ai un panel avec un fond blanc et je voudrais faire bouger un carr&#233; noir dessus avec les touches sans que ca sc


Nos sponsors


Sondage...

Comparez les prix

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,608 sec (4)

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