/******************************************************************************\
* This is a part of the Microsoft Source Code Samples.
* Copyright (C) 1993 Microsoft Corporation.
* All rights reserved.
* This source code is only intended as a supplement to
* Microsoft Development Tools and/or WinHelp documentation.
* See these sources for detailed information regarding the
* Microsoft samples programs.
\******************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "console.h"
#include "size.h" /* for resizeConBufAndWindow() */
/* used to set our initial console screen buffer size */
#define CONX 80
#define CONY 43
/* prototypes of externally defined demo functions */
extern void demoAllocFree(HANDLE hConOld, HANDLE *hConsole);
extern void demoCreate(HANDLE hConOld);
extern void demoFillAtt(HANDLE hConOut);
extern void demoFillChar(HANDLE hConOut);
extern void demoFlush(HANDLE hConOut);
extern void demoCursor(HANDLE hConOut);
extern void demoConMode(HANDLE hConOut);
extern void demoConInfo(HANDLE hConOut);
extern void demoGetTitle(HANDLE hConOut);
extern void demoGetLargest(HANDLE hConOut);
extern void demoGetNumEvents(HANDLE hConOut);
extern void demoGetNumBut(HANDLE hConOut);
extern void demoReadConOut(HANDLE hConOut);
extern void demoReadConChar(HANDLE hConOut);
extern void demoScrollCon(HANDLE hConOut);
extern void demoSizeInfo(HANDLE hConOut);
extern void demoSetCtrlHandler(HANDLE hConOut);
extern void demoWriteIn(HANDLE hConOut);
// extern void demoCodePage(HANDLE hConOut);
/* information to display on the screen for user to click on */
PCHAR conAPIs[] = {
"AllocConsole Creates a console for the current process",
"CreateConsoleScreenBuffer Returns a handle to a new screen buffer",
"FillConsoleOutputAttribute Writes attributes to the screen buffer",
"FillConsoleOutputCharacter Writes characters to the screen buffer",
"FlushConsoleInputBuffer Clears the console input buffer",
"FreeConsole Frees the current console",
"GenerateConsoleCtrlEvent Generates a console control event",
"GetConsoleCursorInfo Returns console size and visibility",
"GetConsoleMode Returns console input or output mode",
"GetConsoleScreenBufferInfo Returns screen-buffer information",
"GetConsoleTitle Returns console-window title",
"GetLargestConsoleWindowSize Returns largest possible window size",
"GetNumberOfConsoleInputEvents Retrieves number of console-queue events",
"GetNumberOfConsoleMouseButtons Retrieves number of mouse buttons",
"PeekConsoleInput Previews console input data",
"ReadConsoleInput Reads console input data",
"ReadConsoleOutput Reads screen-buffer data",
"ReadConsoleOutputAttribute Reads a console attribute string",
"ReadConsoleOutputCharacter Reads a screen-buffer string",
"ScrollConsoleScreenBuffer Scrolls data in the screen buffer",
"SetConsoleActiveScreenBuffer Changes displayed screen buffer",
"SetConsoleCursorInfo Sets cursor size and visibility",
"SetConsoleCursorPosition Sets cursor position",
"SetConsoleMode Sets console input or output mode",
"SetConsoleScreenBufferSize Changes screen-buffer size",
"SetConsoleTextAttribute Sets attributes for screen text",
"SetConsoleTitle Sets console-window title string",
"SetConsoleWindowInfo Sets console window size",
"SetConsoleCtrlHandler Sets console ctrl-c handler",
"WriteConsoleInput Writes to console input buffer",
"WriteConsoleOutput Writes to screen buffer",
"WriteConsoleOutputAttribute Writes an attribute string to console",
"WriteConsoleOutputCharacter Writes a character string to console"
};
/* this variable holds the number next to the API on the screen that */
/* the user clicks on */
enum cAPIs { ALLOC = 1, CREATE, FILLATT, FILLCHAR, FLUSH, FREE,
GENCTRL, GETCUR, GETMODE, GETCONINFO, GETTITLE,
GETLARGEST, GETNUMEV, GETNUMBUT, PEEK, READCONIN, READCONOUT,
READCONATT, READCONCHAR, SCROLL, SETACTIVE,
SETCURINF, SETCURPOS, SETMODE, SETSIZE, SETATT, SETTITLE, SETINFO,
SETHAND, WRITEIN, WRITEOUT, WRITEATT, WRITECHAR };
/*****************************************************************
* FUNCTION: myGetchar(void) *
* *
* PURPOSE: get a single character from the standard input handle *
* *
* INPUT: none *
* *
* RETURNS: the char received from the console *
*****************************************************************/
CHAR myGetchar(void)
{
HANDLE hStdIn; /* standard input */
DWORD dwInputMode; /* to save the input mode */
BOOL bSuccess;
CHAR chBuf; /* buffer to read into */
DWORD dwRead;
/* get the standard input handle to read from. There is only one */
/* instance of standard input per process at any given time */
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
/* save the console mode */
bSuccess = GetConsoleMode(hStdIn, &dwInputMode);
PERR(bSuccess, "GetconsoleMode");
/* disable line input. Echo input must be disabled when disabling */
/* line input */
bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_LINE_INPUT &
~ENABLE_ECHO_INPUT);
PERR(bSuccess, "SetConsoleMode");
/* read a character from the console input */
bSuccess = ReadFile(hStdIn, &chBuf, sizeof(chBuf), &dwRead, NULL);
PERR(bSuccess, "ReadFile");
/* restore the original console input mode */
bSuccess = SetConsoleMode(hStdIn, dwInputMode);
PERR(bSuccess, "SetConsoleMode");
return(chBuf);
}
/*********************************************************************
* FUNCTION: perr(PCHAR szFileName, int line, PCHAR szApiName, *
* DWORD dwError) *
* *
* PURPOSE: report API errors. Allocate a new console buffer, display *
* error number and error text, restore previous console *
* buffer *
* *
* INPUT: current source file name, current line number, name of the *
* API that failed, and the error number *
* *
* RETURNS: none *
*********************************************************************/
/* maximum size of the buffer to be returned from FormatMessage */
#define MAX_MSG_BUF_SIZE 512
void perr(PCHAR szFileName, int line, PCHAR szApiName, DWORD dwError)
{
CHAR szTemp[1024];
DWORD cMsgLen;
CHAR *msgBuf; /* buffer for message text from system */
int iButtonPressed; /* receives button pressed in the error box */
/* format our error message */
sprintf(szTemp, "%s: Error %d from %s on line %d:\n", szFileName,
dwError, szApiName, line);
/* get the text description for that error number from the system */
cMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,
MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf, MAX_MSG_BUF_SIZE,
NULL);
if (!cMs
- 1
- 2
- 3
- 4
- 5
- 6
前往页