Click here to Skip to main content
Click here to Skip to main content

Tagged as

C++ -> Drawing Rectangles to Console

, 25 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
C++ -> Drawing Rectangles to Console
For drawing rectangles to console, I have two functions. It draws a rectangle with ASCII chars Smile | :)
One sets the cursor position. The second one draws the rectangle.
The code:
//Caner Korkmaz
#pragma once
//The libs
#include <iostream>
#include <Windows.h>

 *This method sets the cursor position.
BOOL setxy(int x, int y)
	COORD c = {x,y};
	return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 *This method draws the rectangle.
void DrawRect(int x, int y, int width, int height, int curPosX=0, int curPosY=0)
	setxy(x,y);cout << char(201);
	for(int i = 1; i < width; i++)cout << char(205);
	cout << char(187);
	setxy(x,height + y);cout << char(200);
	for(int i = 1; i < width; i++)cout << char(205);
	cout << char(188);
	for(int i = y + 1; i < height + y; i++)
		setxy(x,i);cout << char(186);
		setxy(x + width,i);cout << char(186);
Copy the code in a header file -> then use in your projects or games Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Caner Korkmaz
Turkey Turkey
No Biography provided

Comments and Discussions

GeneralThanks for reporting :) I was found the bug 2 days ago at my... PinmemberCaner Korkmaz25-Aug-11 1:47 
Thanks for reporting Smile | :)
I was found the bug 2 days ago at my game but I forgot to change this :(
GeneralHello, good job!, just two bugs on your code: setxy(x,heig... PinmemberJose David Pujo25-Aug-11 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 25 Aug 2011
Article Copyright 2011 by Caner Korkmaz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid