在自己的软件中使用鼠标
以下举出一个鼠标编程在应用程序中使用的例程,这是一个输入一系列机床故障诊断规则并存盘的程序,注意它的编程结构和对鼠标应用的处理:
头文件 1:mouse.h
#include <conio.h>
#include <dos.h>
char InitMs()
{ /*鼠标复位*/
union REGS Inr, Outr;
Inr.x.ax=0;
int86(0x33,&Inr,&Outr);
if(Outr.x.ax==0) return 0;
else return 1;
}
void ShowMs()
{ /*显示鼠标光标*/
union REGS Inr, Outr;
Inr.x.ax=1;
int86(0x33,&Inr,&Inr);
}
void HideMs()
{ /*隐藏鼠标光标*/
union REGS Inr, Outr;
Inr.x.ax=2;
int86(0x33,&Inr,&Inr);
}
char LeftPrsd()
{ /*按鼠标左键*/
union REGS Inr, Outr;
Inr.x.ax=3;
int86(0x33,&Inr,&Outr);
return(Outr.x.bx&1);
}
char RightPrsd()
{ /*按鼠标右键*/
union REGS Inr, Outr;
Inr.x.ax=3;
int86(0x33,&Inr,&Outr);
return(Outr.x.bx&2);
}
void ReadMs(int *f, int *x, int y) /*f: 按键状态*/
{ /*读鼠标状态*/ /* LEFT: 1*/
union REGS Inr, Outr; /* RIGHT: 2*/
Inr.x.ax=3; /*x: 水平坐标*/
int86(0x33,&Inr,&Outr); /* y: 垂直坐标*/
*f=Outr.x.bx;
*x=Outr.x.cx;
*y=Outr.x.dx;
}
void SetMsXY(int x, int y)
{ /* 设置鼠标光标位置*/
union REGS Inr, Outr;
Inr.x.cx=x;
Inr.x.dx=y;
Inr.x.ax=4;
int86(0x33,&Inr,&Inr);
}
void SetMsArea(int Xmin, int Ymin, int Xmax, int Ymax)
{ /*设置鼠标移动范围*/
union REGS Inr, Outr;
Inr.x.ax=7;
Inr.x.cx=Xmin;
Inr.x.dx=Xmax;
int86(0x33,&Inr,&Inr);
Inr.x.ax=8;
Inr.x.cx=Ymin;
Inr.x.dx=Ymax;
int86(0x33,&Inr,&Inr);
}
void MsHideArea(int x1, int y1, int x2, int y2)
{ /*设置隐藏鼠标区域*/
union REGS Inr, Outr;
Inr.x.cx=x1;
Inr.x.dx=y1;
Inr.x.si=x2;
Inr.x.di=y2;
Inr.x.ax=0x10;
int86(0x33,&Inr,&Inr);
}
int MsInBox(int x1, int y1, int x2, int y2, int x, int y)
{ /*判断鼠标是否在某一矩形区域内*/
return((x>=x1&&x<=x2&&y>=y1&&y<=y2)? 1 : 0 );
}
头文件 2:menu.h
#include <bios.h>
#include <graphics.h>
#include <math.h>
#include <alloc.h>
#include <string.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
/******************************************************
//16点阵汉字输出函数,供主函数及所有函数调用
//入口参数: (X,Y): 输出点坐标. Wid: 字间距.
// Color: 字颜色. *Str: 字符串.
//******************************************************/
void zwsc16(int x, int y, int Wid, int Color, char *Str)
{
FILE *fp;
unsigned Zcode, Bcode;
int i,j,k,Rec;
long Len;
char Buf[32];
fp=fopen("hzk16","rb");
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);
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+Wid;
Str+=2;
}
else Str++;
}
fclose(fp);
}
Box(int x,int y,int l,int h){
setfillstyle(1,8);
bar(x,y,x+l,y+h);
setfillstyle(1,LIGHTGRAY);
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);
}
ReBox(int x,int y,int l,int h){
setfillstyle(1,8);
bar(x,y,x+l,y+h);
setfillstyle(1,LIGHTGRAY);
bar(x+2,y+2,x+l-2,y+h-2);
setcolor(15);
line(x,y+h,x+l,y+h);
line(x+l,y+h,x+l,y);
line(x+1,y+h-1,x+l-1,y+h-1);
line(x+l-1,y+h-1,x+l-1,y+1);
}
CoBox(int x,int y,int l,int h,int color){
setfillstyle(1,8);
bar(x,y,x+l,y+h);
setfillstyle(1,color);
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);
}
CoReBox(int x,int y,int l,int h,int color){
setfillstyle(1,8);
bar(x,y,x+l,y+h);
setfillstyle(1,color);
bar(x+2,y+2,x+l-2,y+h-2);
setcolor(15);
line(x,y+h,x+l,y+h);
line(x+l,y+h,x+l,y);
line(x+1,y+h-1,x+l-1,y+h-1);
line(x+l-1,y+h-1,x+l-1,y+1);
}
void getstr(int x,int y,char *str){
int key;
char ch;
char s[2];
int i=0;
s[1]='\0';
setfillstyle(SOLID_FILL,WHITE);
setcolor(DARKGRAY);
outtextxy(x+i*8,y,"_");
while((
key=getch())!=13){
if((key==8)||(key==75)){
if(i>0){
i--;
bar(x+i*8,y,x+(i+2)*8-1,y+8);
outtextxy(x+i*8,y,"_");
}
}else if(ch=(char)(key&0x00ff)){
str[i]=ch;
s[0]=ch;
bar(x+i*8,y,x+(i+1)*8-1,y+9);
setcolor(DARKGRAY);
outtextxy(x+i*8,y,s);
outtextxy(x+(i+1)*8,y,"_");
i++;
}
else{
sound(1000);
delay(100);
nosound();
}
}
str[i]='\0';
setcolor(WHITE);
outtextxy(x+i*8,y,"_");
}
头文件 3:ruler.h
char fstate[8][20]={
"数控曲轴磨床",
"曲轴磨加工",
"上料定位",
"砂轮修整",
"零件加工",
"加工液压传动",
"磨削量测量",
"上料液压传动",
};
char fcode[8][5]={ "f00","f11","f12",
"f21","f22","f23","f24","f25",
};
char rstate[15][26]={
"艾科斯轴进给故障",
"艾科斯轴进给电气故障",
"艾科斯轴导轨润滑故障",
"行程开关故障",
"接口板故障",
"执行继电器故障",
"行程开关故障",
"接口板故障",
"液压电磁阀故障",
"接口板光隔坏",
"接口板断线",
"接口板接触不良",
"接口板光隔?quot;,
"接口板断线",
"接口板接触不良",
};
char rcode[15][5]={
"R11",
"R21","R22",
"R31","R32","R33","R34","R35","R36",
"R41","R42","R43","R44","R45","R46",
};
主程序 ruler.cpp:
#include<stdio.h>
#include"menu.h"
#define HADD 185
#define LADD -95
#include"ruler.h"
#include"mouse.h"
#define G 5;
char *filename;
struct RULER{
int number;
char result[10];
char premise[10];
float prob;
};
struct RULER brul[100];
int rulsum;
void inform() /*信息显示窗口*/
{
int i;
Box(5,22,630,260);
ReBox(10,25,620,230);
for(i=0;i<2;i++)
{Box(30+i*180,260,60,20);
}
zwsc16(35,262,5,DARKGRAY,"下一页"); /*选择按钮*/
zwsc16(215,263,5,DARKGRAY,"上一页");
setcolor(DARKGRAY);
for(i=1;i<=11;i++) /*显示词条信息*/
{
outtextxy(13,33+(i-1)*20,fcode[i-1]);
zwsc16(100,28+(i-1)*20,0,DARKGRAY,fstate[i-1]);
}
}
void view(int i,int ib) /*已生成的规则信息的显示*/
{
char mids[6];
int j=0;
setfillstyle(1,WHITE);
bar(200+LADD,115+HADD,240+LADD,125+HADD);
bar(170+LADD,130+HADD,240+LADD,145+HADD);
bar(170+LADD,170+HADD,240+LADD,185+HADD);
bar(170+LADD,210+HADD,240+LADD,225+HADD);
setcolor(DARKGRAY);
if(i<ib)
{
itoa(brul[i].number,mids,10);
outtextxy(210+LADD,115+HADD,mids);
outtextxy(180+LADD,135+HADD,brul[i].premise);
outtextxy(180+LADD,175+HADD,brul[i].result);
sprintf(mids,"%1.3f",brul[i].prob);
outtextxy(180+LADD,215+HADD,mids);
}
else
{
brul[i].number=i+1;
itoa(brul[i].number,mids,10);
outtextxy(210+LADD,115+HADD,mids);
}
Box(235,285,400,170);
ReBox(240,290,390,160);
zwsc16(245,295,0,DARKGRAY,"如果");
zwsc16(245,330,0,DARKGRAY,"那么");
zwsc16(245,365,0,DARKGRAY,"可信度");
if(strstr(filename,"f"))
for(j=0;j<5;j++)
{
if(!strcmp(brul[i].premise,fcode[j]))
zwsc16(285,295,0,YELLOW,fstate[j]);
if(!strcmp(brul[i].result,fcode[j]))
zwsc16(285,330,0,YELLOW,fstate[j]);
}
else
if(strstr(filename,"r"))
for(j=0;j<15;j++)
{
if(!strcmp(brul[i].premise,rcode[j]))
zwsc16(285,295,0,YELLOW,rstate[j]);
if(!strcmp(brul[i].result,rcode[j]))
zwsc16(285,330,0,YELLOW,rstate[j]);
}
outtextxy(295,370,mids);
}
void main()
{
int gd=DETECT,gm;
FILE *fp1;
int i=0,pionti;
int btn,mx,my;
int hang=6,hm,sum;
char mids[6];
char ns[6];
char *str=" ";
int ib=0;
int k,listi;
int pagei=1;
system("c:\\ucdos\\ucdos.bat");
printf("\n请输入要编辑的知识库名:");
scanf("%s",filename);
initgraph(&gd,&gm,"c:\\bc\\bin\\bgi");
Box(100+LADD,100+HADD,200,170);
zwsc16(150+LADD,110+HADD,10,YELLOW,"规则");
zwsc16(110+LADD,130+HADD,0,YELLOW,"如果");
zwsc16(110+LADD,170+HADD,0,YELLOW,"就");
zwsc16(110+LADD,210+HADD,0,YELLOW,"置信度");
for(i=0;i<6;i++)
Box(250+LADD,110+HADD+25*i,40,20);
/*直接写出菜单各项的选项信息*/
zwsc16(252+LADD,112+HADD,2,DARKGRAY,"删除");
zwsc16(252+LADD,112+25+HADD,2,DARKGRAY,"重写");
zwsc16(252+LADD,112+50+HADD,2,DARKGRAY,"下页");
zwsc16(252+LADD,112+75+HADD,2,DARKGRAY,"上页");
zwsc16(252+LADD,112+100+HADD,2,DARKGRAY,"添加");
zwsc16(252+LADD,112+125+HADD,2,DARKGRAY,"退出");
if((fp1=fopen(filename,"rt"))==NULL)
{
printf("Can not open file\n");
exit(0);
}
/*从文件读出规则信息到结构体数组中*/
while(fscanf(fp1,"%d",&brul[ib].number)!=EOF)
{
fscanf(fp1,"%s",brul[ib].premise);
fscanf(fp1,"%s",brul[ib].result);
fscanf(fp1,"%s",mids);
brul[ib].prob=atof(mids);
ib++;
}
rewind(fp1);
i=0;
hm=1;
inform();
InitMs(); /*初始化鼠标*/
ShowMs();
view(i,ib); /*显示鼠标光标*/
/*进入循环测试鼠标状态循环*/
for(;;)
{
ReadMs(&btn,&mx,&my);
if(btn==1&&MsInBox(30,260,90,280,mx,my)) /*选中信息窗口向下翻页*/
{
ReBox(10,25,620,230);
setcolor(DARKGRAY);
pagei++;
if(pagei<=5)
for(listi=(1+pagei*10);listi<=(11+pagei*10);listi++)
{
outtextxy(13,33+(listi-1-10*pagei)*20,fcode[listi-1]);
zwsc16(100,28+(listi-1-10*pagei)*20,0,DARKGRAY,fstate[listi-1]);
}
else
{
if(pagei>22)
pagei=22;
for(listi=(1+(pagei-6)*10);listi<=(11+(pagei-6)*10);listi++)
{
outtextxy(13,33+(listi-1-10*(pagei-6))*20,rcode[listi-1]);
zwsc16(100,28+(listi-1-10*(pagei-6))*20,0,DARKGRAY,rstate[listi-1]);
}
}
}
if(btn==1&&MsInBox(210,260,270,280,mx,my)) /*选中信息窗口向上翻页*/
{
ReBox(10,25,620,230);
setcolor(DARKGRAY);
pagei--;
if(pagei<0) pagei=0;
if(pagei<=5&&pagei>0)
for(listi=(1+pagei*10);listi<=(11+pagei*10);listi++)
{
outtextxy(13,33+(listi-1-10*pagei)*20,fcode[listi-1]);
zwsc16(100,28+(listi-1-10*pagei)*20,0,DARKGRAY,fstate[listi-1]);
}
else
for(listi=(1+(pagei-6)*10);listi<=(11+(pagei-6)*10);listi++)
{
outtextxy(13,33+(listi-1-10*(pagei-6))*20,rcode[listi-1]);
zwsc16(100,28+(listi-1-10*(pagei-6))*20,0,DARKGRAY,rstate[listi-1]);
}
}
if(btn==1&&MsInBox(248+LADD,133-25+HADD,245+46+LADD,133-25+24+HADD,mx,my))
/*选中规则库操作的删除选项*/
{
pionti=i;
sum=ib-1;
for(k=pionti;k<sum;k++)
{
brul[k].prob=brul[k+1].prob;
strcpy(brul[k].premise,brul[k+1].premise);
strcpy(brul[k].result,brul[k+1].result);
}
brul[k].prob=0;
strcpy(brul[k].premise,str);
strcpy(brul[k].result,str);
view(i,ib);
delay(200);
}
if(btn==1&&MsInBox(248+LADD,133-25+(2-1)*25+HADD,245+46+LADD,133-25+24+HADD+(2-1)*25,mx,my)) /*选中规则库操作的重写选项*/
{
setfillstyle(1,WHITE);
bar(170+LADD,130+HADD,240+LADD,145+HADD);
bar(170+LADD,170+HADD,240+LADD,185+HADD);
bar(170+LADD,210+HADD,240+LADD,225+HADD);
getstr(180+LADD,135+HADD,brul[i].premise);
getstr(180+LADD,175+HADD,brul[i].result);
getstr(180+LADD,215+HADD,mids);
brul[i].prob=atof(mids);
if(i>=ib)
ib++;
view(i,ib);
delay(200);
}
if(btn==1&&MsInBox(248+LADD,133-25+(3-1)*25+HADD,245+46+LADD,133-25+24+HADD+(3-1)*25,mx,my)) /*选中规则库操作的下页选项*/
{
i++;
view(i,ib);
delay(200);
}
if(btn==1&&MsInBox(248+LADD,133-25+(4-1)*25+HADD,245+46+LADD,133-25+24+HADD+(4-1)*25,mx,my)) /*选中规则库操作的上页选项*/
{
i--;
if(i<0)
i=0;
view(i,ib);
delay(200);
}
if(btn==1&&MsInBox(248+LADD,133-25+(5-1)*25+HADD,245+46+LADD,133-25+24+HADD+(5-1)*25,mx,my)) /*选中规则库操作的添加选项*/
{
i=ib;
view(i,ib);
delay(200);
}
if(btn==1&&MsInBox(248+LADD,133-25+(6-1)*25+HADD,245+46+LADD,133-25+24+HADD+(6-1)*25,mx,my)) /*选中规则库操作的退出选项*/
{
closegraph();
fclose(fp1);
if((fp1=fopen(filename,"wt"))==NULL)
{
printf("Can not open file\n");
exit(0);
}
for(i=0;i<ib;i++)
{
if(brul[i].prob!=0)
fprintf(fp1,"%d %s %s %1.3f\n",brul[i].number,brul[i].premise,
brul[i].result,brul[i].prob);
}
fclose(fp1);
system("c:\\ucdos\\quit.com");
exit(0);
}
}
}