Terminal control/Positional read: Difference between revisions
m (Category:Curses) |
(Add C/Win32.) |
||
Line 16: | Line 16: | ||
20 REM So we subtract one from the coordinates |
20 REM So we subtract one from the coordinates |
||
30 LET c$ = SCREEN$(5,2)</lang> |
30 LET c$ = SCREEN$(5,2)</lang> |
||
=={{header|C}}== |
|||
With the Windows console, call <code>GetConsoleScreenBufferInfo()</code> to find the top-left corner of the display screen. Then add (3, 6) to the top-left corner and call <code>ReadConsoleOutputCharacterW()</code> to read character. This program reckons that the top-left corner is (0, 0). |
|||
{{libheader|Win32}} |
|||
<lang c>#include <windows.h> |
|||
#include <wchar.h> |
|||
int |
|||
main() |
|||
{ |
|||
CONSOLE_SCREEN_BUFFER_INFO info; |
|||
COORD pos; |
|||
HANDLE conout; |
|||
long len; |
|||
wchar_t c; |
|||
/* Create a handle to the console screen. */ |
|||
conout = CreateFileW(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, |
|||
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
|||
0, NULL); |
|||
if (conout == INVALID_HANDLE_VALUE) |
|||
return 1; |
|||
/* Where is the display window? */ |
|||
if (GetConsoleScreenBufferInfo(conout, &info) == 0) |
|||
return 1; |
|||
/* c = character at position. */ |
|||
pos.X = info.srWindow.Left + 3; /* Column */ |
|||
pos.Y = info.srWindow.Top + 6; /* Row */ |
|||
if (ReadConsoleOutputCharacterW(conout, &c, 1, pos, &len) == 0 || |
|||
len <= 0) |
|||
return 1; |
|||
wprintf(L"Character at (3, 6) had been '%lc'\n", c); |
|||
return 0; |
|||
}</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
Revision as of 16:02, 23 August 2011
Determine the character displayed on the screen at column 3, row 6 and store that character in a variable.
BASIC
Locomotive Basic
<lang locobasic>10 LOCATE 3,6 20 a$=COPYCHR$(#0)</lang>
Amstrad CPC screen memory only stores pixels but no character information (as opposed to e.g. the C64), so the firmware routine (TXT_UNWRITE) called by BASIC works by trying to find a match between screen pixels and the shape of a currently defined character. If the character table or screen pixels in the area of the character are changed between writing and reading, COPYCHR$ will therefore fail.
ZX Spectrum Basic
<lang basic> 10 REM The top left corner is at position 0,0
20 REM So we subtract one from the coordinates 30 LET c$ = SCREEN$(5,2)</lang>
C
With the Windows console, call GetConsoleScreenBufferInfo()
to find the top-left corner of the display screen. Then add (3, 6) to the top-left corner and call ReadConsoleOutputCharacterW()
to read character. This program reckons that the top-left corner is (0, 0).
<lang c>#include <windows.h>
- include <wchar.h>
int main() { CONSOLE_SCREEN_BUFFER_INFO info; COORD pos; HANDLE conout; long len; wchar_t c;
/* Create a handle to the console screen. */ conout = CreateFileW(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (conout == INVALID_HANDLE_VALUE) return 1;
/* Where is the display window? */ if (GetConsoleScreenBufferInfo(conout, &info) == 0) return 1;
/* c = character at position. */ pos.X = info.srWindow.Left + 3; /* Column */ pos.Y = info.srWindow.Top + 6; /* Row */ if (ReadConsoleOutputCharacterW(conout, &c, 1, pos, &len) == 0 || len <= 0) return 1;
wprintf(L"Character at (3, 6) had been '%lc'\n", c); return 0; }</lang>
REXX
The REXX doesn't have any cursor or screen management tools, but some REXX interpreters have added the functionality via different methods.
<lang rexx>/*REXX program demonstrates reading a char at specific screen location.*/
row=20 /*point to row twenty. */ col=55 /*point co column fifty-five. */ howMany=3 /*read a trio of characters. */
stuff=scrread(row,col,howMany) /*this'll do it. */
other=scrRead(40,55,2) /*same thing, but for row forty. */</lang>