Tải bản đầy đủ (.pdf) (10 trang)

Giáo trình hướng dẫn tìm hiểu cách tạo các cổng giao tiếp trong các thiết bị ngoại vi phần 6 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (284.94 KB, 10 trang )

LUAÂÄN VAÊN TOÁT NGHIEÄP

77

#include "screen.h"

#define INT_MOUSE 0x33

#define M_LEFT 0x01 /* set if LEFT press */
#define M_RIGHT 0x02 /* set if RIGHT press */
#define M_CENTER 0x04 /* set if CENTER press */
#define M_CHANGE 0x08 /* set if mouse status is changed */

/* Keyboard coding bits */
#define KEY_NULL 0x0
#define ESC 0x1B
#define ENTER 0x0D
#define TAB '\t'
#define INS 0xD2
#define DEL 0xD3
#define BACKSPACE 0x8
#define HOME 0xC7
#define END 0xCF
#define PAGE_UP 0xC9
#define PAGE_DOWN 0xD1

/* Function's keys */
#define F1 0xBB
#define F2 0xBC
#define F3 0xBD
#define F4 0xBE


#define F5 0xBF
#define F6 0xC0
#define F7 0xC1
#define F8 0xC2
#define F9 0xC3
#define F10 0xC4

#define PLUS 0x2B
#define MINUS 0x2D
#define SPACEBAR 0x20

#define SHIFT_TAB 0x8F

#define CTRL_C 0x3

//struct time;
typedef struct tagEVENT{
int Msg;
int Posx, Posy; // toa do xay ra bien co chuot
struct time *t;
} EVENT;
typedef EVENT far *LPEVENT;
// Khai bao cac ham toan cuc
extern void SetMouseRange(LPRECT);//LPRECT: xem SCREEN.H
extern int InitMouse(LPRECT);
extern void ShowMouse(void);
extern void HideMouse(void);
extern void MouseRead(LPEVENT);
extern LPEVENT GetEvent(void);
extern void DeviceRelease(int);

//extern void ButtonPressed(int, LPEVENT);
extern void ResetEvent(LPEVENT);
//extern void MoveMouse(int x,int y);//tri
extern void MouseText(int x,int y,int color1, int color2);//,char
s[]);
//extern int GetKey(void);
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

78

extern int GetMtx();
extern int GetMty();
//extern EVENT OldEvent();
extern int Double(LPEVENT);
#endif
// End of MOUSE.H

IX.MODULE MOUSE.CPP
#include <conio.h>
#include <graphics.h>
//#include <STDLIB.H>
#include <COMPLEX.H>

#include "mouse.h"


void SetMouseRange(LPRECT lpRect)
// Dat vung di chuyen cho chuot
{

_AX = 0x07;//ham 07 cua ngat 33 de dinh gioi han di chuyen ngang
_CX = lpRect->left;
_DX = lpRect->right;
geninterrupt(INT_MOUSE);//INT_MOUSE=0x33:ngat 33
_AX = 0x08;//ham 08 cua ngat 33 dinh gioi han di chuyen doc cho con
tro
_CX = lpRect->top;
_DX = lpRect->bottom;
geninterrupt(INT_MOUSE);
}
//
int InitMouse(LPRECT lpRect)
// Khoi dong chuot
{
int ax;

_AX = 0;
geninterrupt(INT_MOUSE);
ax = _AX;
if (ax == 0)
return FALSE;
SetMouseRange(lpRect);
return TRUE;
}
//
void ShowMouse(void)
// Hien thi chuot
{
_AX = 1;
geninterrupt(INT_MOUSE);

}
//
void HideMouse(void)
// Dau chuot
{
_AX = 2;
geninterrupt(INT_MOUSE);
}
//
#include <CONIO.H>
#include <STDIO.H>
void MouseRead(LPEVENT lpEvent)
// Tra ve bien co nhan duoc tu chuot : Nut trai hay phai duoc an
{
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

79

int bx;
_AX = 3;
geninterrupt(INT_MOUSE);
bx = _BX;
//cx = _CX;
//dx = _DX;
lpEvent->Msg = bx;
lpEvent->Posx = _CX;
lpEvent->Posy = _DX;
gettime(lpEvent->t);
}

//
void ResetEvent(LPEVENT lpEvent)
{
lpEvent->Msg = 0;
lpEvent->Posx = lpEvent->Posy = 0;
}
//
EVENT PrevEvent, CurEvent;
//
LPEVENT GetEvent(void)
{
ResetEvent(&CurEvent);
MouseRead(&CurEvent);
// chuyen sang toa do ViewPort
CurEvent.Posx = CurEvent.Posx-MainWindow.rViewPort.left;
CurEvent.Posy = CurEvent.Posy-MainWindow.rViewPort.top;
// xet trang thai chuot co thay doi hay khong


if((CurEvent.Posx!=PrevEvent.Posx)&&(CurEvent.Posy!=PrevEvent.Posy))
CurEvent.Msg |= M_CHANGE;
PrevEvent = CurEvent;
// delay(50);
return (&CurEvent);
}
//
void DeviceRelease(int Msg)
{
while(GetEvent()->Msg&Msg);
}

//
int Double(LPEVENT lpEvent)
{
if(lpEvent->Msg==1)
{
delay(100);
ResetEvent(lpEvent);
lpEvent=GetEvent();
if(lpEvent->Msg==1)
//if((abs(CurEvent.Posx-oldEvent.Posx))<=10&&abs(CurEvent.Posy-
oldEvent.Posy)<=10)
{
{
return 1;
}
}
else
return 0;
}
else return 0;
}
//
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

80

void MouseText(int x,int y,int color1,int color2)//,char s[])//gia dau
nhay con tro
{

HideMouse();
// int h=textheight(s);
int w=5;
int color=getcolor();
do
{
for(int i=0;i<10&&(!kbhit());i++)
{
setcolor(color2);//WHITE);
line(x,y,x+w,y);
line(x,y+1,x+w,y+1);
delay(10);
setcolor(color);
}
setcolor(color1);//GREEN);
line(x,y,x+w,y);
line(x,y+1,x+w,y+1);
setcolor(color);
for(i=0;i<10&&(!kbhit());i++)
{
setcolor(color1);//GREEN);
line(x,y,x+w,y);
line(x,y+1,x+w,y+1);
delay(10);
setcolor(color);
}
}while(!kbhit());
}
//
int GetMtx()

{
return getx();
}
//
int GetMty()
{
return gety();
}
//
// End of MOUSE.CPP

X.CLASS SCREEN.H
#if !defined(__SCREEN_H__)
#define __SCREEN_H__


#define BORDERSIZE 3
#define TRUE 1
#define FALSE 0
#define DEMODELAY 0

// Dinh nghia cac kieu du lieu
typedef struct tagRECT{
int left, top, right, bottom ;
}RECT;
typedef RECT far *LPRECT;

typedef struct tagWINDOW {
char sCaption[80]; /*Window's caption*/
RECT rWinRect; /*Rectangle in which window is located*/

.
LUAÂÄN VAÊN TOÁT NGHIEÄP

81

RECT rSys; /*System area of window*/
RECT rTitleBar; /*Caption area of window*/
RECT rMenuBar; /*Rectangle in which window's menu is
located*/
RECT rMessage; /* Rectangle in which messages is display
}WINDOWS;

// Dinh nghia cac ham va bien dung toan cuc
extern int Xc, Yc; // Goc toa do
extern int LineColor;
extern int VP_WIDTH;
extern int VP_HEIGHT;
extern WINDOWS MainWindow;

extern void InitGraphics(void);
extern void OutMessage(char *Msg);
extern void InitScreen(void);
extern void InitValues(char *lpCaption);
extern int OutError(char *s);
#endif
// End of SCREEN.H

XI.MODULE SCREEN.CPP
#include <conio.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <graphics.h>

#include "screen.h"
#include "mouse.h"

int T_SIZE, W_SIZE, H_SIZE, W_CHAR, H_CHAR, MSG_SIZE;
int COLOR_WINDOW;
int COLOR_BUTTON;
int COLOR_BORDER;
int COLOR_CAPTION;
int COLOR_TEXT;
int VP_WIDTH;
int VP_HEIGHT;
int Maxx, Maxy; // Maxx - Maxy of screen

WINDOWS MainWindow;
//int Xc, Yc; // Goc toa do
//int LineColor = MAGENTA; // Mau cua duong thang can ve - Dung trong
thu tuc SetPixel
//
void InitGraphics(void)
// Khoi tao che do do hoa
{
int grDrv=DETECT, grMode;
int ErrCode;

initgraph(&grDrv, &grMode, "");
ErrCode = graphresult();

if(ErrCode!= grOk)
{
printf("Graphics error: %s\n", grapherrormsg(ErrCode));
printf("Press any key to halt ");
getch();
exit(1); /* return with error code */
}
Maxx = getmaxx();
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

82

Maxy = getmaxy();
}
//
void InitValues(char *lpCaption)
// Khoi tao cac gia tri cho bien Windows de ve cua so man hinh
{
int WinWidth, WinHeight;

W_CHAR = 8; H_CHAR = 8; // Chieu rong va chieu cao cua mot ki tu
W_SIZE = 8; H_SIZE = 20; T_SIZE = 18; MSG_SIZE = 20;

COLOR_WINDOW = CYAN;
COLOR_BUTTON = LIGHTGRAY;
COLOR_BORDER = LIGHTGRAY;
COLOR_CAPTION = BLUE;
COLOR_TEXT = BLUE;


strcpy(MainWindow.sCaption, lpCaption);

// Gan gia tri cho WinRect - H.C.nhat chua cua so
MainWindow.rWinRect.left = 0;
MainWindow.rWinRect.top = 0;
MainWindow.rWinRect.right = Maxx;
MainWindow.rWinRect.bottom= Maxy;

WinWidth = MainWindow.rWinRect.right-MainWindow.rWinRect.left+1;
WinHeight= MainWindow.rWinRect.bottom-MainWindow.rWinRect.top+1;

// Gan gia tri cho rSys - H.C.nhat chua SystemMenu
MainWindow.rSys.left = BORDERSIZE;
MainWindow.rSys.top = BORDERSIZE;
MainWindow.rSys.right = MainWindow.rSys.left+T_SIZE;
MainWindow.rSys.bottom= MainWindow.rSys.top+T_SIZE;

// Gan gia tri cho rTitleBar - H.C.nhat chua thong tin thanh
tieu de
MainWindow.rTitleBar.left =
BORDERSIZE+1;//MainWindow.rSys.right+1;
MainWindow.rTitleBar.top = BORDERSIZE+1;
MainWindow.rTitleBar.right = WinWidth-BORDERSIZE-1;
MainWindow.rTitleBar.bottom= MainWindow.rSys.top+T_SIZE;

// Gan gia tri cho rMenuBar - H.C.nhat chua thong tin menu
// Gan gia tri cho rMessage -
H.C.nhat chua thong tin cua cac
//thong bao
MainWindow.rMessage.left = BORDERSIZE+1;

MainWindow.rMessage.right = WinWidth-BORDERSIZE-2;
MainWindow.rMessage.top = WinHeight-(BORDERSIZE+2)-MSG_SIZE;
MainWindow.rMessage.bottom= MainWindow.rMessage.top+MSG_SIZE;

// Gan gia tri cho rViewPort - H.C.nhat chua thong tin ve vung
lam viec
// thuc su cua cua so
MainWindow.rViewPort.left = BORDERSIZE+1;
MainWindow.rViewPort.top =
MainWindow.rTitleBar.bottom+2;//MainWindow.rMenuBar.bottom+2;
MainWindow.rViewPort.right = WinWidth-BORDERSIZE-2;
MainWindow.rViewPort.bottom= MainWindow.rMessage.top-2;

VP_WIDTH = MainWindow.rViewPort.right-MainWindow.rViewPort.left;
VP_HEIGHT = MainWindow.rViewPort.bottom-MainWindow.rViewPort.top;
}
//
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

83

void OutMessage(char *Msg)
// Ghi thong bao Msg ra cua so rMessage
{
struct fillsettingstype FillInfo;
int OldColor;
struct viewporttype vp;

// luu thong tin cu

getfillsettings(&FillInfo);
OldColor = getcolor();
getviewsettings(&vp);
// xoa thong bao cu
setviewport(MainWindow.rWinRect.left, MainWindow.rWinRect.top,
MainWindow.rWinRect.right, MainWindow.rWinRect.bottom,
1);
setfillstyle(SOLID_FILL, COLOR_BUTTON);
setcolor(COLOR_TEXT);
bar(MainWindow.rMessage.left, MainWindow.rMessage.top,
MainWindow.rMessage.right, MainWindow.rMessage.bottom);
// in thong bao moi ra
outtextxy(MainWindow.rMessage.left+W_CHAR,
MainWindow.rMessage.bottom-H_CHAR-5, Msg);
// tra ve trang thai cu
setfillstyle(FillInfo.pattern, FillInfo.color);
setcolor(OldColor);
setviewport(vp.left,vp.top, vp.right, vp.bottom, 1);
}
//
void DrawScreenBorder(int left, int top, int right, int bottom)
// Ve duong vien cho mot hinh chu nhat co toa do (left, top), (right,
bottom)
{
setfillstyle(SOLID_FILL, COLOR_BORDER);
bar(left, top, right, top+BORDERSIZE);
bar(left, bottom, right, bottom-BORDERSIZE);
bar(left, top, left+BORDERSIZE, bottom);
bar(right-BORDERSIZE, top, right, bottom);
setcolor(BLACK);

rectangle(left, top, right, bottom);
rectangle(left+BORDERSIZE, top+BORDERSIZE,
right-BORDERSIZE, bottom-BORDERSIZE);
}
//
#include <dos.h>
void InitScreen(void)
// Ve cac thanh phan cua cua so
{
int x, y;
int WinWidth, WinHeight;
struct viewporttype vp;

getviewsettings(&vp);
setviewport(MainWindow.rWinRect.left, MainWindow.rWinRect.top,
MainWindow.rWinRect.right, MainWindow.rWinRect.bottom,
1);
WinWidth = MainWindow.rWinRect.right-MainWindow.rWinRect.left+1;
WinHeight= MainWindow.rWinRect.bottom-MainWindow.rWinRect.top+1;

x = (WinWidth-W_CHAR*strlen(MainWindow.sCaption))/2+1;
y = BORDERSIZE+(T_SIZE-H_CHAR)/2+1;

// Ve duong vien cua hinh chu nhat
DrawScreenBorder(0, 0, WinWidth-1, WinHeight-1);
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

84


delay(DEMODELAY);
// Ve thanh tieu de
setfillstyle(SOLID_FILL, COLOR_CAPTION);
bar(MainWindow.rTitleBar.left, MainWindow.rTitleBar.top,
MainWindow.rTitleBar.right, MainWindow.rTitleBar.bottom);
setcolor(WHITE);
outtextxy(x, y, MainWindow.sCaption);
delay(DEMODELAY);
// Ve SystemMenu
setfillstyle(SOLID_FILL, COLOR_BUTTON);
/* bar(MainWindow.rSys.left+1, MainWindow.rSys.top+1,
MainWindow.rSys.right-1, MainWindow.rSys.bottom-1);
setcolor(BLACK);
rectangle((MainWindow.rSys.left+MainWindow.rSys.right)/2-5,
(MainWindow.rSys.top+MainWindow.rSys.bottom)/2-1,
(MainWindow.rSys.left+MainWindow.rSys.right)/2+5,
(MainWindow.rSys.top+MainWindow.rSys.bottom)/2+1);
delay(DEMODELAY);
*/ // Ve Menu
// bar(MainWindow.rMenuBar.left, MainWindow.rMenuBar.top,
// MainWindow.rMenuBar.right, MainWindow.rMenuBar.bottom);
// outtextxy(MainWindow.rMenuBar.left+8, MainWindow.rMenuBar.bottom-
13,
// "Menu of Program is here");
// delay(DEMODELAY);
// Ve thanh chua thong bao
bar(MainWindow.rMessage.left, MainWindow.rMessage.top,
MainWindow.rMessage.right, MainWindow.rMessage.bottom);
OutMessage("Welcome to Demo Window Program");
delay(DEMODELAY);

// Ve vung lam viec
setfillstyle(SOLID_FILL, COLOR_WINDOW);
bar(MainWindow.rViewPort.left, MainWindow.rViewPort.top,
MainWindow.rViewPort.right, MainWindow.rViewPort.bottom);
setcolor(BLACK);
rectangle(MainWindow.rViewPort.left-1, MainWindow.rViewPort.top,
MainWindow.rViewPort.right+1, MainWindow.rViewPort.bottom);
delay(DEMODELAY);
// Dat lai viewport la rViewPort
setviewport(MainWindow.rViewPort.left, MainWindow.rViewPort.top,
MainWindow.rViewPort.right, MainWindow.rViewPort.bottom,
1);
}
//
/*void DrawSysCoordinates(void)
// Ve he truc toa do
{
Xc = VP_WIDTH/2;
Yc = VP_HEIGHT/2;
setcolor(COLOR_TEXT);
// ve hai truc toa do
line(0, Yc, VP_WIDTH, Yc);
line(Xc, 0, Xc, VP_HEIGHT);
// ve hai mui ten
line(Xc, 0, Xc-5, 5);
line(Xc, 0, Xc+5, 5);
line(VP_WIDTH, Yc, VP_WIDTH-5, Yc-5);
line(VP_WIDTH, Yc, VP_WIDTH-5, Yc+5);
// Ve goc toa do
outtextxy(Xc-8, Yc+8, "O");

}
//
void SetLineColor(int C)
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

85

{
LineColor = C;
}
//
void SetPixel(int x, int y)
{
putpixel(Xc+x, Yc-y, LineColor);
}
//
void Set2Pixel(int Ox, int Oy, int x, int y)
{
putpixel(Ox+x, Oy-y, LineColor);
putpixel(Ox-x, Oy-y, LineColor);
}
//
void Set2PixelHor(int Ox, int Oy, int x, int y)
{
putpixel(Ox+x, Oy-y, LineColor);
putpixel(Ox+x, Oy+y, LineColor);
}
//
void ClearViewPort(void)

{
struct fillsettingstype FillInfo;

// luu thong tin cu
getfillsettings(&FillInfo);
setfillstyle(SOLID_FILL, COLOR_WINDOW);
bar(0, 0, VP_WIDTH, VP_HEIGHT);
setfillstyle(FillInfo.pattern, FillInfo.color);
DrawSysCoordinates();
} */
// End of SCREEN.C
//
#include <alloc.h>
int OutError(char *s)
{
void far *buf;//con tro chi vung luu tru man hinh
unsigned int size = imagesize(200,150,470,240);
LPEVENT lpEvent;
int Cx1=280,Cy1=203,Cx2=337,Cy2=223;
int Ox1=350,Oy1=203,Ox2=405,Oy2=223;
if ((buf = farmalloc(size)) == NULL)
{
return -1;
}
else
{
getimage(200,150,470,240,buf);
}
setfillstyle(SOLID_FILL,BLUE);
HideMouse();

bar(200,150,470,240);
setcolor(WHITE);
rectangle(200,150,470,240);
outtextxy(200+(270-8*strlen(s))/2,170,s);
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(Cx1,Cy1,Cx2,Cy2);//280,203,337,223);
int OK=0;
outtextxy(285,210,"Cancel");
outtextxy(365,210,"OK");
int event;
do
.
LUAÂÄN VAÊN TOÁT NGHIEÄP

86

{
lpEvent=GetEvent();
if(kbhit())
{
if((event=getch())==13)
{
CompleteOk:
HideMouse();
putimage(200,150,buf,COPY_PUT);
ShowMouse();
farfree(buf);
return OK;
}
if(event==27)

{
CompleteCancel:
HideMouse();
putimage(200,150,buf,COPY_PUT);
ShowMouse();
farfree(buf);
return 0;
}
if(event==9)
{
OK=!OK;
Cancel:
if(OK==0)
{
HideMouse();
setfillstyle(SOLID_FILL,BLUE);
bar(Ox1,Oy1,Ox2,Oy2);//350,203,405,223);
outtextxy(365,210,"OK");
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(Cx1,Cy1,Cx2,Cy2);//280,203,337,223);
outtextxy(285,210,"Cancel");
ShowMouse();
}
Ok:
if(OK==1)
{
HideMouse();
setfillstyle(SOLID_FILL,BLUE);
bar(Cx1,Cy1,Cx2,Cy2);//280,203,337,223);
outtextxy(285,210,"Cancel");

setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(Ox1,Oy1,Ox2,Oy2);//350,203,405,223);
outtextxy(365,210,"OK");
ShowMouse();
}
}
if(event==0)
{
event=getch();
if(event==75)
{
OK=0;
goto Cancel;
}
if(event==77)
{
OK=1;
goto Ok;
}
.

×