开发者

C#: Resetting the console color pallet after user has modified it

I'm working on creating an ASCII interface for a school project but I've run into a bit of an issue.

Output & properties window for CMD |Screenshot|

I originally modified the colours to be a bit easier on the eyes and be more aesthetically pleasing just because I tend to used that ol' cmd.exe a fair bit, but looks like it's come back to mess with me. Soft darkish blue with canary yellow text, looked alright. Apparently Console.ConsoleColor takes all of its colours from the colours I modified in the cmd.exe properties window. NO GOOD! I don't see why the two are separate :/

Anyone out there know a way开发者_如何学JAVA to override this pallet? I'm hoping for something that would like me set the RGB in the code but if there's a way to access the System.Drawing.Color pallet that would be kickass!

I'm pretty new at programming here so I would very much appreciate a well explained answer, I'm not picky however :P

Here's the code that created the output above:

//show a title
        Console.Title = "Contact Book";

        //set up the console window all nice like   
        Console.SetWindowSize(80, 50);

        Console.SetBufferSize(80, 50);

        //my attempt to reset the colors :( does not work
        Console.ResetColor();

        Console.BackgroundColor = ConsoleColor.Black;

        Console.ForegroundColor = ConsoleColor.White;

        Console.Clear();

        //write some stuff... :/
        Console.WriteLine("{0} {1}", Console.LargestWindowWidth, Console.LargestWindowHeight);

        Console.WriteLine("11111111112222222222333333333344444444445555555555666666666667777777777888888888889999999999900000000000");

        for (int i = 2; i != 10; i++)
        {
            Console.WriteLine("{0}", i);
        }

PS: Would have embedded the image but this is my first time on stackoverflow.com so I have no rep :)


This is all very much by design. A console app can only ever display 16 distinct colors. The shortcut properties allows a user to customize those colors, much like the Display applet in Control Panel allows her to change the Windows theme colors for a GUI app. Overriding the user's preference is almost never not a mistake.

But you can on Vista and Win7 by pinvoking the SetConsoleScreenBufferEx() API function. Boilerplate code (in C) is in this thread, visit pinvoke.net for the declarations you'll need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜