Last active
August 29, 2015 14:20
-
-
Save Regentag/92f64f06954ae08577d4 to your computer and use it in GitHub Desktop.
윈도 콘솔 함수 예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <stdlib.h> | |
void SetConsoleSize(HANDLE hCon, short width, short height) | |
{ | |
COORD c; | |
c.X = width; | |
c.Y = height; | |
SMALL_RECT r; | |
r.Top = 0; | |
r.Left = 0; | |
r.Bottom = (height - 1); | |
r.Right = (width - 1); | |
SetConsoleScreenBufferSize(hCon, c); | |
SetConsoleWindowInfo(hCon, TRUE, &r); | |
} | |
void ClearConsole(HANDLE hCon) | |
{ | |
CONSOLE_SCREEN_BUFFER_INFO buffInfo; | |
if (GetConsoleScreenBufferInfo(hCon, &buffInfo)) | |
{ | |
DWORD numOfWritten; | |
COORD wp; | |
wp.X = 0; | |
wp.Y = 0; | |
int len = buffInfo.dwSize.X * buffInfo.dwSize.Y; | |
FillConsoleOutputCharacter(hCon, | |
L' ', len, | |
wp, &numOfWritten); | |
} | |
} | |
COORD GetRandomPos(short width, short height) | |
{ | |
COORD c; | |
c.X = (rand() % width); | |
c.Y = (rand() % height); | |
return c; | |
} | |
void SetCharAt(HANDLE hCon, COORD pos, wchar_t c) | |
{ | |
DWORD numOfWritten; | |
FillConsoleOutputCharacter(hCon, | |
c, 1, | |
pos, &numOfWritten); | |
} | |
int main(int argc, char** argv) | |
{ | |
const short WIDTH = 40, HEIGHT = 20; | |
HANDLE conHWnd = GetConsoleWindow(); | |
HANDLE hCon = CreateConsoleScreenBuffer( | |
GENERIC_READ | GENERIC_WRITE, | |
FILE_SHARE_READ, | |
NULL, | |
CONSOLE_TEXTMODE_BUFFER, | |
NULL); | |
SetConsoleTitle(L"CONSOLE TEST"); | |
SetConsoleActiveScreenBuffer(hCon); | |
SetConsoleSize(hCon, WIDTH, HEIGHT); | |
int count = 0; | |
while (TRUE) | |
{ | |
if (count > 100) | |
{ | |
ClearConsole(hCon); | |
count = 0; | |
} | |
SetCharAt(hCon, GetRandomPos(WIDTH, HEIGHT), L'*'); | |
++count; | |
Sleep(100); | |
} | |
ClearConsole(hCon); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment