Przed chwilą popatrzyłem na ekran komputera znajomego z pracy i mi szczęka odpadła kiedy zobaczyłem następujący kod (wyświetla wszystkie kolory tła i tekstu):
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("kernel32.dll")]
public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(uint nStdHandle);
public static void Main(string[] args)
{
uint STD_OUTPUT_HANDLE = 0xfffffff5;
IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 1; i < 255; i++)
{
SetConsoleTextAttribute(hConsole, i);
Console.WriteLine("{0:d3} Leave me alone today!", i);
}
SetConsoleTextAttribute(hConsole, 236);
Console.WriteLine("Press Enter to exit ...");
Console.Read();
}
}
}
Po co się tak męczyć, kiedy można zrobić to tak (i mieć kontrolę nad tłem i tekstem):
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
var i = 0;
var colors = Enum.GetValues(typeof(ConsoleColor)).Cast<ConsoleColor>();
foreach (var back in colors)
{
Console.BackgroundColor = back;
foreach(var fore in colors)
{
Console.ForegroundColor = fore;
Console.WriteLine("{0:d3} Leave me alone today!", i++);
}
}
Console.ResetColor();
Console.WriteLine("Press Enter to exit ...");
Console.Read();
}
}
}















Po co? Bo niektorzy lubia COM-owskie perwersje :P
Comments are closed.