Mouse programming in C
hi i m doing project with c and i want to get a mouse programing help me
Replies
-
Kaustubh KatdareRe: c programaing
Unless you tell us what you need, we cannot help you. Help us help you! π -
Prasad AjinkyaRe: c programaing
Prabhat, check out Let us C by Kanetkar, he has given some sample programs for interfacing with a mouse through C language. -
prabhat kumarRe: c programaing
hi i chek out kanetkar but not give more deeply.any stnd. book 4 this.kidakakaPrabhat, check out Let us C by Kanetkar, he has given some sample programs for interfacing with a mouse through C language. -
Kaustubh KatdareRe: c programaing
Maybe you could post your problem (along with the code) here for others to see and help you. I think it will help. -
friendster7here is few of the programs which will help you..take a look at this
Journey begins...
Our first program checks to see if a mouse driver is loaded or not.Just to remind you,mouse driver is a program that senses the presence of the mouse and understands signals coming from the mouse port before it translates these signals into relevant actions.1) Program to find whether mouse driver is loaded or not #include
The above program declares two variables of type union REGS.union REGS, which is declared in dos.h,contains two structures(struct WORDREGS x, struct BYTEREGS h).These two structures contain some 1-byte long and 2-byte long variables which indirectly represent CPU's registers. By placing 0 (sub-function) in ax register and invoking mouse interrupt(33h),we can check whether mouse driver is loaded or not.we used int86() function to invoke the interrupt.int86() takes 3 arguements:interrupt no and two union REGS type variables. If mouse driver is not loaded,it returns 0 in ax register.All return values are accessed using 'o' i.e why we have o.x.ax==0 in if statement.main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) printf("No Mouse Available....."); else printf("Mouse Available......"); }
2) Program to show the mouse pointer
Our first program only reported mouse driver loaded or not.Even if driver is avilable,we have no signs of mouse pointer.To view mouse pointer,we have to use sub-function 1.Look at the program presented below.
#include
The above program is same as our first program except for the last few lines.We have placed 1 in ax register and invoked mouse interrupt to see the pointer.Since we are in text mode,our pointer is a rectangular box.We can observe an arrow if we switch to graphics mode.main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available....."); exit(); } i.x.ax=1; int86(0x33,&i,&o); }
3) Program to hide the mouse pointer
Mouse cursor still remains,even after our program is terminated.Look at the following program.#include
The above program uses sub-function 2 and invokes mouse interrupt to hide the mouse pointer.This function is quite a useful function,when writing programs that draw a line or rectangle as we move the mouse. While writing those programs we don't want mouse pointer to erase what we draw hence we hide the pointer.main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available....."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(24,23); printf("Press any key to hide mouse cursor..."); getch(); i.x.ax=2; int86(0x33,&i,&o); gotoxy(10,23); printf("Mouse cursor is hidden !! Press any key to terminate the program ..."); getch(); }
4) Program to show the position of the mouse pointer
For writing any useful program with mouse support,we need to know the (x,y) co-ordinates of the mouse position.The below program prints the position of the mouse,as we mouse the mouse.
#include
In the above program we have a while loop.This loop continues until a key is hit.In loop,we used sub-function 3 and invoked mouse interrupt. Sub-function 3 returns X->co-ordinate in cx register and Y->co-ordinate in dx register.printf statements prints x and y co-ordinates as long as the loop continues.Maximum screen resolution for mouse in text mode is 640x200 and in graphics mode is 640x480.main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available..."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(25,23); printf("Press any key to exit..."); while(!kbhit()) { i.x.ax=3; int86(0x33,&i,&o); gotoxy(2,2); printf("x->co-ordinate=%d \n y->co-ordinate=%d ",o.x.cx,o.x.dx); } i.x.ax=2; int86(0x33,&i,&o); }
5) Program to print which mouse button is pressed
Knowing which button is pressed is very important task.The program below prints which button is pressed as soon as we press any button.
#include
The above program is same as the previous program except we have little extra in while loop.In while we used the same sub-function 3 and invoked mouse interrupt.This subfunction even returns button press information in bx register.Entire button press information is stored in the first 3 bits of the bx register.So we ANDED bx with 7 to separate the first 3 bits and stored them in button variable.main() { union REGS i,o; int button; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available...."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(24,23); printf("Press any key to exit...."); while(!kbhit()) { i.x.ax=3; int86(0x33,&i,&o); button=o.x.bx&7; gotoxy(23,11); switch(button) { case 1: printf("Left button pressed "); break; case 2: printf("Right button pressed "); break; case 4: printf("Middle button pressed "); break; case 3: printf("Left and Right buttons pressed "); break; case 5: printf("Left and Middle buttons pressed "); break; case 6: printf("Right and Middle buttons pressed "); break; case 7: printf("All the three buttons pressed "); break; default: printf("No button pressed...."); } } i.x.ax=2; int86(0x33,&i,&o); }
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.
6) 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.
#include
In the above program,we use sub-function 4 to set the pointer's position. We set the X->co-ordinate by placing a value in the cx register and Y->co-ordinate by placing a value in the dx register.main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available"); exit(); } i.x.ax=1; int86(0x33,&i,&o); i.x.ax=3; int86(0x33,&i,&o); gotoxy(1,1); printf("Current Position:x=%d y=%d ",o.x.cx,o.x.dx); gotoxy(15,23); printf("Press any key to set the mouse pointer to (150,100)..."); getch(); i.x.ax=4; i.x.cx=150; i.x.dx=100; int86(0x33,&i,&o); gotoxy(15,23); printf("Cursor is set ... press a key to exit "); getch(); }
7) Program to switch to graphics mode
The program below switches text mode to graphcs mode.After executing this program,observe the mouse pointer.Now,it's in an arrow shape. Try to execute program-4 in graphics mode and observe the maximum screen resolution for mouse is increased to 640x480.
#include
In the above program,we used standard library function initgraph() to initialize graphics system.This function takes 3 arguments;graphics driver, graphics mode,path to the driver.By using DETECT,we tell the function to select a suitable driver by itselt.When DETECT is used,no need to assign anything to graphics mode.Path is null since the driver files are located in the current directory.This function initializes graphics system and when the program terminates we come to text mode by using restorecrtmode() function.#include main() { int gd=DETECT,gm; union REGS i,o; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Avaialable.."); restorecrtmode(); exit(); } i.x.ax=1; int86(0x33,&i,&o); outtextxy(100,400,"Mouse Pointer in graphics mode!!Press any key to exit"); getch(); i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); }
8) Program to restrict the mouse pointer within a boundary
Sometimes,in our program,we need to restrict the mouse pointer with in a screen boundary.In order to do that,we need to specify the top,left co-ordinates as well bottom,right co-ordinates.Sub-functions 7,8 are used to limit the pointer within a boundary.Look at the following program.
#include
In the above program,sub-fuction 7 is used to specify two x->co-ordinates and sub-function 8 is used to specify two y->co-ordinates and these co-ordinates form a rectangular boundary within which mouse is restricted.#include main() { union REGS i,o; int gd=DETECT,gm; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { restorecrtmode(); printf("No Mouse Available....."); exit(); } rectangle(99,49,501,151); i.x.ax=1; int86(0x33,&i,&o); i.x.ax=7; i.x.cx=100; i.x.dx=500; int86(0x33,&i,&o); i.x.ax=8; i.x.cx=50; i.x.dx=150; int86(0x33,&i,&o); while(!kbhit()) ; i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); }
9) Free-hand drawing
If you ever wonder,how pencil tool in paint works.Then,the following program shows how it can be written in C.The following program makes use of some of the sub-function,which we already disussed above,and shows how they can be used to write useful programs like free-hand drawing.Just,go through the following program.
#include
There is nothing in this program to explain.Since you have gone through the program,you must have understood the logic.#include union REGS i,o; main() { int gd=DETECT,gm,button,x1,y1,x2,y2; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } outtextxy(230,400,"Press any key to exit...."); while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); x2=x1; y2=y1; while(button==1) { hide_mouse(); line(x1,y1,x2,y2); x1=x2; y1=y2; get_mouse_pos(&x2,&y2,&button); } } 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; }
10) 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.
#include
#include #include 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 When drawing lines interactively,we must make sure that the currently drawn line doesn't wipe off already drawn lines when it intersects them.In order to do that,the above program uses save and restore functions.These two functions captures and restores screen contents.
Source: #-Link-Snipped-# -
prabhat kumarthanks for example given by u.
-
rajivnayanhi
i am working on a project which combine hardware and software.
i have to control a robotic arm with the help of mouse through parallel port. it will be having 3 dimensional movement.i m not using any microcontroller, just motor driving IC. parallel port ll be taken as input for IC to drive motor. i need some help guysπππ -
shiva1533its really good.try to do the program an u really got the knowlegeπ
-
rajivnayani did the programming part...
now interfacing with d robot arm is left... -
sureshHARDIYAHello there, I am working on a project of C. I have made two buttons using bar function- "YES " and "NO". i want the program to divert to another module if yes is pressed and to another module if y is pressed. please help me with the code. i m new to programming with c in mouse.
-
sureshHARDIYAi am doing a small project in C. and have made a button to go to next module. when i click it, the program diverts to next module and when i click back,,,it does not divert to original module. please suggest,,how to do...
-
PraveenKumar Purushothaman
Please start a new thread. And close this thread #-Link-Snipped-#! πrajivnayanhi
i am working on a project which combine hardware and software.
i have to control a robotic arm with the help of mouse through parallel port. it will be having 3 dimensional movement.i m not using any microcontroller, just motor driving IC. parallel port ll be taken as input for IC to drive motor. i need some help guysπππ
You are reading an archived discussion.
Related Posts
hi i m forget my password and i want to break my own password.i m using service pack 2.and i have no guest acount help.
Hi. I'm starting the planning on my Senior Design project, and am wondering if anyone can recommend a good microcontroller.
My project is basically going to have two ultrasonic sensors...
hello everybodyl!
It's a GREAT forum ever seen before π
I heard a person say that if some vegetable oil is added to diesel while filling the fuel tank then the efficiency of the engine increases.
I confess, this piece...
π
same one can send me a java script code for inlarge image on mouse over......