« Application console » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 23 : Ligne 23 :
</kode>
</kode>


= [http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application Masquer la fenêtre de la Console] =
== [http://rajeshbailwal.blogspot.fr/2012/03/password-in-c-console-application.html Masquer la saisie d'un mot de passe] ==
<kode lang=csharp>
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
 
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
 
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
</kode>
 
= [http://rajeshbailwal.blogspot.fr/2012/03/password-in-c-console-application.html Masquer la saisie d'un mot de passe] =
<kode lang='csharp'>
<kode lang='csharp'>
public static string ReadPassword()
public static string ReadPassword()
Ligne 80 : Ligne 65 :
     return password;
     return password;
}
}
</kode>
= [http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application Masquer la fenêtre de la Console] =
<kode lang=csharp>
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
</kode>
</kode>



Version du 23 février 2020 à 09:26

Code

Csharp.svg
using System;

namespace MonNS
{
    class MaClasse
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("");
        }
    }
}

Read input

Cs.svg
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Escape) { }

Masquer la saisie d'un mot de passe

Csharp.svg
public static string ReadPassword()
{
    string password = "";
    ConsoleKeyInfo info = Console.ReadKey(true);
    while (info.Key != ConsoleKey.Enter)
    {
        if (info.Key != ConsoleKey.Backspace)
        {
            Console.Write("*");
            password += info.KeyChar;
        }
        else if (info.Key == ConsoleKey.Backspace)
        {
            if (!string.IsNullOrEmpty(password))
            {
                // remove one character from the list of password characters
                password = password.Substring(0, password.Length - 1);

                // get the location of the cursor
                int pos = Console.CursorLeft;

                // move the cursor to the left by one character
                Console.SetCursorPosition(pos - 1, Console.CursorTop);

                // replace it with space
                Console.Write(" ");

                // move the cursor to the left by one character again
                Console.SetCursorPosition(pos - 1, Console.CursorTop);
            }
        }

        info = Console.ReadKey(true);
    }

    // add a new line because user pressed enter at the end of their password
    Console.WriteLine();

    return password;
}

Masquer la fenêtre de la Console

Csharp.svg
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_HIDE = 0;
private const int SW_SHOW = 5;

var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);

Couleurs

Cs.svg
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("...");
Console.ResetColor();