Function to draw a rectangle in Console in C

3

I'm trying to create a function called criar_botao() which printable a rectangle on the screen, with the windows.h library is printing the rectangle but not the size I want. I'm trying to pass the parameters, with 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);
}
    
asked by anonymous 03.03.2016 / 16:04

2 answers

2

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

#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 in Linux, you can call tput through the system() method.

    
03.03.2016 / 22:38
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;

}
    
29.09.2016 / 15:13