Mega Code Archive

 
Categories / Delphi / Examples
 

How to use the old (Win)DOS routines in win32 console applications

Title: How to use the old (Win)DOS routines in win32 console applications Question: Have you ever tried to use the gotoxy or keypress function in your win32 applications ? Answer: Unit Win32_Dos; Interface {code used to emulate old dos calls} Function GetKey: char; Procedure GotoXY (X, Y: integer); Function WhereX: integer; Function WhereY: integer; Procedure ClrScr; Procedure SelectCursor (CurOn: boolean); Procedure ClearLine; Function KeyWaiting: boolean; Implementation Uses Windows; Var KeyData: integer = -1; Function KeyWaiting: boolean; Var InputEvents: DWORD; InputEventsRead: DWORD; KeyBuf: TInputRecord; Begin If KeyData Begin GetNumberOfConsoleInputEvents (GetStdHandle (STD_INPUT_HANDLE), InputEvents); If InputEvents 0 Then Begin ReadConsoleInput (GetStdHandle (STD_INPUT_HANDLE), KeyBuf, 1, InputEventsRead); If (KeyBuf.EventType = KEY_EVENT) And (KeyBuf.Event.KeyEvent.bKeyDown) Then Begin KeyData := integer (KeyBuf.Event.KeyEvent.AsciiChar); End; End; End; KeyWaiting := KeyData = 0; End; Function GetKey: char; Begin If KeyData While Not (KeyWaiting) Do {nop} ; If KeyData = 0 Then getkey := char (KeyData) Else getkey := #255; KeyData := -1; End; Procedure GotoXY (X, Y: integer); Var Cord: tCoord; Begin Cord.X := x; Cord.Y := y; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), Cord); End; Function WhereX: integer; Var Cord: tCoord; Info: tConsoleScreenBufferInfo; Begin GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); result := Info.dwCursorPosition.X; End; Function WhereY: integer; Var Cord: tCoord; Info: tConsoleScreenBufferInfo; Begin GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); result := Info.dwCursorPosition.Y; End; Procedure ClrScr; Var Cord: tCoord; CharactersWritten: Cardinal; Info: tConsoleScreenBufferInfo; Size: Cardinal; Begin Cord.X := 0; Cord.Y := 0; GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); Size := succ (Info.dwSize.X) * succ (Info.dwSize.Y); FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE), ' ', Size, Cord, CharactersWritten); End; Procedure SelectCursor (CurOn: boolean); Var Info: tConsoleCursorInfo; Begin GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); If CurOn Then Info.bVisible := true Else Info.bVisible := false; SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); End; Procedure ClearLine; Var i: integer; Info: tConsoleScreenBufferInfo; Begin SelectCursor (FALSE); GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), Info); GotoXY (0, WhereY); For i := 0 To Info.dwSize.X - 2 Do Write (' '); GotoXY (0, WhereY); SelectCursor (TRUE); End; End.