943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1407
  • C++ RSS
Feb 4th, 2009
0

Console GDI

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  1. #define _WIN32_WINNT 0x0500
  2. #include <iostream>
  3. #include <windows.h>
  4. #include "Pen.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. Draw draw(0); ///Sets if the drawing is only in the cmd console or in the entire screen
  11. draw.SetPen(255,100,0,10); ///sets pen color and size SetPen(RED,GREEN,BLUE,PEN SIZE);
  12. while(true) // loop
  13. {
  14. draw.Pen(); /// draws pen
  15. Sleep(10);
  16. }//end
  17. return 0;
  18. }

Pen.h
c++ Syntax (Toggle Plain Text)
  1. #ifndef PEN_H_INCLUDED
  2. #define PEN_H_INCLUDED
  3.  
  4. #include <iostream>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. class Draw{
  10. private:
  11. POINT p;
  12. HWND console;
  13. HDC hdc;
  14. int R;
  15. int G;
  16. int B;
  17. int Size;
  18. public:
  19. Draw(int i)
  20. {
  21. /// SetUP
  22. R=255;
  23. G=0;
  24. B=0;
  25. Size=10;
  26. if(i == 1){/// to draw in cmd window
  27. console = GetConsoleWindow();
  28. hdc = GetDC(console);
  29. SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
  30. GetCursorPos(&p);
  31. ScreenToClient(console, &p);
  32. MoveToEx(hdc, p.x, p.y, NULL);
  33. }else if(i==0){ /// draw on entire screen
  34. console = GetConsoleWindow();
  35. console = NULL;
  36. hdc = GetDC(0);
  37. SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
  38. GetCursorPos(&p);
  39. ScreenToClient(NULL, &p);
  40. MoveToEx(hdc, p.x, p.y, 0);
  41. }
  42. ///End
  43. }
  44. //Functions
  45.  
  46. void Pen();
  47. void Pen(int x, int y);
  48. void SetPen(int r, int g, int b, int s);
  49. // END
  50.  
  51. };
  52. ///Set pen color, Set Pen Size
  53. void Draw::SetPen(int r, int g, int b, int s){
  54. R=r;
  55. G=g;
  56. B=b;
  57. Size=s;
  58. SelectObject(hdc, CreatePen(0, Size, RGB(R,G,B)));
  59. }
  60.  
  61. /// Draws Pen using mousepos
  62. void Draw::Pen()
  63. {
  64. GetCursorPos(&p);
  65. ScreenToClient(console, &p);
  66. LineTo(hdc, p.x, p.y);
  67. }
  68.  
  69. ///using an x and y value input
  70. void Draw::Pen(int x, int y){
  71. GetCursorPos(&p);
  72. ScreenToClient(console, &p);
  73. LineTo(hdc, x, y);
  74. }
  75.  
  76.  
  77. #endif // PEN_H_INCLUDED
Last edited by chrischavez; Feb 4th, 2009 at 12:45 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrischavez is offline Offline
6 posts
since Dec 2008
Feb 4th, 2009
0

Re: Console GDI

This has no sense.
Drawing must be done in WM_PAINT
Console are not for drawing
Reputation Points: -76
Solved Threads: 14
Junior Poster
marco93 is offline Offline
132 posts
since Apr 2008
Feb 4th, 2009
0

Re: Console GDI

Click to Expand / Collapse  Quote originally posted by marco93 ...
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
Last edited by Nick Evan; Feb 4th, 2009 at 11:14 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 4th, 2009
0

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 4th, 2009
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrischavez is offline Offline
6 posts
since Dec 2008
Feb 5th, 2009
0

Re: Console GDI

Store what has been drawn by the user somehow, then redraw it
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 5th, 2009
0

Re: Console GDI

thanks but sorry to bother you how do i store what has been painted
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrischavez is offline Offline
6 posts
since Dec 2008
Feb 6th, 2009
0

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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Running C++ program in the background
Next Thread in C++ Forum Timeline: Heap corruption probelm when i try to return std::string from function in dll





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC