Mouse Programming in C Using Graphics
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> union REGS i, o; struct SREGS s; Module that checks whether Mouse is attached to port: void InitMouse() { i.x.ax=0; int86(0x33, &i, &o); return(o.x.ax); } Module that displays the current shape of Mouse Pointer. void ShowMousePtr() { i.x.ax=1; int86(0x33, &i, &o); } Module that hides the Mouse pointer from the display. void HideMousePtr() { i.x.ax=2; int86(0x33, &i, &o); } Module that outputs Mouse button status and pointer position. void GetMousePosition(int *button, int *x, int *y) { i.x.ax=3; int86(0x33, &i, &o); *button=o.x.bx; *x=o.x.cx; *y=o.x.dx; } Module that initializes Mouse Pointer position on the work area at the desired position. void SetMousePosition(int x, int y) { i.x.ax=4; o.x.cx=x; o.x.dx=y; int86(0x33, &i, &o); } Module that fixes the Mouse movement range (Window Specific) void RestrictMousePtr(int x1, int y1, int x2, int y2) { i.x.ax=7; i.x.cx=x1; i.x.dx=x2; int86(0x33, &i, &o); i.x.ax=8; i.x.cx=y1; i.x.dx=y2; int86(0x33, &i, &o); } Module that dynamically displays the position Coordinate of Mouse Pointer. void ShowMousePosition(int prevx, int prevy, int x, int y) { char xString[4], yString[10], prevxString[4], prevyString[10]; if(x>80 && x<635 && y>21 && y<460) { if(x!=prevx || y!=prevy) { settextstyle(DEFAULT_FONT, 0, 1); prevx-=81; prevy-=22; itoa(prevx, prevxString, 10); itoa(prevy, prevxString, 10); strcat(prevString, " , "); strcat(prevString, prevString); setcolor(LIGHTGRAY); outtextxy(560, 465, prevString); x-=81; y-=22; itoa(x, xString, 10); itoa(y, yString, 10); strcat(yString, " , "); strcat(yString, xString); setcolor(BLUE); outtextxy(560, 465, yString); } else { if(prevx>80 && prevx<635 && prevy>21 && prevy<460) { settextstyle(DEFAULT_FONT, 0, 1); prevx-=81; prevy-=22; itoa(prevx, prevxString, 10); itoa(prevy, prevyString, 10); strcat(prevyString, " , "); strcat(prevyString, prevxString); setcolor(LIGHTGRAY); outtextxy(560, 465, prevyString); } } } Module that changes cursor shape specific to the use. void ChangeCursor(int *shape) { i.x.ax=9; i.x.bx=0; i.x.cx=0; i.x.dx=(unsigned) shape; segread(&s); s.es=s.ds; int86x(0x33, &i, &o, &s); } int HourCursor[32]= { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }; Modules for generating different cursor-shape int HandCursor[32]= { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }; int FillCursor[32]= { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, };