Code:
// _project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include "conv.h"
#define screen_size 26
#define DL 6
#define COL 35
#define ROW 21
using namespace std;
enum key_codes {UP=80, DOWN=72,F9=67,F10=68};
void window(int x,int y,int dx,int dy)
{
int i;
gotoxy(x,y);
for (i=x+1;i<dx;i++){
gotoxy(i,y);
cout<<'-' ;
gotoxy(i,dy);
cout<<'-';
}
for ( i=y;i<dy;i++){
gotoxy(x,i);
cout<<'|';
gotoxy(dx,i);
cout<<'|';
}
gotoxy (x,y);
cout<<'*';
gotoxy (x,dy);
cout<<'*';
gotoxy(dx,y);
cout<<'*';
gotoxy(dx,dy);
cout<<'*';
}
void clear()
{
wchar_t buff[5000];
for(int m=1;m<5000;m+=2) buff[m]=0;
puttext(2, 2, 33, 20,buff);
}
void highlight (int x1, int y1, int x2, int y2, char colour)
{
wchar_t s[320];
gettext(x1, y1, x2, y2, s);
for(int i = 1; i <160; i+=2)
s[i]=colour;
puttext(x1, y1, x2, y2, s);
}
void matr_part_out(char matr[][COL], int icur, int how_many)
{
int i,k,yyy=1;
for (i=icur; i<icur+how_many; i++)
{
gotoxy(1,yyy);
for(k=0;k<strlen(matr[i]);k++)
cout<<matr[i][k];
yyy++;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char matr[ROW][COL];
int count_str=0;
int work_zona=screen_size-DL;
int how_many=screen_size-DL;
int icur=0;
int cur_rest=0;
int x=0, y=0, key = 1;
int xn=35;
int x0=1;
int yn=20;
int y0=1;
int ynn=20;
int yn0=1;
ifstream inFil;
inFil.open("pro.txt",ios::in);
if (!inFil)
{
cout << "Canot open file : pro.txt";
_getch();
return 1;
}
string textline;
while (getline(inFil, textline, '\n'))
{
strcpy (matr[count_str],textline.c_str());
count_str++;
}
matr_part_out(matr, icur, how_many);
window(0,0,35,21);
window(37,0,72,21);
gotoxy(x,y);
while (key!=27)
{
key=_getch();
if (key==0||key==224)
{
key=_getch();
switch (key)
{
case UP:
if (y>y0)
gotoxy(x,--y);
else
if(y==yn0)
{
cur_rest=icur;
if (cur_rest>1)
{
how_many=work_zona;
icur--;
}
else
{
how_many=work_zona;
icur=0;
}
clear();
matr_part_out(matr, icur, how_many);
}
highlight (2,y+1,33,y+1,10);
highlight (2,y+2,33,y+2,7);
continue;
case DOWN:
if (y<yn)
gotoxy(x,++y);
else
if (y==ynn)
{
cur_rest=count_str-icur;
if (cur_rest<=work_zona && cur_rest>1)
{
how_many=count_str-icur;
}
else
if (cur_rest>work_zona)
{
how_many=work_zona;
icur++;
}
clear();
matr_part_out(matr, icur, how_many);
}
highlight (2,y+1,33,y+1,10);
highlight (2,y,33,y,7);
continue;
default: continue;
}
}
}
return 0;
}
Xem giúp mình phần case up và case down sai chỗ nào nhé vì khi mình ấn phím up thì di chuyển con trỏ đi xuống còn ấn phím down thì di chuyển con trỏ đi lên.