1

how do i create a red rectangle in console application that is a certain amount of pixels in width and height.

I have found ways to do it but not in a way that i can decide how many pixels it is in widht and height. If you can please help me with my problem.

I have tried almost anything that comes up on google and somw things that i tried myslef.

I thought of this but that doesnt specify the amount of pixels and i cant change the amount either:

Console.OutpuEncoding = Encoding.GetEncoding(866);
Console.Writeline("┌─┐");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
user2979104
  • 13
  • 1
  • 5
  • 1
    @Liath in a console application? Can you do that? – Maciej Stachowski Jan 13 '14 at 13:12
  • @MaciejStachowski Apparently you can manipulate the buffer all you like but it wont resemble the console any more http://stackoverflow.com/questions/2754518/how-can-i-write-fast-colored-output-to-console – Gusdor Jan 13 '14 at 13:14
  • @Liath I added an example of what ive tried. – user2979104 Jan 13 '14 at 13:22
  • @Gusdor I'm very much not in the know of P/Invoke and WinAPI, but it seems that it's still a text buffer. A n x m pixel rectangle could span from 1 character to 80 x 25, and the edges would probably end up in the middle of a character field. So unless there's some way to query for current console window settings (font size, in particular), then I don't see it. – Maciej Stachowski Jan 13 '14 at 13:22
  • 1
    Pixels? The console is a text buffer where the font is usually fixed width and user changeable. I think this is a case of "looking for a solution to the wrong problem" - why do you need a rectangle sized by pixels? – Skizz Jan 13 '14 at 13:22
  • @Skizz I got a "challenge" from a friend that has alot experience in programming but I am very new to it and this is what he said. – user2979104 Jan 13 '14 at 13:24
  • @Skizz I need it by pixels because thats what my challenge is. He did give me a tip that you need to call for a using "thing" to be able to do this. – user2979104 Jan 13 '14 at 13:28
  • http://stackoverflow.com/questions/12378642/c-pixels-in-console-window - there are some C hacks, maybe you could get it to run via P/Invoke, but I don't know. – Maciej Stachowski Jan 13 '14 at 13:31

1 Answers1

2

Okay. I don't know if the following code will always work, crash, or eat your firstborn, but here it is - drawing a rectangle in a console window, the C# way. Hacked in a few minutes and not optimal by any means, but you can adapt it to your needs.

namespace ConsoleApplication12
{
class Program
{

    [DllImport("gdi32.dll")]
    private extern static int SetPixel(int hdc, int x, int y, int color);

    [DllImport("kernel32.dll")]
    private extern static int GetConsoleWindow();

    [DllImport("user32.dll")]
    private extern static int GetDC(int i);

    static void Main(string[] args)
    {
        int myCon = GetConsoleWindow();
        int myDC = GetDC(myCon);
        for (int i = 50; i < 150; i++)
        {
            for (int j = 50; j < 150; j++)
            {
                if (i == 50 || i == 149 || j == 50 || j == 149)
                    SetPixel(myDC, i, j, 255*256*256 + 255*256 + 255);
            }
        }
        Console.ReadLine();
    }
}
}
Maciej Stachowski
  • 1,708
  • 10
  • 19