Function to draw a rectangle in the Console in C

Asked

Viewed 2,039 times

3

I’m trying to create a function called criar_botao() that prints a rectangle on the screen, with the library windows.h is printing the rectangle but not the size I want. I’m trying to pass the parameters, with the position x and y where the rectangle will be created, and the width and height of the rectangle.

I have this code. If anyone can help me:

void desenha_botao(int altura,int largura, int x, int y,const int cor) {
    COORD posicao = { x, y };
    CHAR_INFO *tela_buffer = calloc(largura * altura, sizeof(CHAR_INFO));
    COORD tamanho_grid = { largura, altura };
    COORD zero_zero = { 0, 0 };
    SMALL_RECT retangulo = { altura, posicao.X, posicao.X + (largura - 1), posicao.Y + (altura - 1) };
    HANDLE h_output = GetStdHandle(STD_OUTPUT_HANDLE);
    int i, j;

    for (j = 0; j < tamanho_grid.Y; j++) {

        for (i = 0; i < tamanho_grid.X; i++) {
            tela_buffer[i + j * largura].Char.AsciiChar = '\xDB';
            tela_buffer[i + j * largura].Attributes = FOREGROUND_GREEN;
        }
    }
    WriteConsoleOutput(h_output, tela_buffer, tamanho_grid, zero_zero, &retangulo);
}

2 answers

2

If you are in Windows you can use the conio library. h

#include <conio.h>
#include <stdio.h>
void draw_button(int x, int y, int w, int h, const int color){
    int i,j;
    for(i=0;i<w;i++)
        for(j=0;j<h;j++){
            // verifica se está nas extremidades do botão
            if(!i && i==(w-1) && !j && j==(h-1)){
                gotoxy(x+i, y+j);
                textcolor(color);
                printf("#");
            }
        }

    printf("\n");
}

void main(){
    draw_button(2,2,10,5)
}

if you are on linux, you can call the tput through the method system().

1

void box(int iTop, int iLeft, int iBottom, int iRight, int icor, char cchar)

{
    int i;
    int x;
    int x1 = iTop;
    int x2 = iLeft;
    int y1 = iBottom;
    int y2 = iRight;

    BOX box = m_frame(1);
    DWORD dummy;
    dwConSize = iRight - iLeft;
    for (x = iTop; x <= iBottom; x++) {
        COORD Home = {
            iLeft + 1, x
        };
        FillConsoleOutputAttribute(hConsole, icor, dwConSize, Home, &cCharsWritten);
        FillConsoleOutputCharacter(hConsole, cchar, iRight - iLeft, Home, &dummy);
    }
    SetConsoleTextAttribute(hConsole, icor);

    setpos(x1, x2);
    cout << box.c1;
    for (i = x2; i < y2; i++)
        cout << box.c5;
    cout << box.c2;
    for (i = x1 + 1; i < y1; i++) {
        cout << "\n";
        setpos(i, x2);
        cout << box.c6;
        setpos(i, y2 + 1);
        cout << box.c6;
    }
    setpos(y1, x2);
    cout << box.c3;
    for (i = x2; i < y2; i++)
        cout << box.c5;
    cout << box.c4;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.