#include<dos.h>
#include "stdio.h"
#include "stdlib.h"
#include<conio.h>
#include<graphics.h>
#include<bios.h>
#include <time.h>
int
pixel_save[16][16];
/* 保存被鼠标覆盖的区域颜色*/
int
mouse_draw[16][16];
/*
0x0003,0x000D,0x0032,0x00C2,
0x0304,0x0C04,0x3008,0xC008,
0x4010,0x2010,0x1020,0x0820,
0x0440,0x0240,0x0180,0x0080,
*/
int
Mouse_Shape[2][16]={
0x0000,0x4000,0x6000,0x7000,
0x7800,0x7c00,0x7e00,0x7f00,
0x7f80,0x7e00,0x7c00,0x4600,
0x0600,0x0300,0x0300,0x0180,
/*
hand
*/
0x0c00,0x1200,0x1200,0x1200,
0x13fe,0x1249,0x1249,0x1249,
0x7249,0x9001,0x9001,0x9001,
0x8001,0x8001,0x4002,0x3ffc,
};
union REGS regs;
int mouse_x,mouse_y,mouse_k;
int old_mx,old_my,old_mk;
int *buttons;
char MouseClickType=1;
#define MouseDoubleClickTime 0.3 /*鼠标双击的最大时间间隔*/
clock_t LastMouseClickTime=0; /*上一次鼠标单击的时间*/
clock_t NowMouseClickTime=0; /*这次鼠标单击的时间 */
int char_v,scan_v;
int hour,minute,second,pre_sec,sec1,old_sec1,time_f=1;
int MouseOn();
int MouseOff();
int shape=0,hot_mouse=0;
int pixi,pixj;
int savimage[16][16];
int disp_time();
int ss=0;
int PutCC16(int x, int y, int, int Color, char *Str);
int PrsBox(int x, int y, int l, int h);
int Box(int x, int y, int l, int h);
int OpenLIB();
int
MsInBox(int x1, int y1, int x2, int y2);
FILE *fp;
int ReadButton();/* 检测按钮信息
*/
int
x1, y1, l, h, x2;
/* https://blog. */
int main()
{
int
gmode=0,gdriver=DETECT;/* 和gdriver = VGA,gmode = VGAHI是同样效果 */
int
Button, x, y, Choice;
char *Tit = "用鼠标选择菜单";
char *Mmu[] = {"绘制新图", "编辑旧图",
"图形输出", "退出系统"};
gdriver=VGA;
gmode=VGAHI;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
if(graphresult())
{
printf ("Graphics isn't available...");
getch();exit(1);
}
x1 = 190;
y1 = 240;
x2 = 350;
l
= 100;
h
= 32;
MouseInit();
GetMouseXY();
old_mx=mouse_x;
old_my=mouse_y;
SetMouseXY(mouse_x,mouse_y);
OpenLIB();
setfillstyle(1, 7);
bar(140, 150, 500, 460);
/* 150
上位置 */
setcolor(14);
rectangle(140, 150, 500, 460);
/* 150
上位置 */
setfillstyle(1, 9);
bar(215,230, 430,180);
/* 左 下 右 上
*/
PutCC16(250, 200, 4, 15, Tit);
/* 200
上 位置 */
Box(x1, y1, l, h);
PutCC16(x1+15, y1+8, 2, 1, Mmu[0]);
/* 显示菜单的项 */
Box(x2, y1, l, h);
PutCC16(x2+15, y1+8, 2, 1, Mmu[1]);
Box(x1, y1+100, l, h);
PutCC16(x1+15, y1+108, 2, 1, Mmu[2]);
Box(x2, y1+100, l, h);
PutCC16(x2+15, y1+108, 2, 1, Mmu[3]);
setcolor(1);
rectangle(140,80,500,110);
setfillstyle(8,2);
floodfill(150,100,1);setcolor(15);
outtextxy(182,85,"Move to me to change mouse's shape");
outtextxy(180,100,"Click me to quit ...
QQ: 25653806");
while(1)
{
read_mouse();
MouseOff();
shape=0;
if(char_v==0x011b) break;
gotoxy(2,9);
printf("X=%03d
Y=%03d
K=%03d
%04X
SS=%5d ",mouse_x,mouse_y,mouse_k,char_v,ss++);
if( MouseInbox(140,80,500,110,mouse_x,mouse_y))
{
/* 改变鼠标形状*/
shape=1;
PrsBox(140,80,500,110);
if(mouse_k==-6)
{break;}
}
if (MouseInbox(x1, y1, x1+l, y1+h))
/* 判断选择的是哪一个功能*/
{
shape=1;
PrsBox(x1, y1, x1+l, y1+h);
if(mouse_k==-6)
{return;}
}
if (MouseInbox(x2, y1, x2+l, y1+h))
{
shape=1;
PrsBox(x2, y1, x2+l, y1+h);
if(mouse_k==-6)
{return;}
}
if (MouseInbox(x1, y1+100, x1+l, y1+100+h))
{
shape=1;
PrsBox(x1, y1+100, x1+l, y1+100+h);
if(mouse_k==-6)
{return;}
}
if (MouseInbox(x2, y1+100, x2+l, y1+100+h))
{
shape=1;
PrsBox(x2, y1+100, x2+l, y1+100+h);
if(mouse_k==-6)
{return;}
}
}
}
/*
putpixel(mouse_x+pixj-hot_mouse,mouse_y+pixi,~savimage[pixi][pixj]&0xFF);
*/
int MouseOn()
{
/* 鼠标光标显示*/
int
i,j;
for(pixi=0;pixi<16;pixi++)
{
int test=0x0001;
for(pixj=15;pixj>=0;pixj--)
{
if((Mouse_Shape[shape][pixi]&test)!=0)
putpixel(mouse_x+pixj-hot_mouse,mouse_y+pixi,~savimage[pixi][pixj]&0xFF);
test<<=1;
}
}
}
int MouseOff()
{
/* 隐藏鼠标*/
int _x,_y;
static int flag=1; /* 第一次运行程序 */
if(flag==1)
{
for(pixi=0,_y=mouse_y;pixi<16;pixi++,_y++)
for(pixj=0,_x=mouse_x;pixj<16;pixj++,_x++)
savimage[pixi][pixj]=getpixel(_x,_y);
flag=0; return;
}
for(pixi=0;pixi<16;pixi++)
{
int test=0x0001;
for(pixj=15;pixj>=0;pixj--)
{
if((Mouse_Shape[shape][pixi]&test)!=0)
putpixel(old_mx+pixj-hot_mouse,old_my+pixi,savimage[pixi][pixj]);
test<<=1;
}
}
for(pixi=0,_y=mouse_y;pixi<16;pixi++,_y++)
for(pixj=0,_x=mouse_x;pixj<16;pixj++,_x++)
savimage[pixi][pixj]=getpixel(_x-hot_mouse,_y);
}
int PrsBox(int x, int y, int l, int h)
/* 立体块"按下", 并"弹起" */
{
void *Buf1, *Buf2;
Buf1 = malloc(imagesize(x, y, l, h));
Buf2 = malloc(imagesize(x+4, y+4, l-4, h-4));
getimage(x, y, l, h, Buf1);
getimage(x+4, y+4, l-4, h-4, Buf2);
setfillstyle(1, 1);
bar(x, y, l, h);
putimage(x+4, y+4, Buf2, COPY_PUT);
delay(2500);
putimage(x, y, Buf1, COPY_PUT);
delay(2500);
free(Buf1);
free(Buf2);
}
int Box(int x, int y, int l, int h)
/* 画一立体块 */
{
setcolor(0);
rectangle(x-1, y-1, x+l+1, y+h+1);
setfillstyle(1, 8);
bar(x, y, x+l, y+h);
setfillstyle(1, 7);
bar(x+2, y+2, x+l-2, y+h-2);
setcolor(15);
line(x, y, x+l, y);
line(x, y, x, y+h);
line(x+1, y+1, x+l-1, y+1);
line(x+1, y+1, x+1, y+h-1);
}
int MouseInbox(int x1,int y1,int x2,int y2)
{
return((mouse_x >= x1 && mouse_x <= x2 && mouse_y >= y1 && mouse_y <= y2) ? 1 : 0);
/* 若在预定区域就返回 1 */
}
/***************************************
old_mx,old_my;
static int mouse_k,old_mk,old_mk2
*****************************************/
int read_mouse()
{
MouseOff();
MouseOn();
mouse_k=0;
do{
if(kbhit()!=0)
{
mouse_k=0;
char_v=get_key(&scan_v);
return char_v;
}/* 若有键盘点击返回键值*/
disp_time();
old_mx=mouse_x;
old_my=mouse_y;
regs.x.ax=3;
regs.x.bx=0;
int86(0x33,®s,®s);
mouse_x=regs.x.cx;
/* x坐标*/
mouse_y=regs.x.dx;
/* y坐标*/
/*
mouse_k=regs.x.bx;
鼠标击键状态*/
if((_BX&1) && !(_BX&2)) /*鼠标左键被按下同时右键没有按下*/
mouse_k=-1;
if(!(_BX&1) && (_BX&2)) /*鼠标右键被按下同时左键没有按下*/
mouse_k=-2;
if(_BX&1 && _BX&2) /*鼠标左键和右键同时被按下*/
mouse_k=-3;
_AX=0x06; /*读取鼠标按钮释放信息*/
_BX=0x00; /*指定读取鼠标左键的释放信息*/
geninterrupt(0x33); /*产生33号鼠标软中断*/
if(_BX==1) /*如果鼠标左键的释放数为1*/
mouse_k=-4; /*产生一个单击左键信息*/
_AX=0x06; /*读取鼠标按钮释放信息*/
_BX=0x01; /*指定读取鼠标右键的释放信息*/
geninterrupt(0x33); /*产生33号鼠标软中断*/
if(_BX==1) /*如果鼠标左键的释放次数为1*/
mouse_k=-5; /*产生一个单击右键信息*/
if(mouse_k==-4)
{
if(MouseClickType==1)
{
NowMouseClickTime=clock();/*获得现在的程序运行时间*/
/*判断上一次按下鼠标的右键到这次按下鼠标左键的时间间隔是不是小于最大鼠标双击时间间隔*/
if(((NowMouseClickTime-LastMouseClickTime)/CLK_TCK)>MouseDoubleClickTime)
mouse_k=-6;
else mouse_k=-8;
}
LastMouseClickTime=NowMouseClickTime;
MouseClickType=1; /*作为下一次的按键判断时的上一次按键状态*/
}
if(mouse_k==-5)
{
if(MouseClickType==2)
{
NowMouseClickTime=clock();/*获得现在的程序运行时间*/
/*判断上一次按下鼠标的右键到这次按下鼠标左键的时间间隔是不是小于最大鼠标双击时间间隔*/
if(((NowMouseClickTime-LastMouseClickTime)/CLK_TCK)>MouseDoubleClickTime)
mouse_k=-7;
else mouse_k=-9;
}
LastMouseClickTime=NowMouseClickTime;
MouseClickType=2; /*作为下一次的按键判断时的上一次按键状态*/
}
}
while(old_mx==mouse_x && old_my==mouse_y
&&
old_mk==mouse_k);
MouseOff();
/* 原位置异或消去擦除,并还原*/
old_mx=mouse_x;
old_my=mouse_y;
old_mk=mouse_k;
MouseOn();
return -1;
}
int MouseInit()
{
/* 初始化鼠标*/
int recored;
regs.x.ax=0;
int86(0x33,®s,®s);/* 调用中断检测*/
recored=regs.x.ax;
if(recored==0)
{
return 0;
}
return recored;
}
/*获取鼠标当前位置*/
int GetMouseXY()
{
_AX=0x03;
geninterrupt(0x33);
mouse_x=_CX;
mouse_y=_DX;
}
/*设置鼠标当前位置
x:横向坐标
y:纵向坐标 */
int SetMouseXY(int x,int y)
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
}
/***********************************************************/
/*
READ_XY(LINE,COLUMN)
*/
/*
PROGRAM FOR READ THE SITE OF SRCP
*/
/***********************************************************/
int read_xy(int *line,int *column)
{
union REGS reg;
reg.h.ah = 0x03;
reg.h.bh = 0x0;
int86(0x10,®,®);
*line = reg.h.dh;
*column = reg.h.dl;
}
/*****************************************************************/
/*
GOTO_XY(X,Y)
SUBPROGRAM FOR SEND THE CURSOR TO X,Y
*/
/*****************************************************************/
int goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2;
/*
cursor adressing function
*/
r.h.dl=y;
/*
column coordinate
*/
r.h.dh=x;
/*
row coordination
*/
r.h.bh=0;
/*
vidio page
*/
int86(0x10,&r,&r);
}
/*************************************************************************/
/*
GET_KEY(...)
SUBPROGRAM FOR READ THE 16_BIT SCAN CODE OF A KEY
*/
/*************************************************************************/
int get_key(int
*scan_vv)
{
union REGS r;
int
c_v;
r.h.ah=0x0;
int86(0x16,&r,&r);
*scan_vv=r.h.ah;
c_v=r.h.al;
return(c_v);
}
/**********************************************/
/*
*/
/**********************************************/
int disp_time()
{
int x1,y1,year,month,day,week;
static int time,old_time;
union REGS reg;
reg.h.ah=0x2a;
int86(0x21,®,®);
year = reg.x.cx;
month = reg.h.dh;
day = reg.h.dl;
week = reg.h.al;
reg.h.ah = 0x2c;
int86(0x21,®,®);
hour = reg.h.ch;
minute = reg.h.cl;
second = reg.h.dh;
pre_sec = reg.h.dl;
time=second;
if (time!=old_time && time_f==1)
{
read_xy(&x1,&y1);
goto_xy(0,70);
printf("%02d:%02d:%02d",hour,minute,second);
goto_xy(24,72);
switch(week)
{ case 0: printf("Sun (0)");break;
case 1: printf("Mon (1)");break;
case 2: printf("Tue (2)");break;
case 3: printf("Wed (3)");break;
case 4: printf("Thu (4)");break;
case 5: printf("Fri (5)");break;
case 6: printf("Sat (6)");break;
}
goto_xy(24,1);
printf("%04d/%02d/%02d",year,month,day);
goto_xy(x1,y1);
MouseOff();
MouseOn();
old_time=time;
}
}
int word()
{
int x=90,y=180;
settextstyle(0,0,4);
setcolor(BLUE);
outtextxy(x,y,"M");
setcolor(GREEN);
outtextxy(x+=32,y,"0");
setcolor(CYAN);
outtextxy(x+=32,y,"U");
setcolor(RED);
outtextxy(x+=32,y,"S");
setcolor(MAGENTA);
outtextxy(x+=32,y,"E");
setcolor(BLACK);
outtextxy(x+=32,y," ");
setcolor(BROWN);
outtextxy(x+=32,y,"M");
setcolor(LIGHTGREEN);
outtextxy(x+=32,y,"a");
setcolor(LIGHTCYAN);
outtextxy(x+=32,y,"g");
setcolor(LIGHTRED);
outtextxy(x+=32,y,"n");
setcolor(LIGHTMAGENTA);
outtextxy(x+=32,y,"i");
setcolor(YELLOW);
outtextxy(x+=32,y,"f");
setcolor(WHITE);
outtextxy(x+=32,y,"i");
setcolor(BLUE);
outtextxy(x+=32,y,"e");
setcolor(RED);
outtextxy(x+=32,y,"r");
}
int OpenLIB()
/* 打开16点阵汉字库 */
{
fp = fopen("hzk16",
"rb");
}
int PutCC16(int x, int y, int z, int Color, char *Str) /* 显示16点阵汉字*/
{
unsigned
Zcode, Bcode;
/* 区码, 位码 */
int
i, j, k, Rec;
long Len;
char Buf[32];
while (*Str)
/* 直到字串显示完 */
{
if ((*Str & 0x80) && (*(Str+1) &0x80))
/* 是汉字 */
{
Zcode = (*Str-0xa1) & 0x07f;
/* 区码 */
Bcode = (*(Str+1)-0xa1) & 0x07f;
/* 位码 */
Rec = Zcode*94+Bcode;
/* 记录号 */
Len = Rec*32L;
/* 在字库中位置 */
fseek(fp, Len, SEEK_SET);
fread (Buf, 1, 32, fp);
/* 32字节 */
for (i = 0; i < 16; i++)
for (j = 0; j < 2; j++)
for (k = 0; k < 8; k++)
if (Buf[i*2+j] >> (7-k) & 1)
putpixel(x+j*8+k, y+i, Color);
x = x+16+z;
Str += 2;
}
}
return;
}
[此贴子已经被作者于2021-8-16 23:34编辑过]