944,126 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 39262
  • C++ RSS
0

Add a little Graphics to your Console

by on Jan 31st, 2005
Here is an example on how to put at least lines and circles onto your windows console display. Yes, you have to jump through a hoop to do it. Dev-C++ can do this thanks to some BCX generated code. You have to set up your project as a Console Application and link with libgdi32.a in the case of Dev-C++, or GDI32.lib with other compilers.
C++ Code Snippet (Toggle Plain Text)
  1. // draw two lines and a circle on your console screen
  2. // original BCX basic code by Sir Joe Caverly
  3. // translated to C code and modified to work with Dev-C++
  4. // link with GDI32.lib or with Dev-C++ link libgdi32.a via
  5. // Project>>Project Options>>Parameters>>Add Lib>>libgdi32.a
  6. // this is a Windows Console Application
  7.  
  8.  
  9. #include <windows.h> // Win32API Header File
  10. #include <cstring>
  11. #include <cstdio>
  12.  
  13. using namespace std;
  14.  
  15. #define Red RGB (255,0,0)
  16. #define Lime RGB (206,255,0)
  17. #define Blue RGB (0,0,255)
  18.  
  19. static HWND hConWnd;
  20.  
  21. int BCX_Line (HWND,int,int,int,int,int=0,HDC=0);
  22. int BCX_Circle (HWND,int,int,int,int=0,int=0,HDC=0);
  23.  
  24. HWND GetConsoleWndHandle (void);
  25.  
  26. int main()
  27. {
  28. hConWnd = GetConsoleWndHandle();
  29. if (hConWnd)
  30. {
  31. // be creative here, draw your own circles or lines
  32.  
  33. // hwin, centerX, centerY, radius, pencolor
  34. BCX_Circle(hConWnd, 150, 130, 105, Blue);
  35. // hwin, ulcX, ulcY, lrcX, lrcY, pencolor
  36. BCX_Line(hConWnd, 5, 5, 300, 250, Red);
  37. BCX_Line(hConWnd, 295, 5, 5, 250, Lime);
  38.  
  39. getchar(); // wait
  40. }
  41. return 0;
  42. }
  43.  
  44.  
  45. int BCX_Line (HWND Wnd,int x1,int y1,int x2,int y2,int Pen,HDC DrawHDC)
  46. {
  47. int a,b=0;
  48. HPEN hOPen;
  49. // penstyle, width, color
  50. HPEN hNPen = CreatePen(PS_SOLID, 2, Pen);
  51. if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1;
  52. hOPen = (HPEN)SelectObject(DrawHDC, hNPen);
  53. // starting point of line
  54. MoveToEx(DrawHDC, x1, y1, NULL);
  55. // ending point of line
  56. a = LineTo(DrawHDC, x2, y2);
  57. DeleteObject(SelectObject(DrawHDC, hOPen));
  58. if (b) ReleaseDC(Wnd, DrawHDC);
  59. return a;
  60. }
  61.  
  62.  
  63. // converts circle(centerX,centerY,radius,pen) to WinApi function
  64. // ellipse inside box with upper left and lower right coordinates
  65. int BCX_Circle(HWND Wnd,int X,int Y,int R,int Pen,int Fill,HDC DrawHDC)
  66. {
  67. int a, b = 0;
  68. if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1;
  69. // penstyle, width, color
  70. HPEN hNPen = CreatePen(PS_SOLID, 2, Pen);
  71. HPEN hOPen = (HPEN)SelectObject(DrawHDC, hNPen);
  72. HBRUSH hOldBrush;
  73. HBRUSH hNewBrush;
  74. // if true will fill circle with pencolor
  75. if (Fill)
  76. {
  77. hNewBrush = CreateSolidBrush(Pen);
  78. hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush);
  79. }
  80. else
  81. {
  82. hNewBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
  83. hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush);
  84. }
  85. a = Ellipse(DrawHDC, X-R, Y+R, X+R, Y-R);
  86. DeleteObject(SelectObject(DrawHDC, hOPen));
  87. DeleteObject(SelectObject(DrawHDC, hOldBrush));
  88. if (b) ReleaseDC(Wnd, DrawHDC);
  89. return a;
  90. }
  91.  
  92.  
  93. // the hoop ...
  94. HWND GetConsoleWndHandle(void)
  95. {
  96. HWND hConWnd;
  97. OSVERSIONINFO os;
  98. char szTempTitle[64], szClassName[128], szOriginalTitle[1024];
  99.  
  100. os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  101. GetVersionEx( &os );
  102. // may not work on WIN9x
  103. if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0;
  104.  
  105. GetConsoleTitle( szOriginalTitle, sizeof( szOriginalTitle ) );
  106. sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() );
  107. SetConsoleTitle( szTempTitle );
  108. Sleep( 40 );
  109. // handle for NT
  110. hConWnd = FindWindow( NULL, szTempTitle );
  111. SetConsoleTitle( szOriginalTitle );
  112.  
  113. // may not work on WIN9x
  114. if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  115. {
  116. hConWnd = GetWindow( hConWnd, GW_CHILD );
  117. if ( hConWnd == NULL ) return 0;
  118. GetClassName( hConWnd, szClassName, sizeof ( szClassName ) );
  119. while ( strcmp( szClassName, "ttyGrab" ) != 0 )
  120. {
  121. hConWnd = GetNextWindow( hConWnd, GW_HWNDNEXT );
  122. if ( hConWnd == NULL ) return 0;
  123. GetClassName( hConWnd, szClassName, sizeof( szClassName ) );
  124. }
  125. }
  126. return hConWnd;
  127. }
  128.  
  129.  
Comments on this Code Snippet
Mar 6th, 2005
0

Re: Add a little Graphics to your Console

Replaced iostream and cin.get() with the cstdio header and getchar() to reduce the exe file size from 415k to 7k
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 1st, 2007
0

Re: Add a little Graphics to your Console

The code works flawlessly. Is there a way to add to these features to plot graphs of functions? Or will that require more sophisticated code and libraries?

An example of what I mean is: using the BCX_Line function to draw a figure window, the X and Y axes, and then another function to plot--let's say sin(x) curve.
Newbie Poster
wxstorm is offline Offline
1 posts
since Jun 2007
Dec 21st, 2007
0

Re: Add a little Graphics to your Console

how can I move the drawen object using the mouse from his place to onther please
Newbie Poster
Ali.M.Habib is offline Offline
17 posts
since Nov 2007
Jun 12th, 2008
0

Re: Add a little Graphics to your Console

Hi!

How can I clear the screen of graphical images ?

I'm reffering to a function similar to BORLAND C's cleardevice() funcion.

Thanks in advance,

gh0sst
Newbie Poster
gh0sst is offline Offline
3 posts
since Jun 2008
May 12th, 2010
-2

Re: Add a little Graphics to your Console

The code snippet does not compile with Dev-C++ 4.9.9.2. The error messages look like this:
[Linker error] undefined reference to 'CreatePen@12'
[Linker error] undefined reference to 'SelectObject@8'
...
and so on..
Newbie Poster
tordenflesk is offline Offline
1 posts
since May 2010
May 12th, 2010
0

Re: Add a little Graphics to your Console

This thread was originally posted 5 years ago, before that version of the compiler existed. So use the code at your own risk.
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
May 12th, 2010
0

Re: Add a little Graphics to your Console

>> The code snippet does not compile with Dev-C++ 4.9.9.2. The error messages look like this:
>> [Linker error] undefined reference to 'CreatePen@12'
>> [Linker error] undefined reference to 'SelectObject@8'


See the code snippet comments, there it says;
  • with Dev-C++ link libgdi32.a via Project>>Project Options>>Parameters>>Add Lib>>libgdi32.a
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Message:
Previous Thread in C++ Forum Timeline: Help Me
Next Thread in C++ Forum Timeline: c++ program for standard deviation





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


Follow us on Twitter


© 2011 DaniWeb® LLC