请问在TC中如何实现鼠标操作

jinweifu 2005-12-02 04:35:15
我想用鼠标在C程序中画图 不知道如何实现 请大家给个方向,最好有代码
...全文
658 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinweifu 2005-12-03
  • 打赏
  • 举报
回复
顶出来 有人能详细做答吗?
jinweifu 2005-12-02
  • 打赏
  • 举报
回复
我想他调用中断是用33H号功能调用 它用了一个geninterrupt(0x33);函数 不过不能看到他的代码 可惜 它里面是不是用了汇编代码
是不是在_CX=vx; 这个是放了X坐标
_DX=vy;放了Y坐标
_AX=0x0f;这个决定了什么
jixingzhong 2005-12-02
  • 打赏
  • 举报
回复
看看 C高级程序设计 中的中断相关内容吧 ~
在 C 中要用鼠标作图, 一定要用中断的 ~

上面的程序,也是用的中断技术~
类似 _AX=0x03; 的语句都是 中断号 或者是 功能号 ...
jixingzhong 2005-12-02
  • 打赏
  • 举报
回复
是用的中断技术 ...

x86 2005-12-02
  • 打赏
  • 举报
回复
通过设置不同寄存器参数去调用中断0x33可以实现鼠标的各种功能。
比如说
void MouseGetXY(int *x,int *y)
{
_AX=0x03;
geninterrupt(0x33);
*x=_CX;
*y=_DX;
}
这个函数通过调用汇编代码来得到鼠标的坐标。

那个Cursor数组是一个16x16的整形数组,表示一个16x16的掩码。相应位置为1就在屏幕上画一个点,这样就可以画出一个鼠标图像。
MouseSave将鼠标坐标位置的图形先保存下来,然后再显示鼠标。MouseOff时将之前保存的图像恢复到屏幕。
jsjjms 2005-12-02
  • 打赏
  • 举报
回复
涉及硬件了,比较麻烦的。
jinweifu 2005-12-02
  • 打赏
  • 举报
回复
那些符号是C宏吗
jinweifu 2005-12-02
  • 打赏
  • 举报
回复
我看到了这样的鼠标代码 下面有人帮我分析一下吗
为什么要设置__AX,__DX等寄存器
这个是MOUSE.C文件
#include <graphics.h>
void MouseOn(int x,int y)
{
int Cursor[16][16]={
{1},
{1,1},
{1,1,1},
{1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,0,1,1},
{1,1,0,0,1,1},
{1,0,0,0,0,1,1},
{0,0,0,0,0,1,1},
{0,0,0,0,0,0,1}

};
int i,j;
for(i=0;i<16;i++)
for(j=0;j<16;j++)
{
if(Cursor[i][j]!=0)
putpixel(x+j,y+i,RED);
}
}

void MouseSave(int Cursor[16][16],int x,int y)
{
int i,j;
for(i=0;i<16;i++)
for(j=0;j<16;j++)
Cursor[j][i]=getpixel(x+j,y+i);
}
MouseOff(int Cursor[16][16],int x,int y)
{
int i,j;
for(i=0;i<16;i++)
for(j=0;j<16;j++)
putpixel(x+j,y+i,Cursor[j][i]);
}
/*获取鼠标当前位置*/
void MouseGetXY(int *x,int *y)
{
_AX=0x03;
geninterrupt(0x33);
*x=_CX;
*y=_DX;
}

/*鼠标状态值初始化*/
void MouseReset()
{
_AX=0x00;
geninterrupt(0x33);
}

/*设置鼠标左右边界
lx:左边界
gx:右边界 */
void MouseSetX(int lx,int rx)
{
_CX=lx;
_DX=rx;
_AX=0x07;
geninterrupt(0x33);
}



/*设置鼠标速度(缺省值:vx=8,vy=1)
值越大速度越慢 */
void MouseSpeed(int vx,int vy)
{
_CX=vx;
_DX=vy;
_AX=0x0f;
geninterrupt(0x33);
}

/*获取鼠标按下键的信息*/
/*是否按下左键
返回值: 1=按下 0=释放*/
int LeftPress()
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&1);
}

/*是否按下右键
返回值同上 */
int RightPress()
{
_AX=0x03;
geninterrupt(0x33);
return(_BX&2);
}

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>

#include<f:\gui\mouse.c>

typedef struct MSG
{
int event;
int data;
struct MSG *next;
} msg;
typedef struct Window
{
int x,y;
int color,bkcolor;
int style;
}win;

typedef struct MENU{
int x,y;
int width,hight;
int bkcolor;
int fcolor;
int itemnum;
char *text;
void (*proc)(void *m,int e,msg data);
}menu;

typedef struct POINT
{
int x,y;
}point;



void *Buffer;
int x,y;
int Cursor[16][16];
point pt;
msg message[10];

msg TranslateMsg(menu *m,point *pt)
{
msg Msg;
int x=pt->y;
if(m==NULL)
{
Msg.event=3;
return Msg;
}
if((pt->x>m->x)&&(pt->x<(m->x+m->x+m->width))&&(pt->y>m->y)&&(pt->y<(m->y+m->hight)))
{
x=x-m->y;
Msg.data=x/18;
}
return Msg;
}

void drawmenu(menu *m)
{
int i;
if(m==NULL)
return;
setcolor(m->bkcolor);
bar(m->x,m->y,m->x+m->width,m->y+m->itemnum*18+2);
setcolor(m->fcolor);
settextstyle(1,0,1);
for(i=0;i<m->itemnum;i++)
outtextxy((m->x)+10,(m->y)+i*18,(m->text+i*16));
}
void MenuMsgProc(menu *m,int Msg,msg menumsg)
{
int i,j,color;

if(m==NULL)
return;

switch(Msg)
{
case 0:
for(i=m->x+1;i<m->x+100;i++)
for(j=m->y+1+menumsg.data*18;j<m->y+20+menumsg.data*18;j++)
{
color=getpixel(i,j);
if(color!=0&&color!=3)
putpixel(i,j,3);
}
break;
case 1:
for(i=m->x+1;i<m->x+100;i++)
for(j=m->y+1+menumsg.data*18;j<m->y+20+menumsg.data*18;j++)
{
color=getpixel(i,j);
if(color==3&&color!=WHITE)
putpixel(i,j,WHITE);
}
break;
case 3:
getimage(m->x,m->y,m->width,m->hight,Buffer);
break;
putimage(x,y,Buffer,COPY_PUT);
case 4:
break;
}
}

void MessageBox(win Box,char *text)
{
int i,j,color;
setcolor(RED);
line(Box.x-1,Box.y-1,Box.x+Box.width,Box.y+Box.hight);
line(Box.x-2,Box.y-2,Box.x+Box.width,Box.y+Box.hight);
bar(Box.x,Box.y,Box.x+Box.width,Box.y+Box.hight);
for(i=Box.x+1;i<Box.x+Box.width;i++)
for(j=Box.y+1;j<Box.y+20;j++)
{
color=getpixel(i,j);
if(color!=BLACK)
putpixel(i,j,3);
}

setcolor(BLACK);
outtextxy(Box.x+5,Box.y,"MessageBox");
outtextxy(Box.x+50,Box.y+70,text);
for(i=Box.x+100;i<Box.x+170;i++)
for(j=Box.y+100;j<Box.y+120;j++)
{
putpixel(i,j,2);
}

outtextxy(Box.x+120,Box.y+100,"OK");
}

void Init()
{
int gm=VGA,gd=VGAHI,i;
initgraph(&gm,&gd,"");
MouseReset();
Buffer=malloc(100*200);
if(Buffer==NULL)
{
printf("Error");
exit();
}

message[9].next=NULL;
for(i=0;i<9;i++)
message[i]=message[i+1];

}

main()
{
int mx,my,ox,oy,n=2,menuitem=100,mousestatus;
msg newMsg,oldMsg;
win MsgBox={180,150,300,150};
menu tmenu={100,100,100,200,BLACK,BLACK,6,NULL,MenuMsgProc},*disp=NULL;
char menutext[][16]={"New","Clean","Save","Save as","Exit"};
tmenu.text=menutext;

Init();
setbkcolor(BLUE);
MouseGetXY(&ox,&oy);
MouseSave(Cursor,ox,oy);
while(1)
{
MouseGetXY(&mx,&my);
pt.x=mx;
pt.y=my;
if(mx!=ox||my!=oy)
{
MouseOff(Cursor,ox,oy);
newMsg=TranslateMsg(&tmenu,&pt);
if(newMsg.data!=oldMsg.data)
{
(*tmenu.proc)(&tmenu,1,oldMsg);
(*tmenu.proc)(&tmenu,0,newMsg);
oldMsg.data=newMsg.data;
}
MouseSave(Cursor,mx,my);
MouseOn(mx,my);
ox=mx;oy=my;

}
if(newMsg.data==4&&LeftPress()!=0)
exit();
if(newMsg.data==1&&LeftPress()!=0)
{
cleardevice();
MouseSave(Cursor,mx,my);
MouseOn(mx,my);
}


if(RightPress()!=0)
{
tmenu.x=mx;
tmenu.y=my;
cleardevice();
drawmenu(&tmenu);
MouseSave(Cursor,mx,my);
MouseOn(mx,my);
}
if(newMsg.data!=4&&newMsg.data!=1&&LeftPress()!=0)
{
cleardevice();
MessageBox(MsgBox,menutext[newMsg.data]);
getch();
cleardevice();
MouseSave(Cursor,mx,my);
MouseOn(mx,my);
}
}
}
fiftymetre 2005-12-02
  • 打赏
  • 举报
回复
这个...等高手了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧