Memory Drive

반응형
  1. #include <stdlib.h>
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. HANDLE wHnd;    // Handle to write to the console.
  5. HANDLE rHnd;    // Handle to read from the console.
  6. int main(int argc, char* argv[]) {
  7.     // Set up the handles for reading/writing:
  8.     wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
  9.     rHnd = GetStdHandle(STD_INPUT_HANDLE);
  10.     // Change the window title:
  11.     SetConsoleTitle("Win32 Console Control Demo");
  12.     // Set up the required window size:
  13.     SMALL_RECT windowSize = {0, 0, 79, 49};
  14.    
  15.     // Change the console window size:
  16.     SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
  17.    
  18.     // Create a COORD to hold the buffer size:
  19.     COORD bufferSize = {80, 50};
  20.     // Change the internal buffer size:
  21.     SetConsoleScreenBufferSize(wHnd, bufferSize);
  22.     // Set up the character buffer:
  23.     CHAR_INFO consoleBuffer[80*50];
  24.     // Clear the CHAR_INFO buffer:
  25.     for (int i=0; i<80*50; ++i) {
  26.         // Fill it with white-backgrounded spaces
  27.         consoleBuffer[i].Char.AsciiChar = ' ';
  28.         consoleBuffer[i].Attributes =
  29.             BACKGROUND_BLUE
  30.             BACKGROUND_GREEN
  31.             BACKGROUND_RED
  32.             BACKGROUND_INTENSITY;
  33.     }
  34.     // Set up the positions:
  35.     COORD charBufSize = {80,50};
  36.     COORD characterPos = {0,0};
  37.     SMALL_RECT writeArea = {0,0,79,49};
  38.     // Copy to display:
  39.     WriteConsoleOutput(wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);
  40.     // How many events have happened?
  41.     DWORD numEvents = 0;
  42.     // How many events have we read from the console?
  43.     DWORD numEventsRead = 0;
  44.    
  45.     // Boolean flag to state whether app is running or not.
  46.     bool appIsRunning = true;
  47.     // If we set appIsRunning to false, the program will end!
  48.     while (appIsRunning) {
  49.         // Find out how many console events have happened:
  50.         GetNumberOfConsoleInputEvents(rHnd, &numEvents);
  51.         // If it's not zero (something happened...)
  52.         if (numEvents!=0) {
  53.             // Create a buffer of that size to store the events
  54.             INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];
  55.             // Read the console events into that buffer, and save how
  56.             // many events have been read into numEventsRead.
  57.             ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  58.             // Now, cycle through all the events that have happened:
  59.             for (unsigned int i=0; i<numEventsRead; ++i) {
  60.                 // Check the event type: was it a key?
  61.                 if (eventBuffer[i].EventType==KEY_EVENT) {
  62.                     // Yes! Was the key code the escape key?
  63.                     if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE) {
  64.                         // Yes, it was, so set the appIsRunning to false.
  65.                         appIsRunning = false;
  66.                    
  67.                     // Was if the 'c' key?
  68.                     } else if (eventBuffer[i].Event.KeyEvent.uChar.AsciiChar=='c') {
  69.                         // Yes, so clear the buffer to spaces:
  70.                         for (int i=0; i<80*50; ++i) {
  71.                             consoleBuffer[i].Char.AsciiChar = ' ';
  72.                         }
  73.                         // Redraw our buffer:
  74.                         WriteConsoleOutput(
  75.                             wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);
  76.                     }
  77.                 } else if (eventBuffer[i].EventType==MOUSE_EVENT) {
  78.                     // Set the index to our buffer of CHAR_INFO
  79.                     int offsetPos =
  80.                         eventBuffer[i].Event.MouseEvent.dwMousePosition.X
  81.                         + 80 * eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
  82.                     // Is it a left click?
  83.                     if (eventBuffer[i].Event.MouseEvent.dwButtonState
  84.                         & FROM_LEFT_1ST_BUTTON_PRESSED) {
  85.                         // Yep, so set with character 0xDB (solid block)
  86.                         consoleBuffer[offsetPos].Char.AsciiChar = 'O';
  87.                         // Redraw our buffer:
  88.                         WriteConsoleOutput(
  89.                             wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);
  90.              
  91.                     // Is it a right click?
  92.                     } else if (eventBuffer[i].Event.MouseEvent.dwButtonState
  93.                         & RIGHTMOST_BUTTON_PRESSED) {
  94.                         // Yep, so set with character 0xB1 (50% block)
  95.                         consoleBuffer[offsetPos].Char.AsciiChar = 'b';
  96.                         // Redraw our buffer:
  97.                         WriteConsoleOutput(
  98.                             wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);
  99.              
  100.                     // Is it a middle click?
  101.                     } else if (eventBuffer[i].Event.MouseEvent.dwButtonState
  102.                         & FROM_LEFT_2ND_BUTTON_PRESSED) {
  103.                         // Yep, so set with character space.
  104.                         consoleBuffer[offsetPos].Char.AsciiChar = ' ';
  105.                         // Redraw our buffer:
  106.                         WriteConsoleOutput(
  107.                             wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);           
  108.                     }
  109.                 }
  110.             }
  111.             // Clean up our event buffer:
  112.             delete eventBuffer;
  113.         }
  114.     }
  115.     // Exit
  116.     return 0;
  117. }


Console창 사이즈 조절이 가능하다.
반응형