Advertisement
pushrbx

Untitled

Jan 17th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. // HomeWork1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <tchar.h>
  7. #include <iostream>
  8. #include <Windows.h>
  9.  
  10. using namespace std;
  11.  
  12. int PressAnyKey(char *prompt) // msdn library
  13. {
  14.     DWORD mode;
  15.     HANDLE hstdin;
  16.     INPUT_RECORD inrec;
  17.     DWORD count;
  18.     char default_prompt[] = "Press a key to continue...";
  19.  
  20.     // Set the console mode to no-echo, raw input,
  21.     // and no window or mouse events.
  22.     hstdin = GetStdHandle(STD_INPUT_HANDLE);
  23.     if (hstdin == INVALID_HANDLE_VALUE || !GetConsoleMode(hstdin, &mode) || !SetConsoleMode(hstdin, 0))
  24.         return 0;
  25.  
  26.     if (!prompt)
  27.         prompt = default_prompt;
  28.  
  29.     // little fix on the example code by me: convert const char* to WCHAR*
  30.     int buffer_size = MultiByteToWideChar(0, 0, prompt, -1, NULL, NULL) * sizeof(wchar_t);
  31.     WCHAR *w_prompt = new WCHAR[buffer_size]; // allocate memory
  32.     MultiByteToWideChar(0, 0, prompt, -1, w_prompt, buffer_size);
  33.  
  34.     // Instruct the user
  35.     WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), w_prompt, lstrlen(w_prompt), &count, NULL);
  36.  
  37.     FlushConsoleInputBuffer(hstdin);
  38.  
  39.     // Get a single key RELEASE
  40.     do ReadConsoleInput(hstdin, &inrec, 1, &count);
  41.     while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
  42.  
  43.     // Restore the original console mode
  44.     SetConsoleMode(hstdin, mode);
  45.  
  46.     return inrec.Event.KeyEvent.wVirtualKeyCode;
  47. }
  48.  
  49. int _tmain(int argc, _TCHAR* argv[])
  50. {
  51.     cout << "Countback program." << endl;
  52.  
  53.     for(int i = 0; i <= 10; i++)
  54.     {
  55.         cout << i << endl;
  56.  
  57.         if(i == 10)
  58.         {
  59.             cout << "Thunderbirds are go!" << endl;
  60.             break;
  61.         }
  62.  
  63.         Sleep(1000);
  64.     }
  65.  
  66.     PressAnyKey("Press any key to continue...");
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement