커서 위치 알아내는 코드
#include<stdio.h>
#include<Windows.h>
int main()
{
CONSOLE_SCREEN_BUFFER_INFO presentCur;
// 콘솔 출력창의 정보를 담기 위해서 정의한 구조체
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &presentCur);
//현재 커서의 위치 정보를 저장하는 함수
printf("%d, %d\n", presentCur.dwCursorPosition.X, presentCur.dwCursorPosition.Y);
//구조체의 저장한 값 출력
printf("\n\n ");
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &presentCur);
printf("%d, %d\n", presentCur.dwCursorPosition.X, presentCur.dwCursorPosition.Y);
return 0;
}
(실행결과)
메인함수 맨 처음에 CONSOLE_SCREEN_BUFFER_INFO는 Windows.h 헤더파일에 구조체로 정의 되어있다.
그 구조체를 사용하기 위해서 메인함수에서 구조체 변수를 선언해준 것이다.
CONSOLE_SCREEN_BUFFER_INFO structure - Windows Console
Contains information about a console screen buffer.
docs.microsoft.com
GetConsoleScreenBufferInfo함수 역시 Windows.h 헤더파일에 있으며, 두개의 인자값을 받게 된다.
GetConsoleScreenBufferInfo function - Windows Console
Retrieves information about the specified console screen buffer.
docs.microsoft.com
첫번째 인자값으로 읽기 권한이 있는 콘솔 화면 버퍼에 대한 핸들이 필요하고
Console Buffer Security and Access Rights - Windows Console
The Windows security model enables you to control access to console input buffers and console screen buffers. For more information about security, see Access-Control Model.
docs.microsoft.com
두번째 인자값으로 CONSOLE_SCREEN_BUFFER_INFO 구조체에 대한 주소값을 필요로 한다.
그 중에서 첫번째 인자값은 GetStdHandle 함수를 주게 되었는데,
GetStdHandle 함수는 지정된 표준 입,출력,오류에 대한 핸들을 검색하는 기능을 가지고 있다.
물론 이 역시 Windows.h 헤더파일에 있다.
GetStdHandle function - Windows Console
Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
docs.microsoft.com
'C언어 > C_지식_정리' 카테고리의 다른 글
[C 언어] JSON 파일을 이용해서 데이터 받아오기 (0) | 2020.04.15 |
---|---|
[C 언어] 현재 콘솔창의 커서 위치 바꾸기 (0) | 2020.04.14 |
[C 언어] 무작위로 숫자 생성하기 (난수 생성기) (0) | 2020.04.14 |
[C 언어]do while로 최소 한번은 무조건 출력후 반복하기 (0) | 2019.12.23 |