C语言图形模式编程之个性鼠标(引擎)

/* -----   C语言图形模式编程,个性鼠标。 -----*/
int Msinit()
{   /* 初始化鼠标*/
    int recored;
    regs.x.ax=0;
    int86(0x33,&regs,&regs);
    recored=regs.x.ax;
    if(recored==0)
    {   printf("Mouse not found !\n");
        getch();return 0;
    }
    regs.x.ax = 1;  /* 显示鼠标*/
    int86(0x33,&regs,&regs);
    return recored;
}
void MskindMath()    /*计算鼠标的样子*/
{   /*Point hotspot[]={{0,11},{0,0},{4,0},{4,0}};热点*/
    int    i,j,jj,k;
    int    full,kind;
    long   UpNum[2][16] = {/*  hand */
        0xf3ff,0xe1ff,0xe1ff,0xe1ff,
        0xe001,0xe000,0xe000,0xe000,
        0x8000,0x0000,0x0000,0x0000,
        0x0000,0x0000,0x8001,0xc003,
        0x3fff,0x1fff,0x0fff,0x07ff,/*  arrow箭头 */
        0x03ff,0x01ff,0x00ff,0x007f,
        0x003f,0x00ff,0x01ff,0x10ff,
        0x30ff,0xf87f,0xf87f,0xfc3f,
    };
    long   DownNum[2][16]={/*  hand */
        0x0c00,0x1200,0x1200,0x1200,
        0x13fe,0x1249,0x1249,0x1249,
        0x7249,0x9001,0x9001,0x9001,
        0x8001,0x8001,0x4002,0x3ffc,
        0x0000,0x4000,0x6000,0x7000,/*  arrow箭头 */
        0x7800,0x7c00,0x7e00,0x7f00,
        0x7f80,0x7e00,0x7c00,0x4600,
        0x0600,0x0300,0x0300,0x0180,
    };
    kind = (Mskind>>1)%2;  /* 手形还是箭头*/
    full = Mskind%2;       /* 是否空心*/
    for(i=0;i<16;i++)
    {
         j=jj=15;
         while(UpNum[kind][i]!=0)
         {
             up[i][j]=UpNum[kind][i]%2;
             j--;
             UpNum[kind][i]/=2;
         }
         while(DownNum[kind][i]!=0)
         {
             down[i][jj--]=DownNum[kind][i]%2;
             DownNum[kind][i]/=2;
         }
         for(k=j ;k>=0;k--)  up  [i][k]=0;
         for(k=jj;k>=0;k--)  down[i][k]=0;
         for(k=0;k<16;k++)
         {   /*四种组合方式*/
             if(up[i][k]==0&&down[i][k]==0)
             {   /* 是否为空心*/
                 if(full) mouse_draw[i][k]=1;
                 else     mouse_draw[i][k]=2;
             }
             else if(up[i][k]==0&&down[i][k]==1)
             {
                 if(full) mouse_draw[i][k]=2;
                 else     mouse_draw[i][k]=1;
             }
             else if(up[i][k]==1&&down[i][k]==0) mouse_draw[i][k]=3;
             else                                mouse_draw[i][k]=4;
         }
    }
    if(kind==1) mouse_draw[1][2]=0;/*特殊点*/
    else        mouse_draw[1][2]=0;
}
void MsOn(int x,int y)
{   /* 鼠标光标显示*/
    int  i,j;
    int  color[2];
    color[0]=Mskind>>2;
    color[1]=Mskind>>6;
    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
        {
            pixel_save[i][j]=getpixel(x+j,y+i);/* 保存原来的颜色*/
            if(mouse_draw[i][j]==1)
               putpixel(x+j,y+i,color[0]);
            else if(mouse_draw[i][j]==2)       /* 画鼠标*/
               putpixel(x+j,y+i,color[1]);
        }
    }
}
void MsOff(int x,int y)
{   /* 隐藏鼠标*/
    int  i,j,color;
    for(i=0;i<16;i++)
    for(j=0;j<16;j++)
    {
         if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)  continue;
         color=getpixel(x+j,y+i);
         putpixel(x+j,y+i,color^color);     /* 原位置异或消去*/
         putpixel(x+j,y+i,pixel_save[i][j]);/* 还原原背景*/
    }
}
int Msread(int *px,int *py,int *buttons)
{  /* 读取鼠标及键盘状态*/
    int    but_old=*buttons;
    int    x_old=*px,y_old=*py;
    struct time t;
    struct date d;/* 当前时间信息*/
    do{
         if(kbhit()) return bioskey(0);
         regs.x.ax=3;
         int86(0x33,&regs,&regs);
         *px=regs.x.cx;
         *py=regs.x.dx;
         *buttons=regs.x.bx;
    }while(*px==x_old&&*py==y_old&&*buttons==but_old);
    MsOff(x_old,y_old);
    but_old=*buttons;
    x_old=*px;
    y_old=*py;
    MsOn(x_old,y_old);
    return -1;
}
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);  /* 若在预定区域就返回 1 */
}
 
实例1
/*-----------------------------------------------------------*/
/*---                                                     ---*/
/*---          C 语言"箭头鼠标"与"手形鼠标"的切换         ---*/
/*---                                                     ---*/
/*---                                ---   2007.03.18  ---*/
/*-----------------------------------------------------------*/
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
/*=======================  必需的全局变量  ===================*/
union REGS regs;
int   up[16][16],down[16][16];
int   mouse_draw[16][16];
int   pixel_save[16][16];      /* 保存被鼠标覆盖的区域颜色*/
int   Mousekind=0+1*2+15*4+2*64;
      /* "形状(0和1)","标记外围或内部颜色(其中的 15 和 2)"*/
/*===========================================================*/
void MouseMath()    /* 计算鼠标的样子*/
{   /*Point hotspot[]={{0,11},{0,0},{4,0},{4,0}};热点*/
    int    i,j,jj,k;
    int    full,kind;
    long   UpNum[2][16] = { /* hand */
        0xf3ff,0xe1ff,0xe1ff,0xe1ff,
        0xe001,0xe000,0xe000,0xe000,
        0x8000,0x0000,0x0000,0x0000,
        0x0000,0x0000,0x8001,0xc003,
        0x3fff,0x1fff,0x0fff,0x07ff,/*  arrow箭头 */
        0x03ff,0x01ff,0x00ff,0x007f,
        0x003f,0x00ff,0x01ff,0x10ff,
        0x30ff,0xf87f,0xf87f,0xfc3f,
    };
    long   DownNum[2][16]={ /* hand */
        0x0c00,0x1200,0x1200,0x1200,
        0x13fe,0x1249,0x1249,0x1249,
        0x7249,0x9001,0x9001,0x9001,
        0x8001,0x8001,0x4002,0x3ffc,
        0x0000,0x4000,0x6000,0x7000,/*  arrow箭头 */
        0x7800,0x7c00,0x7e00,0x7f00,
        0x7f80,0x7e00,0x7c00,0x4600,
        0x0600,0x0300,0x0300,0x0180,
    };
    kind = (Mousekind>>1)%2;  /* 手形还是箭头*/
    full = Mousekind%2;       /* 是否空心*/
    for(i=0;i<16;i++)
    {
         j=jj=15;
         while(UpNum[kind][i]!=0)
         {
             up[i][j]=UpNum[kind][i]%2;
             j--;
             UpNum[kind][i]/=2;
         }
         while(DownNum[kind][i]!=0)
         {
             down[i][jj--]=DownNum[kind][i]%2;
             DownNum[kind][i]/=2;
         }
         for(k=j ;k>=0;k--)  up  [i][k]=0;
         for(k=jj;k>=0;k--)  down[i][k]=0;
         for(k=0;k<16;k++)
         {   /*四种组合方式*/
             if(up[i][k]==0&&down[i][k]==0)
             {   /* 是否为空心*/
                 if(full)    mouse_draw[i][k]=1;
                 else        mouse_draw[i][k]=2;
             }
             else if(up[i][k]==0&&down[i][k]==1)
             {
                 if(full)    mouse_draw[i][k]=2;
                 else        mouse_draw[i][k]=1;
             }
             else if(up[i][k]==1&&down[i][k]==0) mouse_draw[i][k]=3;
             else                                mouse_draw[i][k]=4;
         }
    }
    if(kind==1) mouse_draw[1][2]=0;/*特殊点*/
    else        mouse_draw[1][2]=4;
}
void MouseOn(int x,int y)
{   /* 鼠标光标显示*/
    int  i,j;
    int  color[2];
    color[0]=Mousekind>>2;
    color[1]=Mousekind>>6;
    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
        {
            pixel_save[i][j]=getpixel(x+j,y+i);/* 保存原来的颜色*/
            if(mouse_draw[i][j]==1)
                putpixel(x+j,y+i,color[0]);
            else if(mouse_draw[i][j]==2)       /* 画鼠标*/
                putpixel(x+j,y+i,color[1]);
        }
    }
}
void MouseOff(int x,int y)
{   /* 隐藏鼠标*/
    int  i,j,color;
    for(i=0;i<16;i++)
    for(j=0;j<16;j++)
    {
         if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)  continue;
         color=getpixel(x+j,y+i);
         putpixel(x+j,y+i,color^color);     /* 原位置异或消去*/
         putpixel(x+j,y+i,pixel_save[i][j]);/* 还原原背景*/
    }
}
int MouseRead(int *px,int *py,int *buttons)
{  /* 读取鼠标及键盘状态*/
   int    but_old=*buttons;
   int    x_old=*px,y_old=*py;
   do{               /* 或 return getch(); 返回ASCII码值*/
         if(kbhit()) return bioskey(0);/* 若有键盘点击返回键值*/
         regs.x.ax=3;
         int86(0x33,&regs,&regs);
         *px=regs.x.cx;       /* x坐标*/
         *py=regs.x.dx;       /* y坐标*/
         *buttons=regs.x.bx;  /* 返回鼠标击键状态:
                                 0未点击    1点击左键
                                 2点击右键  3点击双键*/
   }while(*px==x_old&&*py==y_old&&*buttons==but_old);
   MouseOff(x_old,y_old);/* 原位置异或消去擦除,并还原*/
   but_old=*buttons;
   x_old=*px;
   y_old=*py;
   MouseOn(*px,*py);     /* 新位置显示*/
   return -1;
}
int MouseInbox(int x1,int y1,int x2,int y2,int x,int y)
{
     return((x>=x1&&x<=x2&&y>=y1&&y<=y2)?1:0);  /* 若在预定区域就返回 1 */
}
int MouseInit()
{   /* 初始化鼠标*/
    int recored;
    MouseMath();            /* 初始化时先计算*/
    regs.x.ax=0;
    int86(0x33,&regs,&regs);/* 调用中断检测*/
    recored=regs.x.ax;
    if(recored==0)
    {   printf("Mouse not found !\n");
        getch();return 0;
    }
    regs.x.ax = 1;          /* 显示鼠标*/
    int86(0x33,&regs,&regs);
    return recored;
}
void main()
{
    int  msx,msy,msz,order;
    int  gmode=0,gdriver=DETECT;/* 和gdriver = VGA,gmode = VGAHI是同样效果 */
    registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
    initgraph(&gmode,&gdriver,"");
    if(graphresult())
    {
        printf ("Graphics isn't available...");
        getch();exit(1);
    }
    MouseInit();/* 初始化*/
    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: 527274766");
    for(;;)
    {
        order=MouseRead(&msx,&msy,&msz);
        if(order==283)  break;
        if( MouseInbox(140,80,500,110,msx,msy))
        {   /* 改变鼠标形状*/
            if(Mousekind!=1+0*2+9*4+15*64)
            {
                MouseOff(msx,msy);/* 擦除*/
                Mousekind=1+0*2+9*4+15*64;/* 改变*/
                MouseMath();/* 重新计算鼠标形状*/
            }
            if(msz==1)   { closegraph();exit(0);}
        }
        else
        {   /* 还原鼠标形状*/
            if(Mousekind!=0+1*2+15*4+2*64)
            {
                MouseOff(msx,msy);
                Mousekind=0+1*2+15*4+2*64;
                MouseMath();
            }
       }
   }
}
实例2
/***********************************************************************
                            简易绘图板
引擎了个简易绘画版。
                                                        修改人:cdmalcl
***********************************************************************/

/* -----   C语言图形模式编程,个性鼠标。 -----*/
#include <graphics.h>
#include <dos.h>
#define WL -3   /* 鼠标绘制的横向宽度 */
#define HL 3    /* 鼠标绘制的纵向高度 */
#define LEFT_C 15       /* 鼠标左键绘制图形的颜色 */
#define RIGHT_C 0       /* 鼠标右键绘制图形的颜色 */
#define EXIT_X 450      /* 退出菜单的横坐标位置 */
#define EXIT_Y 350      /* 退出菜单的纵坐标位置 */
#define E_WL 60         /* 退出菜单的横向长度 */
#define E_HL 15         /* 退出菜单的纵向高度 */
int Init();     /*程序初始化*/
int Run();      /*程序主要过程*/
int DrawRectangle(int OutType ,int x ,int y);       /*绘制一个矩形或线或点*/
int Msinit();
void MskindMath();
void MsOn(int x,int y);
void MsOff(int x,int y);
int Msread(int *px,int *py,int *buttons);
int Msinbox(int x1,int y1,int x2,int y2,int x,int y);
int End();      /*结束程序*/
int Mskind = 0xb;
long up[2][16]={0} ,down[2][16]={0};
long mouse_draw[16][16]={0} ,pixel_save[16][16]={0};
union REGS regs;
int main()
{
    Init();
    Run();
    End();
}
int Init()
{
    int gd = DETECT ,gm = 0;
    initgraph(&gd ,&gm ,"");
    rectangle(EXIT_X ,EXIT_Y ,EXIT_X+E_WL ,EXIT_Y + E_HL);
    settextstyle(1 ,0 ,4);
    outtextxy(EXIT_X+E_WL/4 ,EXIT_Y+E_HL/3 ,"EXIT");
    Msinit();
    MskindMath();
}
int Run()
{
    int x ,y ,buttons = 0;
    x = getmaxx()/2;
    y = getmaxy()/2;
    MsOn(x,y);
    while(Msread(&x ,&y ,&buttons)!=283)    /* 退出键退出程序 */
    {
        if(Msinbox(EXIT_X ,EXIT_Y ,EXIT_X+E_WL ,EXIT_Y + E_HL ,x ,y))  /* 点击退出选项退出程序 */
        {
            if(buttons == 1)
            {
                End();
            }
        }
        else
        {
            if(buttons == 1)
            {
                DrawRectangle(0 ,x ,y);
            }
            if(buttons == 2)
            {
                DrawRectangle(1 ,x ,y);
            }
        }
    }
    return 1;
}
#if WL < 0
#define WL 0
#endif
#if HL < 0
#define HL 0
#endif
int DrawRectangle(int OutType ,int x ,int y)
{
    MsOff(x ,y);
    if(OutType)
    {
         #if HL > 1
            setcolor(RIGHT_C);
            rectangle(x ,y ,x+WL ,y+HL);
            #if WL > 1
                setfillstyle(1 ,RIGHT_C);
                floodfill(x+1 ,y+1 ,getcolor());
            #endif
        #else
            putpixel(x ,y ,RIGHT_C);
        #endif
    }
    else
    {
        #if WL > 1
            setcolor(LEFT_C);
            rectangle(x ,y ,x+WL ,y+HL);
            #if HL > 1
                setfillstyle(1 ,LEFT_C);
                floodfill(x+1 ,y+1 ,getcolor());
            #endif
        #else
            putpixel(x ,y ,LEFT_C);
        #endif
    }
    MsOn(x,y);
}
int Msinit()
{   /* 初始化鼠标*/
    int recored;
    regs.x.ax=0;
    int86(0x33,&regs,&regs);
    recored=regs.x.ax;
    if(recored==0)
    {   printf("Mouse not found !\n");
        getch();return 0;
    }
    regs.x.ax = 1;  /* 显示鼠标*/
    int86(0x33,&regs,&regs);
    return recored;
}
void MskindMath()    /*计算鼠标的样子*/
{   /*Point hotspot[]={{0,11},{0,0},{4,0},{4,0}};热点*/
    int    i,j,jj,k;
    int    full,kind;
    long   UpNum[2][16] = {/*  hand */
        0xf3ff,0xe1ff,0xe1ff,0xe1ff,
        0xe001,0xe000,0xe000,0xe000,
        0x8000,0x0000,0x0000,0x0000,
        0x0000,0x0000,0x8001,0xc003,
        0x3fff,0x1fff,0x0fff,0x07ff,/*  arrow箭头 */
        0x03ff,0x01ff,0x00ff,0x007f,
        0x003f,0x00ff,0x01ff,0x10ff,
        0x30ff,0xf87f,0xf87f,0xfc3f,
    };
    long   DownNum[2][16]={/*  hand */
        0x0c00,0x1200,0x1200,0x1200,
        0x13fe,0x1249,0x1249,0x1249,
        0x7249,0x9001,0x9001,0x9001,
        0x8001,0x8001,0x4002,0x3ffc,
        0x0000,0x4000,0x6000,0x7000,/*  arrow箭头 */
        0x7800,0x7c00,0x7e00,0x7f00,
        0x7f80,0x7e00,0x7c00,0x4600,
        0x0600,0x0300,0x0300,0x0180,
    };
    kind = (Mskind>>1)%2;  /* 手形还是箭头*/
    full = Mskind%2;       /* 是否空心*/
    for(i=0;i<16;i++)
    {
         j=jj=15;
         while(UpNum[kind][i]!=0)
         {
             up[i][j]=UpNum[kind][i]%2;
             j--;
             UpNum[kind][i]/=2;
         }
         while(DownNum[kind][i]!=0)
         {
             down[i][jj--]=DownNum[kind][i]%2;
             DownNum[kind][i]/=2;
         }
         for(k=j ;k>=0;k--)  up  [i][k]=0;
         for(k=jj;k>=0;k--)  down[i][k]=0;
         for(k=0;k<16;k++)
         {   /*四种组合方式*/
             if(up[i][k]==0&&down[i][k]==0)
             {   /* 是否为空心*/
                 if(full) mouse_draw[i][k]=1;
                 else     mouse_draw[i][k]=2;
             }
             else if(up[i][k]==0&&down[i][k]==1)
             {
                 if(full) mouse_draw[i][k]=2;
                 else     mouse_draw[i][k]=1;
             }
             else if(up[i][k]==1&&down[i][k]==0) mouse_draw[i][k]=3;
             else                                mouse_draw[i][k]=4;
         }
    }
    if(kind==1) mouse_draw[1][2]=0;/*特殊点*/
    else        mouse_draw[1][2]=0;
}
void MsOn(int x,int y)
{   /* 鼠标光标显示*/
    int  i,j;
    int  color[2];
    color[0]=Mskind>>2;
    color[1]=Mskind>>6;
    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
        {
            pixel_save[i][j]=getpixel(x+j,y+i);/* 保存原来的颜色*/
            if(mouse_draw[i][j]==1)
               putpixel(x+j,y+i,color[0]);
            else if(mouse_draw[i][j]==2)       /* 画鼠标*/
               putpixel(x+j,y+i,color[1]);
        }
    }
}
void MsOff(int x,int y)
{   /* 隐藏鼠标*/
    int  i,j,color;
    for(i=0;i<16;i++)
    for(j=0;j<16;j++)
    {
         if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)  continue;
         color=getpixel(x+j,y+i);
         putpixel(x+j,y+i,color^color);     /* 原位置异或消去*/
         putpixel(x+j,y+i,pixel_save[i][j]);/* 还原原背景*/
    }
}
int Msread(int *px,int *py,int *buttons)
{  /* 读取鼠标及键盘状态*/
    int    but_old=*buttons;
    int    x_old=*px,y_old=*py;
    struct time t;
    struct date d;/* 当前时间信息*/
    do{
         if(kbhit()) return bioskey(0);
         regs.x.ax=3;
         int86(0x33,&regs,&regs);
         *px=regs.x.cx;
         *py=regs.x.dx;
         *buttons=regs.x.bx;
    }while(*px==x_old&&*py==y_old&&*buttons==but_old);
    MsOff(x_old,y_old);
    but_old=*buttons;
    x_old=*px;
    y_old=*py;
    MsOn(x_old,y_old);
    return -1;
}
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);  /* 若在预定区域就返回 1 */
}
int End()
{
    closegraph();
    exit(0);
}

你可能感兴趣的:(职场,鼠标,休闲)