DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Console GDI (http://www.daniweb.com/forums/thread173412.html)

chrischavez Feb 4th, 2009 12:39 am
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

marco93 Feb 4th, 2009 9:54 am
Re: Console GDI
 
This has no sense.
Drawing must be done in WM_PAINT
Console are not for drawing

niek_e Feb 4th, 2009 11:06 am
Re: Console GDI
 
Quote:

Originally Posted by marco93 (Post 795822)
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

Freaky_Chris Feb 4th, 2009 3:43 pm
Re: Console GDI
 
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 :)

chrischavez Feb 4th, 2009 8:10 pm
Re: Console GDI
 
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

Freaky_Chris Feb 5th, 2009 4:25 am
Re: Console GDI
 
Store what has been drawn by the user somehow, then redraw it :)

chrischavez Feb 5th, 2009 10:33 pm
Re: Console GDI
 
thanks but sorry to bother you how do i store what has been painted

Freaky_Chris Feb 6th, 2009 5:23 am
Re: Console GDI
 
Store the points at which you have something drawn on the screen in an array/vector or what ever you feel is best.


All times are GMT -4. The time now is 8:44 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC