Introduction
Mouse has revolutionized the computer world. In GUI mouse is very essential for user interaction. it is a simple input device which allows you to point and execute interface in a random and faster manner than the keyboard which provides sequential interface. Mouse Programming is a topic which every C programmer needs to have in his toolbox to have a cutting edge.It will be used almost everywhere.
Requirement
Turbo C++ IDE installed in folder C:\TC.Fundamental Knowledge
First thing you must know how to tell a mouse to do anything. In actual we do not communicate with mouse directly but through the driver provided. We use "Interrupts" to get access to this driver. Each device provided by a computer has a unique port which is a hexadecimal value which is designed to be machine independent enhancing portability of program. We access these ports in our program. Mouse has port 0X33 attached to it. We also make use of address registers. These are basically UNION of type REGS defined in "dos.h". We use two registers to communicate to a device driver one for input and one for output. We send value to device driver through the input register and receive information in it embedded in output register.
AX Register
We can access various mouse functions using different values of AX input Register and passing those values to mouse port using a interrupt.
The Functions are listed below - Here AX, BX, CX and DX are members of UNION REGS.
Input --> Functions Performed with Return Param if any
- AX=0 --> Gets Mouse status (AX Value = FFFFh support is available. AX Value = 0 ,support is not available.)
- AX=1 --> Show Mouse Pointer.
- AX=2 --> Hide mouse pointer.
- AX=3 --> Mouse Position. (CX = Mouse X Coordinate, DX = Mouse Y Coordinate.)
- AX=3 --> Mouse Button Press (BX = 0 No Key Is Pressed, BX = 1 Left Button is Pressed, BX = 2 Right Button is Pressed, BX = 3 Centre Button is Pressed.)
- AX=7
CX=MaxX1
DX=MaxX2 --> Set Horizontal Limit. - AX=8
CX=MaxX1
DX=MaxX2 --> Set Vertical Limit.
Detecting Mouse
Our first program checks to see if a mouse driver is loaded or not and that mouse programming is supported or not. Mouse driver is a program that senses the presence of the mouse and understands a signal coming from the mouse port before it translates these signals into relevant actions.
If somehow mouse fails to initialise you should always make sure that either program terminates or employ a error handling approach.
Code:
#include <dos.h> union REGS in, out; void detect_mouse () { in.x.ax = 0; int86 (0X33,&in,&out); //invoke interrupt if (out.x.ax == 0) printf ("\nMouse Failed To Initialize"); else printf ("\nMouse was Succesfully Initialized"); } int main () { detect_mouse (); getch (); return 0; }
Showing Mouse
- Mouse can be used both in text mode and graphic mode.
- In text mode it looks like a square while in graphics mode it looks like a pointer.
Mouse Programming in Text Mode
Code:
#include <dos.h> union REGS in, out; void showmouse_text () { in.x.ax = 1; int86 (0X33,&in,&out); } int main () { detect_mouse (); showmouse_text (); getch (); return 0; }
Code:
#include <dos.h> #include <graphics.h> union REGS in, out; void showmouse_graphics () { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); in.x.ax = 1; int86 (0X33,&in,&out); getch (); closegraph (); } int main () { detect_mouse (); showmouse_graphics (); getch (); return 0; }
Hiding Mouse
Code:
void hide_mouse () { in.x.ax = 2; int86 (0X33,&in,&out); }
Program To Print Which Mouse Button Is Pressed
Code:
#include <dos.h> #include <graphics.h> union REGS in, out; void detect () { int button; while (!kbhit () ) { in.x.ax = 3; int86 (0X33,&in,&out); button=out.x.bx&7 switch(button) { case 1: print(“left button pressed\n”); break; case 2: print(“right button pressed\n”); break; case 4: print(“middle button pressed\n”); break; case 3: print(“left and right button pressed\n”); break; case 5: print(“left and middle button pressed\n”); break; case 6: print(“right and middle button pressed\n”); break; case 7: print(“all the three buttons pressed\n”); break; default: print(“No button pressed\n”); } delay (200); // Otherwise due to quick computer response 100s of words will get print } } int main () { detect_mouse (); showmouse_text (); detect (); hide_mouse (); getch (); return 0; }
If the first bit's value is 1 then the left button is pressed, if the value is 0 then it is not pressed. If the second bit's value is 1 then the right button is pressed, if value is 0 then it is not pressed. If the last bit's value is 1 then the middle button is pressed, if value is 0 then it is not pressed.
Program To Show The Position Of Mouse Pointer
Code:
#include <dos.h> #include <graphics.h> union REGS in, out; void detect () { while (!kbhit () ) { int x,y; in.x.ax = 3; int86 (0X33,&in,&out); if (out.x.bx == 1) { x = out.x.cx; y = out.x.dx; printf ("\nLeft || X - %d Y - %d", x, y); } delay (200); // Otherwise due to quick computer response 100s of words will get print } } int main () { detect_mouse (); showmouse_text (); detect (); hide_mouse (); getch (); return 0; }
Program To Set The Position Of The Mouse Pointer On The Screen
Sometimes we need to set the position of the mouse pointer, just as we set the position of the keyboard's cursor using gotoxy().The following program sets the pointer to (x=150,y=100) positon on the screen.
Code:
#include <dos.h> #include <graphics.h> union REGS in, out; void set() { in.x.ax=4; in.x.cx=150; in.x.dx=100; int86(0x33,&in,&out); }
Program To Restrict The Mouse Pointer Within A Boundary
Code:
#include <dos.h> #include <graphics.h> union REGS in, out; void restrict (int x1,int y1,int x2, int y2) { in.x.ax = 7; in.x.cx = x1; in.x.dx = x2; int86 (0X33,&in,&out); in.x.ax = 8; in.x.cx = y1; in.x.dx = y2; int86 (0X33,&in,&out); } int main () { detect_mouse (); showmouse_text (); restrict (100,100,500,500); // Change values here to create different mouse movement space. detect (); hide_mouse (); getch (); return 0; }
Free- Hand Drawing
If you ever wonder, how pencil tool in paint works. Then, he following program shows how it can be written in C. The following program makes use of some of the sub function, which we already discussed above, and shows how they can be used to write useful programs like free-hand drawing. Just, go through the following program.
Code:
#include <graphics.h> #include <dos.h> union REGS in,out; show_mouse() { i.x.ax=1; int86(0x33,&i,&o); } hide_mouse() { i.x.ax=2; int86(0x33,&i,&o); } get_mouse_pos(int *x,int *y,int *button) { i.x.ax=3; int86(0x33,&i,&o); *x=o.x.cx; *y=o.x.dx; *button=o.x.bx&1; } main() {
Line drawing
Following program shows how to draw a line interactively using mouse. If you know how to draw a line,there is no big deal in developing a program that draws a square.So,carefully observe and understand the following program.
Code:
#include <graphics.h> #include <alloc.h> #include <dos.h> union REGS i,o; char far *p; main() { int gd=DETECT,gm,button,x1,y1,x2,y2,prevx2,prevy2,x,y; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); if(button==1) { hide_mouse(); x2=x1; y2=y1; save(x1,y1,x2,y2); line(x1,y1,x2,y2); prevx2=x2; prevy2=y2; get_mouse_pos(&x2,&y2,&button); while(button==1) { if(x2!=prevx2 || y2!=prevy2) { setcolor(BLACK); line(x1,y1,prevx2,prevy2); x=x1<prevx2?x1:prevx2; y=y1<prevy2?y1:prevy2; restore(x,y); setcolor(WHITE); save(x1,y1,x2,y2); line(x1,y1,x2,y2); prevx2=x2; prevy2=y2; } get_mouse_pos(&x2,&y2,&button); } farfree(p); } } restorecrtmode(); } show_mouse() { i.x.ax=1; int86(0x33,&i,&o); } hide_mouse() { i.x.ax=2; int86(0x33,&i,&o); } get_mouse_pos(int *x,int *y,int *button) { i.x.ax=3; int86(0x33,&i,&o); *x=o.x.cx; *y=o.x.dx; *button=o.x.bx&1; } save(int x1,int y1,int x2,int y2) { unsigned area; area=imagesize(x1,y1,x2,y2); p=farmalloc(area); if(p==NULL) { restorecrtmode(); printf("No Memory..."); exit(); } getimage(x1,y1,x2,y2,p); } restore(int x1,int y1) { putimage(x1,y1,p,OR_PUT); farfree(p); }
No comments:
Post a Comment