Add a little Graphics to your Console

Please support our C++ advertiser: Intel Parallel Studio Home
vegaseat vegaseat is offline Offline Jan 31st, 2005, 1:07 am |
0
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.
Quick reply to this message  
C++ Syntax
  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.  
0
vegaseat vegaseat is offline Offline | Mar 6th, 2005
Replaced iostream and cin.get() with the cstdio header and getchar() to reduce the exe file size from 415k to 7k
 
0
wxstorm wxstorm is offline Offline | Jun 1st, 2007
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.
 
0
Ali.M.Habib Ali.M.Habib is offline Offline | Dec 21st, 2007
how can I move the drawen object using the mouse from his place to onther please
 
0
gh0sst gh0sst is offline Offline | Jun 12th, 2008
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
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC