adambkehl

Console Clicking

Feb 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.06 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "pch.h"
  4. #include <iostream>
  5.  
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <windows.h>
  9.  
  10. #include <cwchar>
  11.  
  12. //using namespace std;
  13.  
  14. void gotoxy(int x, int y);
  15. void setcolor(WORD color);
  16. void setForeGroundAndBackGroundColor(int ForeGroundColor, int BackGroundColor);
  17. void clearscreen();
  18. void drawpixel(unsigned char x, unsigned char y, unsigned char Color);
  19. void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character);
  20. void drawcircle(int x, int y, int a, int b, int color);
  21. void drawline(int x0, int y0, int x1, int y1, int color);
  22. void drawfilledrectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char bkcol);
  23. void drawframe(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, char text_[]);
  24. void drawwindow(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, unsigned char bkcol, char text_[]);
  25.  
  26. using namespace std;
  27. int main()
  28. {
  29.    
  30.     system("mode 50, 10");
  31.     CONSOLE_FONT_INFOEX cfi;
  32.     cfi.cbSize = sizeof(cfi);
  33.     cfi.nFont = 0;
  34.     cfi.dwFontSize.X = 0;                   // Width of each character in the font
  35.     cfi.dwFontSize.Y = 48;                  // Height
  36.     cfi.FontFamily = FF_DONTCARE;
  37.     cfi.FontWeight = FW_NORMAL;
  38.     wcscpy_s(cfi.FaceName, L"wendy"); // Choose your font
  39.     SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
  40.  
  41.     gotoxy(1, 23);
  42.     setcolor(7);
  43.     clearscreen();
  44.  
  45.     cout << "click anywhere in console window to write - hello world -\n\n\n\n\n\n\n\n\n\n\n\n\n"
  46.         "Press Ctrl+C to Exit";
  47.  
  48.     HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
  49.     HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
  50.     INPUT_RECORD InputRecord;
  51.     DWORD Events;
  52.     COORD coord;
  53.     CONSOLE_CURSOR_INFO cci;
  54.     cci.dwSize = 25;
  55.     cci.bVisible = FALSE;
  56.     SetConsoleCursorInfo(hout, &cci);
  57.     SetConsoleMode(hin, ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
  58.     bool EXITGAME = false;
  59.     int buttonX = 1, buttonY = 1;
  60.     SetConsoleOutputCP(CP_UTF8);
  61.     drawpixel(buttonX, buttonY, 1);
  62.     gotoxy(buttonX + 2, buttonY);
  63.     setcolor(1); //1 = blue, 2 = green, 6 = sand, 7 = white
  64.     cout << "<----- a button \n";
  65.  
  66.  
  67.     while (!EXITGAME)
  68.     {
  69.  
  70.         ReadConsoleInput(hin, &InputRecord, 1, &Events);
  71.  
  72.  
  73.         switch (InputRecord.EventType) {
  74.         case KEY_EVENT: // keyboard input
  75.  
  76.  
  77.             switch (InputRecord.Event.KeyEvent.wVirtualKeyCode)
  78.             {
  79.             case VK_ESCAPE:
  80.                 EXITGAME = TRUE;
  81.                 break;
  82.  
  83.             case VK_SPACE:
  84.  
  85.                 break;
  86.  
  87.  
  88.             case VK_RETURN:
  89.  
  90.                 break;
  91.  
  92.             case VK_LEFT:
  93.                 // left key   move player left
  94.                 cout << "VK_LEFT   = " << InputRecord.Event.KeyEvent.wVirtualKeyCode << " \n";
  95.  
  96.                 break;
  97.  
  98.             case VK_RIGHT:
  99.                 // right key   move player right
  100.                 cout << "VK_RIGHT   = " << InputRecord.Event.KeyEvent.wVirtualKeyCode << " \n";
  101.  
  102.                 break;
  103.  
  104.             case VK_UP:
  105.                 // up key   move player up
  106.                 cout << "VK_UP   = " << InputRecord.Event.KeyEvent.wVirtualKeyCode << " \n";
  107.  
  108.  
  109.                 break;
  110.  
  111.             case VK_DOWN:
  112.                 // up key   move player down
  113.                 cout << "VK_DOWN   = " << InputRecord.Event.KeyEvent.wVirtualKeyCode << " \n";
  114.  
  115.  
  116.                 break;
  117.  
  118.  
  119.  
  120.             }//switch
  121.  
  122.             //---------------------------------------------------------------------------------
  123.             break;
  124.  
  125.         case MOUSE_EVENT: // mouse input
  126.  
  127.             if (InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
  128.             {
  129.                 coord.X = InputRecord.Event.MouseEvent.dwMousePosition.X;
  130.                 coord.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
  131.                 SetConsoleCursorPosition(hout, coord);
  132.                 //SetConsoleTextAttribute(hout, rand() % 7 + 9);
  133.  
  134.                 if ((InputRecord.Event.MouseEvent.dwMousePosition.X == buttonX) &&
  135.                     (InputRecord.Event.MouseEvent.dwMousePosition.Y == buttonY)) {
  136.  
  137.                     clearscreen();
  138.                     gotoxy(1, 1);
  139.                     setcolor(7);
  140.                     drawpixel(buttonX, buttonY, 1);
  141.                     setcolor(3);
  142.                     cout << " mybutton was pressed \n";
  143.                     setcolor(7);
  144.                     Sleep(500);
  145.                     drawpixel(buttonX, buttonY, 1);
  146.                     gotoxy(buttonX + 2, buttonY);
  147.                     setcolor(3);
  148.                     cout << "<----- a button      \n";
  149.  
  150.  
  151.                 }
  152.                
  153.                 cout << "**********";
  154.  
  155.             }// mouse
  156.  
  157.             break;
  158.  
  159.         case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
  160.             ;
  161.             break;
  162.  
  163.         case FOCUS_EVENT:  // disregard focus events
  164.  
  165.         case MENU_EVENT:   // disregard menu events
  166.  
  167.             break;
  168.  
  169.         default:
  170.             cout << "Unknown event type \n";
  171.             break;
  172.         }
  173.  
  174.  
  175.  
  176.         FlushConsoleInputBuffer(hin);
  177.     }
  178.     gotoxy(1, 23);
  179.     setcolor(7);
  180.     clearscreen();
  181.     cout << "\n";
  182.     return 0;
  183.  
  184. }
  185.  
  186.  
  187.  
  188. void gotoxy(int x, int y) {
  189.     COORD coord;
  190.     coord.X = x; coord.Y = y;
  191.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  192.     return;
  193. }
  194.  
  195. //*****************************************************************************
  196.  
  197. void setcolor(WORD color) {
  198.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
  199.     return;
  200. }
  201.  
  202. //*****************************************************************************
  203.  
  204.  
  205. //
  206. //     colors:
  207. //     0 = Black
  208. //     1 = Blue
  209. //     2 = Green
  210. //     3 = Cyan
  211. //     4 = Red
  212. //     5 = Magenta
  213. //     6 = Yellow
  214. //     7 = LightGray
  215. //     8 = DarkGray
  216. //     9 = LightBlue
  217. //     10 = LightGreen
  218. //     11 = LightCyan
  219. //     12 = LightRed
  220. //     13 = LightMagenta
  221. //     14 = LightYellow
  222. //     15 = White
  223.  
  224.  
  225. //
  226.  
  227. //*****************************************************************************
  228.  
  229. void setForeGroundAndBackGroundColor(int ForeGroundColor, int BackGroundColor) {
  230.     int color = 16 * BackGroundColor + ForeGroundColor;
  231.     setcolor(color);
  232. }
  233.  
  234. //*****************************************************************************
  235.  
  236. void clearscreen() {
  237.     COORD coordScreen = { 0, 0 };
  238.     DWORD cCharsWritten;
  239.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  240.     DWORD dwConSize;
  241.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  242.  
  243.     GetConsoleScreenBufferInfo(hConsole, &csbi);
  244.     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  245.     FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  246.     GetConsoleScreenBufferInfo(hConsole, &csbi);
  247.     FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  248.     SetConsoleCursorPosition(hConsole, coordScreen);
  249.     return;
  250. }
  251.  
  252. //*****************************************************************************
  253.  
  254. void drawpixel(unsigned char x, unsigned char y, unsigned char Color) {
  255.     setcolor(Color);
  256.     gotoxy(x, y); printf("Û");
  257. }
  258. //*****************************************************************************
  259.  
  260. void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character) {
  261.     setcolor(Color);
  262.     gotoxy(x, y); printf("%c", character);
  263. }
  264.  
  265. //*****************************************************************************
  266.  
  267. void drawcircle(int x, int y, int a, int b, int color) {
  268.     int wx, wy;
  269.     int thresh;
  270.     int asq = a * a;
  271.     int bsq = b * b;
  272.     int xa, ya;
  273.  
  274.     drawpixel(x, y + b, color);
  275.     drawpixel(x, y - b, color);
  276.  
  277.     wx = 0;
  278.     wy = b;
  279.     xa = 0;
  280.     ya = asq * 2 * b;
  281.     thresh = asq / 4 - asq * b;
  282.  
  283.     for (;;) {
  284.         thresh += xa + bsq;
  285.  
  286.         if (thresh >= 0) {
  287.             ya -= asq * 2;
  288.             thresh -= ya;
  289.             wy--;
  290.         }
  291.  
  292.         xa += bsq * 2;
  293.         wx++;
  294.  
  295.         if (xa >= ya)
  296.             break;
  297.  
  298.  
  299.         drawpixel(x + wx, y - wy, color);
  300.         drawpixel(x - wx, y - wy, color);
  301.         drawpixel(x + wx, y + wy, color);
  302.         drawpixel(x - wx, y + wy, color);
  303.     }
  304.  
  305.     drawpixel(x + a, y, color);
  306.     drawpixel(x - a, y, color);
  307.  
  308.     wx = a;
  309.     wy = 0;
  310.     xa = bsq * 2 * a;
  311.  
  312.     ya = 0;
  313.     thresh = bsq / 4 - bsq * a;
  314.  
  315.     for (;;) {
  316.         thresh += ya + asq;
  317.  
  318.         if (thresh >= 0) {
  319.             xa -= bsq * 2;
  320.             thresh = thresh - xa;
  321.             wx--;
  322.         }
  323.  
  324.         ya += asq * 2;
  325.         wy++;
  326.  
  327.         if (ya > xa)
  328.             break;
  329.  
  330.         drawpixel(x + wx, y - wy, color);
  331.         drawpixel(x - wx, y - wy, color);
  332.         drawpixel(x + wx, y + wy, color);
  333.         drawpixel(x - wx, y + wy, color);
  334.     }
  335. }
  336.  
  337. //*****************************************************************************
  338.  
  339. void drawline(int x0, int y0, int x1, int y1, int color) {
  340.     int pix = color;
  341.     int dy = y1 - y0;
  342.     int dx = x1 - x0;
  343.     int stepx, stepy;
  344.  
  345.     if (dy < 0) { dy = -dy;  stepy = -1; }
  346.     else { stepy = 1; }
  347.     if (dx < 0) { dx = -dx;  stepx = -1; }
  348.     else { stepx = 1; }
  349.     dy <<= 1;                                                  // dy is now 2*dy
  350.     dx <<= 1;                                                  // dx is now 2*dx
  351.  
  352.     drawpixel(x0, y0, pix);
  353.     if (dx > dy) {
  354.         int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
  355.         while (x0 != x1) {
  356.             if (fraction >= 0) {
  357.                 y0 += stepy;
  358.                 fraction -= dx;                                // same as fraction -= 2*dx
  359.             }
  360.             x0 += stepx;
  361.             fraction += dy;                                    // same as fraction -= 2*dy
  362.             drawpixel(x0, y0, pix);
  363.         }
  364.     }
  365.     else {
  366.         int fraction = dx - (dy >> 1);
  367.         while (y0 != y1) {
  368.             if (fraction >= 0) {
  369.                 x0 += stepx;
  370.                 fraction -= dy;
  371.             }
  372.             y0 += stepy;
  373.             fraction += dx;
  374.             drawpixel(x0, y0, pix);
  375.         }
  376.     }
  377. }
  378.  
  379. //*****************************************************************************
  380.  
  381.  
  382. void drawframe(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, char text_[]) {
  383.     unsigned i, j, m; {
  384.  
  385.         m = (sx - x);                       //differential
  386.         j = m / 8;                          //adjust
  387.         j = j - 1;                          //more adjustment
  388.         gotoxy(x, y); printf("É");       //Top left corner of drawframe
  389.         gotoxy(sx, y); printf("»");      //Top right corner of drawframe
  390.         gotoxy(x, sy); printf("È");      //Bottom left corner of drawframe
  391.         gotoxy(sx, sy); printf("¼");     //Bottom right corner of drawframe
  392.  
  393.         for (i = x + 1; i < sx; i++) {
  394.             gotoxy(i, y); printf("Í");     // Top horizontol line
  395.             gotoxy(i, sy); printf("Í");    // Bottom Horizontal line
  396.         }
  397.  
  398.         for (i = y + 1; i < sy; i++) {
  399.             gotoxy(x, i); printf("º");     //Left Vertical line
  400.             gotoxy(sx, i); printf("º");    //Right Vertical Line
  401.         }
  402.  
  403.         gotoxy(x + j, y); printf(text_); //put Title
  404.         gotoxy(1, 24);
  405.     }
  406. }
  407.  
  408. //*****************************************************************************
  409.  
  410. void drawfilledrectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char bkcol) {
  411.     int x, y;
  412.     setcolor(bkcol);                       //Set to color bkcol
  413.  
  414.     for (y = y1; y < y2; y++) {                    //Fill Y Region Loop
  415.         for (x = x1; x < x2; x++) {              //Fill X region Loop
  416.             gotoxy(x, y); printf(" ");       //Draw Solid space
  417.         }
  418.     }
  419. }
  420.  
  421. //*****************************************************************************
  422.  
  423. void drawwindow(unsigned x, unsigned y, unsigned sx, unsigned sy,
  424.     unsigned char col, unsigned char col2, unsigned char bkcol, char text_[]) {
  425.     drawfilledrectangle(x, y, sx, sy, bkcol);
  426.     drawframe(x, y, sx, sy, col, col2, text_);
  427. }
  428.  
  429. //*****************************************************************************
  430.  
  431.  
  432. void drawcolorpalette() {
  433.     for (int i = 0; i < 16; i++) {
  434.         for (int j = 0; j < 16; j++) {
  435.             setForeGroundAndBackGroundColor(i, j);
  436.             gotoxy(i * 4, j); printf("%d", (i*j) + 1);
  437.  
  438.  
  439.         }
  440.  
  441.     }
  442.  
  443. }
  444.  
  445.  
  446. //*****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment