You are on page 1of 14

Chng 7.

c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

Chng 7

c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit


Mch ch ca chng S dng lp lm vic vi tp M v ng tp Nhng hm x l lin quan n tp Mt s hm trong DIR.H Mt s hm c/Ghi cng Lp trnh cho chut Phng ch ting Vit

7.1 S dng lp lm vic vi tp


H thng I/O ca C++ c cc lp ifstream, ofstream, fstream v filebuf cung cp cc hm x l tp. Nhng lp c k tha t fstreambase v nhng lp tng ng trong FSTREAM.H. ios istream ostream iostream fstreambase streambuf filebuf

ifstream fstream

ofstream

Cc lp trong fstream.h filebuf: L lp cc vng m (buffer) c/ghi tp, close() v open() l hai hm thnh phn. fstreambase: l lp c s ca ifstream, ofstream v fstream . ifstream: Gm cc hm lin quan n input, open() vi mode input mc nh. K tha cc hm get(), getline(), read(), seekg(), tellg() t lp istream. ofstream: Gm cc hm lin quan n output, open() vi mode output mc nh. K tha cc hm put(), write(), seekp(), tellp() t lp ostream. fstream: H tr cho vic thc hin ng thi input v output. K tha tt c t ifstream v ofstream.

7.2 M v ng tp
M tp bng cu t: ofstream <OutFile>(<FileName> [, <mode>]); //Ch ghi ifstream <InFile>(<FileName> [, <mode>]); //Ch c fstream <File>(<FileName> [, <mode>]); //c/Ghi Trong , <mode> c th nhn cc gi tr sau (c th kt hp): Mode Meaning ios::app Ghi b sung vo cui tp ios::ate Sau khi m, chuyn con tr tp v cui tp ios::binary Tp nh phn ios::in c ios::out ghi ios::nocreate Khng m nu tp cha c ios::noreplate Khng m nu tp c ios::trunc Xo ni dung tp nu c V d: ofstream F(Thu.bak, ios::trunc | ios::binary); ifstream G(Thu.pas, ios::nocreate | ios::binary); fstream H(Thu.dat, ios::in | ios::out | ios::binary);
Created by On Ngu Minh Trang 53

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

Ch : Khi phi ch ra tn tp y th dng hai du \\ (hoc mt du /) thay cho mt du \. V d C:\\BC\\BGI hoc C:/BC/BGI. Khi dng cu t, mi bin tp ch c th gn vi mt tp duy nht. Tuy nhin, mt tp vn c th c gn cho hai bin tp. Hn na, vic ng tp l t ng khi ht phm vi ca bin tp, ta khng cn gi phng thc close() ng, loi tr ta ch ng ng n. V d: Kt qu: #include <fstream.h> Line number:0 #include <conio.h> Line number:1 int main() Line number:2 { char s[20]; Line number:3 ofstream outf("Thu.txt"); ifstream inf("Thu.txt"); for (int k=0; k<4; k++) outf <<"Line number:"<<k<<endl; for (k=0; k<4; k++) { inf.getline(s, 20); cout << s<<endl;} Kt qu: getch(); return 0; Line } Ch rng, nu thay lnh inf.getline(s, 20) bi lnh inf >> s, th trn mn number:0 Line hnh s nh hnh di, v ton t >> khng c nhiu t mt lc. number:1 M tp bng phng thc open() Nu mun s dng mt bin tp lm vic vi nhiu tp, ta dng phng thc open() m tp, vic ng tp do ngi dng ch ng. C php s dng open() nh sau: <stream> <File>; <File>.open(<FileName> [, <mode>]); Gi tr ca <mode> cng ging nh dng vi cu t. V d sau y ch s dng mt bin tp outf m 5 tp v ghi vo mi tp chnh tn ca tp : #include <fstream.h> #include <stdlib.h> //S dng hm char *itoa(int value, char *string, int radix) #include <string.h> //S dng hm strcat() #include <conio.h> int main() { char s[6]; ofstream outf; for (int k=0; k<5;k++) { itoa(k, s, 10); strcat(s,".txt"); outf.open(s); outf <<s<<endl; outf.close(); } return 0; }

7.3 Nhng hm x l lin quan n tp


output. long istream::tellg (member function): Tr li v tr hin thi ca con tr vi tp input. long ostream::tellp (member function): Tr li v tr hin thi ca con tr vi tp void istream & seekg(long pos): Chuyn con tr ti v tr pos vi tp input. void ostream & seekp(long pos): Chuyn con tr ti v tr pos vi tp output. void istream & seekg(long pos, enum seek_dir): Chuyn con tr theo a ch tng

i. Trong , seek_dir nhn mt trong cc gi tr: ios::beg: Tnh t u tp ios::cur: Tnh t v tr hin thi ios::end: Tnh t cui tp. Nu pos dng, chiu di chuyn l hng ti cui tp, nu pos m, hng v u tp. seekp(long pos, enum seek_dir): Ging nh seekg(long pos, enum seek_dir), nhng vi cc tp output. put(char): Ghi mt byte vo tp. get(char): c mt byte t tp. write(const char * Buf, int n): Ghi mt khi n byte t vng m Buf. V d, mun ghi ton b d liu trong vng m Buf vo tp c ch nh bi bin tp outf, ta dng lnh:
Created by On Ngu Minh Trang 54

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

outf.write((char *) & Buf, sizeof(Buf)); Trong , (char *) dng i kiu, v vng m Buf thng l mng hoc bn ghi, bn ghi. Hm sizeof() cho kch thc (theo byte) ca vng m. read(char * Buf, int n): c mt khi n byte vo vng m Buf. V d, mun c d liu t tp c ch nh bi bin tp outf vo vng m Buf, ta dng lnh: inf.read((char *) & Buf, sizeof(Buf)); S byte thc t c c t tp c tr v bi hm gcount(), thng qua li gi inf.gcount(). eof(): Tr li gi tr khc 0 (True) nu con tr n cui tp, bng 0 nu cn trong tp. fail():Tr li gi tr khc 0 (True) nu khng thc hin c input/output. bad():Tr li gi tr khc 0 (True) nu thc hin mt thao tc khng hp l hoc c li m cha pht hin c. Tr li 0 (False) khi pht hin li thng bo v tip tc thc hin. good():Tr li gi tr khc 0 (True) nu khng li, tc l cc hm eof(), fail() v bad() u bng 0. Sau y l v d: #include <iomanip.h> #include <fstream.h> #include <string.h> #include <conio.h> struct SV{ char Ten[20]; float Toan, Tin;}; void TaoTep(char *TenTep) { ofstream f(TenTep); int i=0; char ch; SV Buf; if (!f) cout << "Khong mo duoc" << endl; else do { cout << setw(2)<<++i<<".Ho ten (ENTER - Ket thuc): "; cin.getline(Buf.Ten,20); if (*Buf.Ten) { cout << "Diem toan: "; cin >> Buf.Toan; cout << " Diem tin: "; cin >> Buf.Tin; cin.get(ch); //Loi b phm ENTER khi vng m bn phm f.write((char *) &Buf, sizeof(SV)); } } while (*Buf.Ten); } void SuaTep(char *TenTep) { fstream f(TenTep,ios::out | ios::in); if (!f) cout << "Khong mo duoc" <<endl; else{ char ch; SV Buf, Tam; long pos; int Found=0; do { cout << " Ho ten can tim (ENTER - Ket thuc): "; cin.getline(Tam.Ten,20); if (*Tam.Ten) { Found=0; f.seekg(0,ios::beg); while (f && !Found) { pos=f.tellg(); f.read((char *) &Buf, sizeof(SV)); if (f.gcount() && !stricmp(Buf.Ten,Tam.Ten)) { Found=1; cout << "Diem toan cu: "<< Buf.Toan <<" Diem toan moi: "; cin >> Tam.Toan; cout << "Diem tin cu: "<< Buf.Toan <<" Diem tin moi: "; cin >> Tam.Tin;
Created by On Ngu Minh Trang 55

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

cin.get(ch); f.seekp(pos,ios::beg); f.write((char *) &Tam, sizeof(SV)); } } if (!Found) cout <<"Khong thay"<<endl; } } while (*Tam.Ten); } } void DocTep(char *TenTep) { ifstream f(TenTep, ios::in); char ch; SV Buf; int i=0; cout << setiosflags(ios::showpoint) << setprecision(2); if (!f) cout << "Khong mo duoc" << endl; else { cout <<"Danh sach la:" <<endl; while (f) { f.read((char *) &Buf, sizeof(SV)); if (f.gcount()) { cout << setw(2)<<++i<<"."<< Buf.Ten; cout << setw(sizeof(Buf.Ten) - strlen(Buf.Ten)); cout << Buf.Toan << setw(5) << Buf.Tin << endl; } } } } int main() { char s[80]; cout <<" Ten tep: "; cin.getline(s,13); TaoTep(s); SuaTep(s); DocTep(s); getch(); return 0; } 7.4 Mt s hm trong DIR.H int fnsplit(const char *path, char *drive, char *dir, char *name, char *ext); Tch xu biu th tn tp y (fullpath) thnh bn phn: drive: a, c c du hai chm. dir: Dy th mc cha tp, c du backslash c u v cui name: Tn tp (ngn gn). ext: Phn m rng. void fnmerge ( char *path, const char *drive, const char *dir, const char *name, const char *ext); Ghp cc xu drive, dir, name v ext thnh xu biu th tn tp y (fullpath)

V d v fnsplit() v fnmerge() #include <dir.h> #include <iostream.h> #include <conio.h> int main()
Created by On Ngu Minh Trang 56

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

char *s="C:\\Bc\\Cpp\\Example.exe"; char drive[MAXDRIVE]; //MAXDRIVE =3 char dir[MAXDIR]; //MAXDIR =66 char file[MAXFILE]; //MAXFILE =9 char ext[MAXEXT]; //MAXEXT =5 char path[MAXPATH]; //MAXPATH =80 int flags=fnsplit(s, drive, dir, file, ext); if(flags & DRIVE) cout <<"Drive: "<<drive<<endl; if(flags & DIRECTORY) cout <<"Directory: "<<dir<<endl; if(flags & FILENAME) cout <<"File: "<<file<<endl; if(flags & EXTENSION) cout <<"Extension:"<<ext<<endl; if(flags & WILDCARDS) cout <<"Wildcards:"<<ext<<endl; fnmerge(path, drive, dir, file, ext); cout <<"Full path: "<<path<<endl; getch(); return 0;

int getdisk(void): Ly a mc nh (0=A, 1=B, vv...) int setdisk(int drive): t a mc nh. Hm tr li s cc i a c th. #include <iostream.h> #include <dir.h> int main(void) { int maxdrives = setdisk(2); //t mc nh l C: int disk = getdisk() + 'A'; cout <<"The number of logical drives is "<< maxdrives<<endl; cout <<"The current drive is: "<< disk<<endl; return 0; } int getcurdir(int drive, char *directory): Ly th mc mc nh (khng c tn a u) ca ch nh drive. #include <dir.h> #include <iostream.h> #include <string.h> char *current_directory(char *path) { strcpy(path, "X:\\"); // fill string with form of response: X:\ path[0] = 'A' + getdisk(); // replace X with current drive letter getcurdir(0, path+3); // fill rest of string with current directory return(path); } int main(void) { char curdir[MAXPATH]; current_directory(curdir); cout <<"The current directory is: "<< curdir<<endl; return 0; } char *getcwd(char *buf, int buflen): Ly th mc mc nh, c c tn a. int findfirst(const char *pathname, struct ffblk *ffblk, int attrib); Tm tp u tin c ch ra bi pathname, c thuc tnh l attrib, thng tin v tp cha trong ffblk. int findnext(struct ffblk *ffblk): Tip tc tm kiu tp c tm bi findfirst() ffblk DOS file control block structure. struct ffblk { char ff_reserved[21]; // reserved by DOS char ff_attrib; // attribute found
Created by On Ngu Minh Trang 57

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

int ff_ftime; int ff_fdate; long ff_fsize; char ff_name[13]; };

// Bit: 0-4: Seconds/2, 5-10:Minutes, 11-15:Hours // Bit: 0-4: Day, 5-8: Month, 9-15:Years since 1980 // file size // found file name

V d v findfirst() v findnext() #include <iostream.h> #include <dir.h> int main(void) { struct ffblk f; char path[MAXPATH]; getcwd(path, MAXPATH); cout <<"Directory listing of *.CPP"<<endl; int done = findfirst("*.CPP",&f,0); while (!done) { cout << path<<'\\'<<f.ff_name<<endl; done = findnext(&f); } return 0; } char *searchpath(const char *file): Tm tp trong th mc mc nh v trong PATH #include <iostream.h> #include <dir.h> int main(void) { char *p; // Looks for TLINK and returns a pointer to the path p = searchpath("TLINK.EXE"); //Use PATH environment cout <<"Search for TLINK.EXE : " << p<<endl; // Looks for non-existent file p = searchpath("NOTEXIST.FIL"); cout <<"Search for NOTEXIST.FIL : " << p<<endl; //(null) return 0; } char *mktemp(char *template): To tn tp duy nht t xu template. #include <dir.h> #include <iostream.h> int main(void) { // fname defines the template for the temporary file. char *fname = "TXXXXXX", *ptr = mktemp(fname); cout <<ptr<<endl; //TAA.AAA cout <<fname<<endl; //TAA.AAA return 0; } int rmdir(const char *path): Xo th mc int mkdir(const char *path): To tth mc int chdir(const char *path): Thay i th mc mc nh int unlink(const char *filename): Xo tp. int remove(const char *filename): Xo tp (Macro gi unlink) int chmod(const char *path, int amode): Thay i thuc tnh tp

7.5 Mt s hm c/Ghi cng


Trong DOS.H unsigned inport(unsigned portid); Hm nhn gi tr c 2 byte t cng portid. Byte thp c t cng portid, byte cao t portid+1.
Created by On Ngu Minh Trang 58

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

unsigned char inportb (unsigned portid); Hm nhn gi tr c 1 byte t cng portid. void outport(unsigned portid, unsigned value): a value (2 byte) ra cng portid. Byte thp ra cng portid, byte cao ra cng portid+1. void outportb(unsigned portid, unsigned char value): a value (1 byte) ra cng portid. Trong CONIO.H unsigned inpw(unsigned portid): Hm nhn gi tr c 2 byte t cng portid. Byte thp c t cng portid, byte cao t portid+1. int inp(unsigned portid); Hm nhn gi tr c 1 byte t cng portid. unsigned outpw(unsigned portid, unsigned value) : a value (2 byte) ra cng portid. Byte thp ra cng portid, byte cao ra portid+1. Nu thnh cng th ng bng value. int outp(unsigned portid, int value) : a value (1 byte) ra cng portid. Hm nhn li gi tr a ra, nu thnh cng th ng bng value. Trong BIOS.H Truyn thng: int bioscom(int cmd, char abyte, int port); bioscom s dng ngt 14h ca BIOS truyn d liu qua cng ni tip. Cc tham s l: abyte Kt hp hoc (OR) cc bit ch nh cng truyn COM (b qua nu cmd = 2 hoc 3) cmd Xc nh tc v truyn. port Tn cng: 0 = COM1, 1 = COM2, vv... Gi tr tr v: S nguyn 16-bit. Tm bit cao (upper 8 bits) l cc bit trng thi, mt trong chng m bng 1 th tc v b li, tri li l thnh cng. Ngha ca tm bit cao l: Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Meaning Received line signal detect Ring indicator Data set ready Clear to send Change in receive line signal detector Trailing edge ring detector Change in data set ready Change in clear to send Data ready Overrun error Parity error Framing error Break detect Transmit holding register empty Transmit shift register empty Time out (set to 1 if abyte value could not be sent)

Tm bit thp (lower 8 bits) ph thuc gi tr ch nh ca cmd. Gi tr cmd l: 0 (_COM_INIT): Khi to gi tr abyte cho phin truyn 1 (_COM_SEND): Gi gi tr abyte ln ng truyn 2 (_COM_RECEIVE): Nhn gi tr trn ng truyn 3 (_COM_STATUS): Nhn trng thi cng truyn Khi cmd = 2 hoc 3, tham s abyte c b qua. Khi cmd=0, gi tr ca abyte c kt hp hoc (OR) t cc bit sau( mi nhm chn mt): 0x00 0x20 0x40 0x60 0x80 baud 110 baud 150 baud 300 baud 600 baud 1200 baud parity 0x00 No parity 0x08 Odd parity 0x18 Even parity stop bit 0x00 1 stop bit 0x04 2 stop bits data bits 0x02 7 data bits 0x03 8 data bits

Created by On Ngu Minh

Trang 59

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

0xA0 2400 baud 0xC0 4800 baud 0xE0 9600 baud V d, nu abyte = 0xEB = (0xE0 | 0x08 | 0x00 | 0x03) th cng truyn c thit lp: 9600 baud (0xE0 ) Odd parity(0x08 ) 1 stop bit (0x00 ) 8 data bits (0x03 ) Sau y l v d minh ho /*bioscom.cpp*/ #include <iostream.h> #include <bios.h> #include <conio.h> #define COM1 0 #define DATA_READY 0x100 #define TRUE 1 #define FALSE 0 #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00) int main(void) { int in, out, status, DONE = FALSE; bioscom(0, SETTINGS, COM1); cout<<"... BIOSCOM [ESC] to exit ..."<<endl; while (!DONE) { status = bioscom(3, 0, COM1); if (status & DATA_READY) if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) putch(out); if (kbhit()) { if ((in = getch()) == '\x1B') DONE = TRUE; bioscom(1, in, COM1); } } return 0; } iu khin my in: int biosprint(int cmd, int abyte, int port); biosprint(): s dng ngt 17h ca BIOS thc hin chc nng in theo cng ch nh. abyte: Gi tr cn in, trong khong t 0 n 255. cmd: Ch nh chc nng 0 (_PRINTER_WRITE): In k t abyte 1 (_PRINTER_INIT): Khi to cng my in 2 (_PRINTER_STATUS): c trng thi my in. Nu cmd = 1 hoc 2, abyte c b qua port: Ch nh cng my in (0 = LPT1, 1 = LPT2, vv...) Hm tr li trng thi hin ti ca my in, phn bit bi cc bit nh sau: Bit Gi tr Trng thi 0 0x01 Device time out 3 0x08 I/O error 4 0x10 Selected 5 0x20 Out of paper 6 0x40 Acknowledge 7 0x80 Not busy Di y l chng trnh minh ho /*BiosPRN.cpp*/ #include <iomanip.h> #include <stdio.h> #include <conio.h> #include <bios.h> int main(void)
Created by On Ngu Minh Trang 60

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

{ cout<<setiosflags(ios::hex)<<setiosflags(ios::uppercase); #define STATUS 2 /* printer status command */ #define PORTNUM 0 /* port number for LPT1 */ int status, abyte=0; cout <<"Please turn off your printer. Press any key to continue\n"; getch(); status = biosprint(STATUS, abyte, PORTNUM); cout <<status<<endl; if (status & 0x01) cout <<"Device time out.\n"; if (status & 0x08) cout <<"I/O error.\n"; if (status & 0x10) cout <<"Selected.\n"; if (status & 0x20) cout <<"Out of paper.\n"; if (status & 0x40) cout <<"Acknowledge.\n"; if (status & 0x80) cout <<"Not busy.\n"; return 0; } Bt phm: int bioskey(int cmd); bioskey(): S dng ngt 16h ca BIOS giao tc vi bn phm. Tham s cmd ch nh thao tc. Ngha ca gi tr tr li ca hm ph thuc vo cmd. Gi tr ca cmd nh sau: 0 (_KEYBRD_READ): Nhn k t trong vng m bn phm. Nu tm bit thp ca bioskey khc khng, gi tr ca phm bm tng ng vi k t trong bng ASCII. 1 (_KEYBRD_READY): Nu bioskey bng 0, cha c phm no ang ch, tri li, bng gi tr ca phm trong ln gi ngay sau ca hm vi cmd=0. 2(_KEYBRD_SHIFTSTATUS): Tr li trng thi cc phm, theo cc OR bit sau: 0 0x01 Right Shift pressed 1 0x02 Left Shift pressed 2 0x04 Ctrl pressed 3 0x08 Alt pressed 4 0x10 Scroll Lock on 5 0x20 Num Lock on 6 0x40 Caps on 7 0x80 Insert on Sau y l chng trnh minh ho: /*BIOSKey.cpp*/ #include <iomanip.h> #include <bios.h> #include <ctype.h> #define ShiftRight 0x01 #define ShiftLeft 0x02 #define Ctrl 0x04 #define Alt 0x08 #define Scroll 0x10 #define NumLock 0x20 #define Caps 0x40 #define Insert 0x80 int main(void) { cout <<setiosflags(ios::hex)<<setiosflags(ios::uppercase); unsigned int key, modifiers; do{ while (bioskey(1) == 0); /* function 1 returns 0 until a key is pressed */ key = bioskey(0);/* function 0 returns the key that is waiting */ modifiers = bioskey(2); /* use function 2 to determine if shift keys were used */ if (modifiers) { cout <<"["; if (modifiers & ShiftRight) cout <<"Right Shift. ";
Created by On Ngu Minh Trang 61

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

if (modifiers & ShiftLeft) if (modifiers & Ctrl) if (modifiers & Alt) if (modifiers & Scroll) if (modifiers & NumLock) if (modifiers & Caps) if (modifiers & Insert) cout <<"] ";

cout <<"Left Shift. "; cout <<"Ctrl. "; cout <<"Alt. "; cout <<"Scroll lock on. "; cout <<"Num Lock on. "; cout <<"Caps Lock on. "; cout <<"Insert on. ";

} /* print out the character read */ if( key & 0xFF) cout <<"0x"<< (key & 0xFF); else cout <<"0x"<< (key >>8); if (isalnum(key & 0xFF)) cout <<(<<(char)key<<)<<endl; else cout <<endl; } while ((key & 0xFF) !=0x1B); //ESC return 0; } Sau y l cc hm lin quan (u c mt tham s int c): int isalnum: c l ch ci v ch s (0..9, A .. Z v a .. z) int isalpha: c l ch ci (A .. Z v a .. z). int isascii: c thuc na thp ca bng ASCII (0x00 .. 0x7F) int iscntrl: c l cc k t iu khin (0x7F, 0x00 .. 0x1F) int isdigit: c l ch s (0 to 9) int isgraph: c l cc k t in c, ging nh isprint, loi tr k t trng (space). int islower: c l cc ch ci thng (a .. z) int isprint: c l cc k t in c (0x20 to 0x7E) int ispunct: c l cc k t du (punctuation character) int isspace: c l space, tab, carriage return, new line, vv...(0x09 .. 0x0D, 0x20) int isupper: c l cc ch ci hoa (A .. Z) int isxdigit: c l ch s hexa (0 .. 9, A .. F, a .. f) 7.6 Lp trnh cho chut #include <dos.h> #include <math.h> #include <conio.h> #include <graphics.h> int PM; // Bin chung void AskMouse() { struct REGPACK reg; reg.r_ax = 0; intr(0x33,&reg); PM=(reg.r_ax=0 ? 0:1); } void ShowMouse() { struct REGPACK reg; if (PM) { reg.r_ax=1; intr(0x33,&reg);} } int LeftMouse(int &x,int &y) { struct REGPACK reg; if (PM) { reg.r_ax=3; intr(0x33,&reg); if ((reg.r_bx & 1) ==1) { x=reg.r_cx ; y=reg.r_dx; return 1; } else return 0; } else return 0; } int RightMouse(int &x,int &y) { struct REGPACK reg; if (PM) { reg.r_ax=3; intr(0x33,&reg); if ((reg.r_bx & 2) ==2) { x=reg.r_cx; y=reg.r_dx; return 1; } else return 0; } else return 0; } int Events() { struct REGPACK reg; int x,y;
Created by On Ngu Minh Trang 62

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

if ( kbhit() || LeftMouse(x,y) || RightMouse(x,y)) return 1; else return 0; } int ExitEvent() { int ch, x,y; if (kbhit()) { ch=getch(); if (ch==27) return 1; else return 0;} else { if (RightMouse(x,y)) return 1; else return 0;} } void HiddenMouse() { struct REGPACK reg; if (PM==1) { reg.r_ax=2; intr(0x33,&reg); } } void GetMousePos(int &x,int &y) { struct REGPACK reg; if (PM) { reg.r_ax=3; intr(0x33,&reg); x=reg.r_cx ; y=reg.r_dx;} } int MouseButton(int &x, int &y) { return LeftMouse(x,y) || RightMouse(x,y);} void SetMousePos(int x,int y) { struct REGPACK reg; if (PM){ reg.r_ax=4; reg.r_cx=x; reg.r_dx=y;intr(0x33,&reg);} } void SetMouseRowPos(int top,int bottom) { struct REGPACK reg; if (PM){ reg.r_ax=8;reg.r_cx=top; reg.r_dx=bottom; intr(0x33,&reg);} } void SetMouseColPos(int left,int right) { struct REGPACK reg; if (PM){ reg.r_ax=7; reg.r_cx=left; reg.r_dx=right; intr(0x33,&reg);} } int MouseMove(int &x,int &y) { struct REGPACK reg; if (PM){ reg.r_ax=0xB; intr(0x33,&reg); x=reg.r_cx ; y=reg.r_dx ; return (x!=0 ||y!=0) ? 1:0; } else return 0; } void WinHiddenMouse(int left,int top,int right,int bottom) { struct REGPACK reg; if (PM) { reg.r_ax=0x10; reg.r_cx=left ; reg.r_dx=top; reg.r_si=right ; reg.r_di=bottom; intr(0x33,&reg); } } int LDownButton(int &x,int &y) { struct REGPACK reg; if (PM) { reg.r_ax=6; reg.r_cx=0; intr(0x33,&reg); if ((reg.r_ax & 1) ==1) { x=reg.r_cx ; y=reg.r_dx ; return 1; } else return 0; } else return 0; } void FormMouse(unsigned int tips,unsigned int FramAnd,unsigned int FramXor) { struct REGPACK reg; if (PM){ reg.r_ax=0x0A; reg.r_bx=tips; reg.r_cx=FramAnd; reg.r_dx=FramXor; intr(0x33,&reg); } } void MoveMouseTo(int x,int y) { int xp,yp,dx,dy,i=1,M; if (PM)
Created by On Ngu Minh Trang 63

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

{ GetMousePos(xp,yp); dx=x-xp;dy=y-yp; if ((dx!=0)||(dy!=0)) { M= (abs(dx) > abs(dy) ? abs(dx) : abs(dy)); while (Events()!=1 && i<M ) { HiddenMouse(); putpixel(xp+(float) i*dx/M,yp+ (float) i*dy/M,11); SetMousePos(xp+(float) i*dx/M,yp+ (float) i*dy/M); ShowMouse(); delay(5); i++; } } } } 7.7 Phng ch ting Vit Trong ch vn bn Ta phi nh ngha li b k t trong mt mng, mi k t ng vi 16 byte (nu l 16x8 chm). V d, ta cn nh ngha li 128 k t, khi mng MyFont c dng: const unsigned char MyFont[128*16] ={ 0x00,0x10,0x38,0x6C,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x18,0x00,0x00, ... 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 00 Gi tr ca mi byte xc nh chm sng ca mi dng trong ma trn 10 k t (xem hnh bn, k t ng vi 16 byte u tin trong mng). 38 6C Sau , s dng SubFn10h, Fn12h ca int10h np b k t ny vo 00 RAM (vng dnh cho font). Thanh ghi BH xc nh chiu cao ca k t, cn 7C C6 BL chn b font (0=font mc nh). CX ch nh s k t cn np, DX ch C6 C6 nh k t u tin trong b font, cn cp ES:BP tr ti a ch mng font ca C6 ngi dng (mang MyFont). Khi cn ti li b font ca ROM trong card mn C6 7C hnh, s dng SubFn04h, Fn11h cuat int10h. Di y l hai th tc c ci 00 t bng C++: 18 00 Np b phng c nh ngha bi mng MyFont: 00 void LoadTextFont() { REGPACK reg; // struct REGPACK trong dos.h reg.r_ax=0x1110; //AH=0x11 (Fn11h), AL=0x10 (SubFn10h) reg.r_bx=0x1000; //BH=16 (chiu cao k t), BL=0 (b font mc nh) reg.r_cx=0x80; //CX=128 (S k t cn np) reg.r_dx=0x80; //DX=128 (Bt u t k t 126, na cao ca b font) reg.r_bp = FP_OFF(MyFont); //BP=Offset ca MyFont reg.r_es = FP_SEG(MyFont); //ES=Segment ca MyFont intr(0x10, &reg); //Gi int10h bng hm intr(int, REGPACK &) } Np font c ca ROM void UnLoad() { REGS reg; // struct REGS trong dos.h reg.h.ah=0x11; //AH=0x11 (Fn11h) reg.h.al=0x04; //AL=0x04 (SubFn04h) reg.h.bl=0; //BL=0 (b font mc nh) int86(0x10, &reg, &reg); //Gi int10h bng hm int86(int, REGS &, REGS &) } Nu b phng mi khng c g khc ngoi vic mun b sung cc k t trong ting Vit, nn s dng b phng ca ROM trong card mn hnh lm mu. S dng SubFn30h, Fn11h ca int10h ly a ch ca bng font, s hiu ca bng font cn ly c t trong thanh ghi BH (BH=3: font 8x8, BH=6: font 16x8). Sau khi gi ngt, a ch ca bng font c tr bi cp ES:BP. Ta ch vic c vng nh , i tng byte ra dng hexa v ghi vo tp vn bn tin sa i. Di y l chng trnh minh ho ly font 16x8. Trong chng trnh c s dng hm:
Created by On Ngu Minh Trang 64

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

void movedata(unsigned srcseg, unsigned srcoff,unsigned destseg,unsigned destoff, size_t n); Hm ny sao chp n byte t vng nh c a ch srcseg:srcoff sang vng nh c a ch destseg:destoff. Chng trnh ny to ra tp header Font16.h, u tp c dng khai bo unsigned char MyFont[256*16]={, mi dng trong 256 dng tip theo gm 16 byte dng hexa, gia mi byte c du phy ngn cch, cui mi dng c hai du // v k t tng ng ca dng tin theo di (loi tr ba dng ng vi cc k t th 10, 13 v 26). Cui dng cui cng l du }. /* GetFont.cpp */ #include <iomanip.h> #include <fstream.h> #include <dos.h> #define n 16 //Thay 16 bi 8 v reg.r_bx=0x0300 nu ly font 8x8 int main(void) { unsigned char MyFont[256*n]; REGPACK reg; reg.r_ax=0x1130; reg.r_bx=0x0600; intr(0x10,&reg); //Ly a ch font 16x8 movedata(reg.r_es,reg.r_bp, FP_SEG(MyFont), FP_OFF(MyFont),256*n); ofstream f("Font16.h"); f <<"unsigned char MyFont[256*"<<n<<"]={"<<endl; f<<setiosflags(ios::hex)<<setiosflags(ios::uppercase); for (int k=0;k<256;k++) { for (int j=0;j<n;j++) { f << setw(3) << "0x"; if (MyFont[k*n+j]<16) f << 0; f << (int) MyFont[k*n+j]; //Chuyn t char sang int if (k==255 && j==n-1) f << "};"; else f << ,; } if( k!=10&&k!=13 && k!=26) f<<setw(5)<<"// "<<(char) k; f << endl; } return 0; } Trong v d trn, ta sao chp 256*16 byte t vng ES:BP sang vng MyFont ri mi c tng byte ca MyFont ghi vo tp Font16.h. Tuy nhin, ta c th ghi trc tip t vng ES:BP vo tp Font16.h. Mun vy, ta phi cho mt con tr PFont kiu unsigned char far tr ti vng ES:BP bng cch gi hm void far *MK_FP(unsigned seg, unsigned ofs); Chng trnh sau thc hin theo hng v c s dng tham s dng lnh, cc tp c sinh ra l Font16.h hay Font08.h tu thuc tham s bng 6 hay 3. /*GetFont2.cpp*/ #include <iomanip.h> #include <string.h> #include <fstream.h> #include <dos.h> int main(int argc, char *arg[]) { if(argc==2 && (*arg[1]==3 || *arg[1]==6)) { int m=(*arg[1]-48), n=m/3*8; char FontFile[10]= "Font"; REGPACK reg; reg.r_ax=0x1130; reg.r_bx=(m<<8); intr(0x10,&reg); unsigned char far *PFont=(unsigned char far *) MK_FP(reg.r_es,reg.r_bp); if (m==6) strcat(FontFile, "16.h"); else strcat(FontFile, "08,h"); ofstream f(FontFile); f <<"unsigned char MyFont[256*"<<n<<"]={"<<endl; f<<setiosflags(ios::hex)<<setiosflags(ios::uppercase); for (int k=0;k<256;k++) { for (int j=0;j<n;j++) { f << setw(2) << "0x"; if (*(PFont+k*n+j)<16) f << 0;
Created by On Ngu Minh Trang 65

Chng 7. c/ghi tp, c/ghi cng, lp trnh mouse, font ting vit

f << (int) *(PFont+k*n+j); if (k==255 && j==n-1) f << "};"; else f << ,; } if( k!=10&&k!=13 && k!=26) f<<setw(5)<<"// "<<(char) k; else f<<setw(5)<<"// "<< k; f << endl; } } else cout <<"Syntax: "<<arg[0]<<" [6] [3]"<<endl; return 0; } Ngoi ra, ta c th s dng cc hm sau y: unsigned FP_OFF(void far *p): Nhn hoc t offset cho con tr far. Nhn: unsigned ofs= FP_OFF(p). t: FP_OFF(p)=ofs; unsigned FP_SEG(void far *p): Nhn hoc t segment cho con tr far. Nhn: unsigned seg= FP_SEG(p). t: FP_SEG(p)=seg; Nh vy, cu lnh PFont=(unsigned char far *) MK_FP(reg.r_es,reg.r_bp) c thay bi hai lnh: FP_SEG(PFont)=reg.r_es; FP_OFF(PFont)=reg.r_bp; Trong ch ho ho trong C++ khng khc l my ho trong Pascal, ngoi vic mt s tn hng trong C++ thng c vit hoa. s dng ting Vit trong ho ca C++, ta phi nh ngha li cch thc a xu vn bn ra mn hnh. Mi k t c v ln bi cc im ng vi bit 1 trong ma trn k t ca n. V vy ta phi nh ngha b font ging nh trong ch vn bn. Sau y l chng trnh ngun minh ho. Mng MyFont nh trong ch vn bn, nhng phi cho 256 k t. Mng Sample gm 8 byte nh hnh bn, ch l mt n so vi tng byte ca mng MyFont, t nhn ra bit 1. /* GrFont16.h */ #include <math.h> #include <graphics.h> #include <string.h> const unsigned char MyFont[256*16] ={...}; const unsigned char Sample[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; Hm a mt k t Ch ln mn hnh ti to (x, y), mu Color, c ch Size v nghing Angle void OutChXY(int x,int y,int Angle,int Size, int Color,unsigned char Ch) { int i, j, k, l; float Cos=cos(Angle*M_PI/180), Sin=sin(Angle*M_PI/180); for (i=0;i<16;i++) for (j=0;j<8;j++) if (MyFont[Ch*16+i] & Sample[j]) for (k=0;k<Size;k++) for (l=0;l<Size;l++) putpixel(x+(Size*j+k)*Cos+(Size*i+l)*Sin, y-(Size*j+k)*Sin+(Size*i+l)*Cos, Color); } Hm a mt xu S ln mn hnh ti to (x, y), mu Color, c ch Size v nghing Angle void OutStrAngXY(int x,int y,int Angle,int Size, int Color,char *S) { float Cos=cos(Angle*M_PI/180), Sin=sin(Angle*M_PI/180); for (int k=0; k< strlen(S); k++) { OutChXY(x+8*Size*k*Cos, y-8*Size*k*Sin, Angle, Size, Color, S[k]); } } Hm a mt xu S ln mn hnh ti to (x, y), mu Color, c ch Size (Angle =0) void OutStrXY(int x,int y,int Size, int Color,char *S) { for (int k=0; k<strlen(S); k++) OutChXY(x+8*Size*k, y, 0, Size, Color, S[k]); }
Created by On Ngu Minh Trang 66