IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

.Net 8.0 : Changer la couleur de fond de la console


Sujet :

C#

  1. #1
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 696
    Points : 5 302
    Points
    5 302
    Par défaut .Net 8.0 : Changer la couleur de fond de la console
    Bonjour,

    Je cherche à modifier la couleur de fond d'une application console.
    Pas uniquement le fond du texte affiché, donc Console.BackgroundColor ne me conviens pas.

    En fouillant sur le net j'ai trouvé un bout de code que j'ai du retravailler car il ne compilait pas.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    using System;
    using System.Runtime.InteropServices;
     
    namespace ConsoleBackgroundColor
    {
        class Program
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX csbiex);
     
            [StructLayout(LayoutKind.Sequential)]
            struct CONSOLE_SCREEN_BUFFER_INFOEX
            {
                public uint cbSize;
                public COORD dwCursorPosition;
                public ushort wAttributes;
                public SMALL_RECT srWindow;
                public COORD dwMaximumWindowSize;
                public ushort wPopupAttributes;
                public bool bFullscreenSupported;
                public COLORREF ColorRefs;
            }
     
            [StructLayout(LayoutKind.Sequential)]
            struct COORD
            {
                public short X;
                public short Y;
            }
     
            [StructLayout(LayoutKind.Sequential)]
            struct SMALL_RECT
            {
                public short Left;
                public short Top;
                public short Right;
                public short Bottom;
            }
     
            [StructLayout(LayoutKind.Sequential)]
            struct COLORREF
            {
                public uint ColorValue;
            }
     
            static void Main(string[] args)
            {
                IntPtr hConsoleOutput = GetStdHandle(-11);
                CONSOLE_SCREEN_BUFFER_INFOEX csbiex = new CONSOLE_SCREEN_BUFFER_INFOEX();
                csbiex.cbSize = (uint)Marshal.SizeOf(csbiex);
     
                // Obtenir les informations actuelles de la console
                GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbiex);
     
                // Définir la nouvelle couleur d'arrière-plan (ici en bleu)
                csbiex.ColorRefs.ColorValue = 0x000000FF;
     
                // Appliquer la nouvelle couleur d'arrière-plan
                SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbiex);
     
                Console.WriteLine("Appuyez sur une touche pour quitter...");
                Console.ReadKey();
            }
     
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr GetStdHandle(int nStdHandle);
     
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX csbiex);
        }
    }
    Sauf qu'il n'a aucun effet.
    Je pensais que c'était trivial mais force est de constater que je n'y parviens pas.
    Quelqu'un a déjà fait ça ?

  2. #2
    Membre expérimenté
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    1 164
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 1 164
    Points : 1 692
    Points
    1 692
    Par défaut
    la partie Remarque de la doc de Console.BackgroundColor donne pourtant la réponse (si j'ai bien compris)
    https://learn.microsoft.com/fr-fr/do...et-8.0#remarks
    To change the background color of the console window as a whole, set the BackgroundColor property and call the Clear method.

  3. #3
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 696
    Points : 5 302
    Points
    5 302
    Par défaut
    C'était effectivement trivial.
    Honte à moi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 21/03/2012, 23h48
  2. [Vb.Net] Changer la couleur de fond d'un control "désactivé"
    Par Thierryx dans le forum Windows Forms
    Réponses: 1
    Dernier message: 15/09/2006, 19h02
  3. Réponses: 4
    Dernier message: 26/07/2005, 10h45
  4. [POO] Changer la couleur de fond d'un tableau
    Par Netoman dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 31/12/2004, 20h12
  5. Changer la couleur de fond de l'écran
    Par tnk dans le forum x86 16-bits
    Réponses: 5
    Dernier message: 19/01/2003, 01h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo