Voici une classe qui vous permettra de gérer des affichages de fond d'écran facilement.
Source / Exemple :
// 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.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.