954,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Console GDI

i Wrote some basic code on something i saw here i while ago and was wondering what people could do with it. it uses a console and you can draw on the console or the entire desktop. i was hopping there was thing people could do to add or improve my code. if you get linker errors you have to add the libgdi32.a library
Main.cpp

#define _WIN32_WINNT 0x0500
#include <iostream>
#include <windows.h>
#include "Pen.h"

using namespace std;

int main()
{
	Draw draw(0); ///Sets if the drawing is only in the cmd console or in the entire screen
	draw.SetPen(255,100,0,10); ///sets pen color and size SetPen(RED,GREEN,BLUE,PEN SIZE);
	while(true) // loop
	{
		draw.Pen(); /// draws pen
		Sleep(10);
	}//end
	return 0;
}


Pen.h

#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED

#include <iostream>
#include <windows.h>

using namespace std;

class Draw{
	private:
	POINT p;
	HWND console;
	HDC hdc;
	int R;
	int G;
	int B;
	int Size;
	public:
	Draw(int i)
	{
		/// SetUP
		R=255;
		G=0;
		B=0;
		Size=10;
		if(i == 1){/// to draw in cmd window
		console = GetConsoleWindow();
		hdc = GetDC(console);
		SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
		GetCursorPos(&p);
		ScreenToClient(console, &p);
		MoveToEx(hdc, p.x, p.y, NULL);
		}else if(i==0){ /// draw on entire screen
		console = GetConsoleWindow();
		console = NULL;
		hdc = GetDC(0);
		SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
		GetCursorPos(&p);
		ScreenToClient(NULL, &p);
		MoveToEx(hdc, p.x, p.y, 0);
		}
		///End
	}
	//Functions

	void Pen();
	void Pen(int x, int y);
	void SetPen(int r, int g, int b, int s);
	// END

};
///Set pen color, Set Pen Size
void Draw::SetPen(int r, int g, int b, int s){
	R=r;
	G=g;
	B=b;
	Size=s;
	SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
}

/// Draws Pen using mousepos
void Draw::Pen()
{
	GetCursorPos(&p);
	ScreenToClient(console, &p);
	LineTo(hdc, p.x, p.y);
}

///using an x and y value input
void Draw::Pen(int x, int y){
	GetCursorPos(&p);
	ScreenToClient(console, &p);
	LineTo(hdc, x, y);
}


#endif // PEN_H_INCLUDED
chrischavez
Newbie Poster
6 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

This has no sense.
Drawing must be done in WM_PAINT
Console are not for drawing

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 
This has no sense. Drawing must be done in WM_PAINT Console are not for drawing

Why not? If it's possible? Try thinking outside the box :)

To be honest, I can't think of any uses right now, but if were to make a console-based hangman game or something in that order, this might come in useful

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Well if one was to hide the console window and paint on the main window / active window you could use it in conjunction with something like presentations as a pointing aidor something like that.

@marco
Thats like saying you should do all DirectX Drawing inside WM_PAINT :)

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

Well my use for this was for presentations. so i can draw on the screen. only problem is that if it draws over anything that changes or moves the line is deleted on that area

chrischavez
Newbie Poster
6 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Store what has been drawn by the user somehow, then redraw it :)

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

thanks but sorry to bother you how do i store what has been painted

chrischavez
Newbie Poster
6 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Store the points at which you have something drawn on the screen in an array/vector or what ever you feel is best.

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You