crtdemo.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. {************************************************}
  2. { }
  3. { CRT Unit Demo }
  4. { Copyright (c) 1985,90 by Borland International }
  5. { }
  6. {************************************************}
  7. program CrtDemo;
  8. { Example program that uses the Crt unit. Uses the following routines
  9. from the Crt unit:
  10. ClrScr
  11. DelLine
  12. GoToXY
  13. InsLine
  14. KeyPressed
  15. ReadKey
  16. TextBackground
  17. TextColor
  18. TextMode
  19. WhereX
  20. WhereY
  21. Window
  22. Write
  23. WriteLn;
  24. Also uses LastMode and WindMax variables from Crt unit.
  25. 1. Init routine:
  26. - Save original video mode. On an EGA or VGA, use the 8x8 font
  27. (43 lines on an EGA, 50 on VGA).
  28. - Setup LastRow to preserve last line on screen for messages
  29. (preserves last 2 lines in 40-column mode). Setup LastCol.
  30. - Initialize the random number generator.
  31. 2. MakeWindow routine:
  32. - Puts up random-sized, random-colored windows on screen.
  33. 3. Program body:
  34. - Call Init
  35. - Loop until Contrl-C is typed:
  36. - Echo keystrokes (Turbo Pascal windows automatically wrap
  37. and scroll).
  38. - Support special keys:
  39. <Ins> inserts a line at the cursor
  40. <Del> deletes a line at the cursor
  41. <Up>,
  42. <Dn>,
  43. <Right>,
  44. <Left> position the cursor in the window
  45. <Alt-R> generate random text until a key is pressed
  46. <Alt-W> creates another random window
  47. <ESC> exits the program
  48. }
  49. uses Crt;
  50. var
  51. OrigMode,LastCol,LastRow: Word;
  52. Ch: Char;
  53. Done: Boolean;
  54. procedure Initialize;
  55. { Initialize the video mode, LastCol, LastRow, and the random number }
  56. { generator. Paint the help line. }
  57. begin
  58. OrigMode:=LastMode; { Remember original video mode }
  59. TextMode(_80cols+_50rows); { use 43 or 50 lines on EGA/VGA }
  60. LastCol:=Lo(WindMax)+1; { get last column, row }
  61. LastRow:=Hi(WindMax)+1;
  62. GoToXY(1,LastRow); { put message line on screen }
  63. TextBackground(Black);
  64. TextColor(White);
  65. Write(' Ins-InsLine '+
  66. 'Del-DelLine '+
  67. #27#24#25#26'-Cursor '+
  68. 'Alt-W-Window '+
  69. 'Alt-R-Random '+
  70. 'Esc-Exit');
  71. LastRow:=lastrow-80 div LastCol; { don't write on message line }
  72. Randomize; { init random number generator }
  73. end; { Init }
  74. procedure MakeWindow;
  75. { Make a random window, with random background and foreground colors }
  76. var
  77. X,Y,Width,Height: Word;
  78. begin
  79. Width:=Random(LastCol-2)+2; { random window size }
  80. Height:=Random(LastRow-2)+2;
  81. X:=Random(LastCol-Width)+1; { random position on screen }
  82. Y:=Random(LastRow-Height)+1;
  83. Window(X,Y,X+Width,Y+Height);
  84. TextBackground(Random(8));
  85. TextColor(Random(7)+9);
  86. ClrScr;
  87. end; { MakeWindow }
  88. procedure RandomText;
  89. { Generate random text until a key is pressed. Filter out }
  90. { control characters. }
  91. begin
  92. repeat
  93. Write(Chr(Random(256-32)+32));
  94. until KeyPressed;
  95. end; { RandomText }
  96. begin { program body }
  97. Initialize;
  98. MakeWindow;
  99. Done:=False;
  100. repeat
  101. Ch:=ReadKey;
  102. case Ch of
  103. #0: { Function keys }
  104. begin
  105. Ch:=ReadKey;
  106. case Ch of
  107. #17: MakeWindow; { Alt-W }
  108. #19: RandomText; { Alt-R }
  109. #45: Done:=True; { Alt-X }
  110. #72: GotoXY(WhereX,WhereY-1); { Up }
  111. #75: GotoXY(WhereX-1,WhereY); { Left }
  112. #77: GotoXY(WhereX+1,WhereY); { Right }
  113. #80: GotoXY(WhereX,WhereY+1); { Down }
  114. #82: InsLine; { Ins }
  115. #83: DelLine; { Del }
  116. end;
  117. end;
  118. #3: Done:=True; { Ctrl-C }
  119. #13: WriteLn; { Enter }
  120. #27: Done:=True; { Esc }
  121. else
  122. Write(Ch);
  123. end;
  124. until Done;
  125. TextMode(OrigMode);
  126. end.