Khoa Công Nghệ Thông Tin ---BÁO CÁO ĐỒ ÁN LẬP TRÌNH HỆ THỐNG Giáo viên hướng dẫn : Đặng Bá Lư... Đề tài Minh họa sử dụng chuột, hiển thị tọa độ chuột 2.. Yêu cầu II- Chương trình 1.. Đ
Trang 1Khoa Công Nghệ Thông Tin
-BÁO CÁO ĐỒ ÁN LẬP TRÌNH HỆ THỐNG
Giáo viên hướng dẫn : Đặng Bá Lư
Trang 2I- Giới thiệu
1 Đề tài
Minh họa sử dụng chuột, hiển thị tọa độ chuột
2 Yêu cầu
II- Chương trình
1 Ngắt sử dụng
+ Khai báo thư viện dos.h cho int 33h và union REGS
#include <dos.h>
+ Gọi ngắt 33h / ax = 0000h
Nếu thành công : ax = 0FFFFh , bx = “number of mouse button”
Gọi ngắt 33h / ax = 0001h để hiển thị con trỏ chuột
Đồng thời gọi ngắt 33h / ax = 0024h lấy thông tin chuột
Code :
union REGS _regs;
_regs.x.ax = 0x0000; // Gan ax = 0000h -> Goi ngat khoi tao chuot int86(0x33, &_regs, &_regs);
Num_Mouse_Button = _regs.h.bl;// Dem so button chuot _regs.x.ax = 0x0001; // gan ax = 0001h -> Goi ngat hien thi chuot int86(0x33, &_regs, &_regs);
_regs.x.ax = 0x0024; // Ngat ve thong tin chuot
int86(0x33,&_regs, &_regs);
Num_Type_Mouse = _regs.h.ch;
Mở vòng lặp :
+ Lấy vị trí con trỏ và trạng thái button
Gọi ngắt 33h với ax = 0003h
Khi đó cx,dx tương ứng là hoành độ và tung độ của vị trí con trỏ
Đồng thời gán trạng thái click cho *St_Button
Nếu :
Code :
void Get_Pos_Mouse(int * MP_x,int * MP_y,int * St_Button)
{
union REGS _regs;
_regs.x.ax = 0x0003;
int86(0x33,&_regs,&_regs);
Trang 3*MP_x = _regs.x.cx / 8;
*MP_y = _regs.x.dx / 8;
*St_Button= _regs.x.bx;
}
+ Tính toán tọa độ chuột
Code :
void Change_Str_Pos_Mouse(int x,int y)
{
Str_Pos_Mouse[0]=(x/10 + 0x30);
Str_Pos_Mouse[1]=(x%10 + 0x30);
Str_Pos_Mouse[3]=(y/10 + 0x30);
Str_Pos_Mouse[4]=(y%10 + 0x30);
}
+ Tùy biến theo trạng thái button
Code :
void Event_Mouse(int MouseX,int MouseY,int MouseClick)
{
char _Is_OnMenu ;
_Is_OnMenu = Is_OnMenu(MouseX, MouseY);
if ( _Is_OnMenu != 0)
switch (MouseClick) {
} }
+ Hiển thị tọa độ và Menu
Code :
void Show_Menu(char On_Menu)
{
WriteXY(Colum_Menu,Row_Menu,Str_Pos_Mouse);
WriteXY(Colum_Menu,Row_Menu+1,Str_Menu);
}
void WriteXY(char x,char y,char * Msg)
{
gotoxy(x,y);
cprintf("%s",Msg); // Hien menu }
int Is_OnMenu(int x, int y)
{
int result;
result=0;
Trang 4if ((x+1 >= Colum_Menu) && (x+1 <= Colum_Menu + 4)
&& ( y+1 >= Row_Menu + 1) && (y+1 <= Row_Menu+ 3 )) result = y - Row_Menu +1;
return(result);
}
Đóng vòng lặp
III- Mã nguồn C++
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
char * Str_Pos_Mouse = "00:00";
char * Str_Menu = "Exit ";
char Colum_Menu = 1; // Hien thi goc trai man hinh
char Row_Menu = 1;
const char Left_Click = 1;
int MouseX, MouseY, MouseClick;
char Num_Mouse_Button, Num_Type_Mouse;// So nut bam va loai chuot
void WriteXY(char x,char y, char * Msg);
void Show_Menu(char On_Menu);
void Clear_Screen();
void Setup_Mouse();
void Get_Pos_Mouse(int * MP_x,int * MP_y,int * St_Button);
void Change_Str_Pos_Mouse(int x,int y);
int ShowFile(char * StrFileName);
void Event_Left_Click(char _Is_OnMenu);
int Is_OnMenu(int x, int y);
void Show_Info();
void Event_Mouse(int MouseX,int MouseY,int MouseClick);
// -void main()
{
Clear_Screen();
Setup_Mouse();
Show_Info();
do
{
Get_Pos_Mouse(&MouseX, &MouseY,&MouseClick);
Change_Str_Pos_Mouse(MouseX, MouseY);
Event_Mouse(MouseX, MouseY, MouseClick);
Show_Menu(Is_OnMenu(MouseX, MouseY));
} while (1);
}
// -void Show_Info()
Trang 5char *Type_Mouse;
switch(Num_Type_Mouse)
{
case 1 : Type_Mouse ="Duong tuyen"; break;
case 2 : Type_Mouse ="Tuan tu"; break;
case 3 : Type_Mouse ="USB - Inport"; break;
case 4 : Type_Mouse ="PS/2"; break;
case 5 : Type_Mouse ="HP"; break;
}
gotoxy(1,3);
printf("\n Thong tin co ban ve chuot : \n");
printf("\n Loai chuot : %s",Type_Mouse);
printf("\n So nut bam : %d",Num_Mouse_Button);
}
// -void Clear_Screen()
{
textcolor(WHITE);
textbackground(BLUE);
clrscr();
}
// -void WriteXY(char x,char y,char * Msg)
{
gotoxy(x,y);
cprintf("%s",Msg);
}
// -void Show_Menu(char On_Menu)
{
WriteXY(Colum_Menu,Row_Menu,Str_Pos_Mouse);
WriteXY(Colum_Menu,Row_Menu+1,Str_Menu);
}
// -int Is_OnMenu(// -int x, // -int y)
{
int result;
result=0;
if ((x+1 >= Colum_Menu) && (x+1 <= Colum_Menu + 4)
&& ( y+1 >= Row_Menu + 1) && (y+1 <= Row_Menu+ 3 )) result = y - Row_Menu +1;
return(result);
}
// -void Setup_Mouse()
{
union REGS _regs;
_regs.x.ax = 0x0000; // Gan ax = 0000h -> Goi ngat khoi tao chuot int86(0x33, &_regs, &_regs);
Num_Mouse_Button = _regs.h.bl;// Dem so button chuot _regs.x.ax = 0x0001; // gan ax = 0001h -> Goi ngat hien thi chuot _regs.x.ax = 0x0024;
int86(0x33,&_regs, &_regs);
Trang 6Num_Type_Mouse = _regs.h.ch;// Xac dinh loai chuot
}
// -void Get_Pos_Mouse(int * MP_x,int * MP_y,int * St_Button)
{
union REGS _regs;
_regs.x.ax = 0x0003;
int86(0x33,&_regs,&_regs); // Goi ngat int 33h/ax=0000h
*MP_x = _regs.x.cx / 8 ; // Lay toa do
*MP_y = _regs.x.dx / 8 ; // Lay toa do
*St_Button= _regs.x.bx; // Lay trang thai
}
// -void Change_Str_Pos_Mouse(int x,int y)
{
Str_Pos_Mouse[0]=(x/10 + 0x30);
Str_Pos_Mouse[1]=(x%10 + 0x30); // Tinh toan toa do
Str_Pos_Mouse[3]=(y/10 + 0x30);
Str_Pos_Mouse[4]=(y%10 + 0x30);
}
// -void Event_Left_Click(char _Is_OnMenu)
{
switch (_Is_OnMenu) {
case 1 : exit(0); break;
} }
// -void Event_Mouse(int MouseX,int MouseY,int MouseClick)
{
char _Is_OnMenu ;
_Is_OnMenu = Is_OnMenu(MouseX, MouseY);
if ( _Is_OnMenu != 0)
switch (MouseClick) {
} }