Monday, July 18, 2011

drag and drop a box in c++ using cursor

hello everyone,today im going to show you something you never expected from c++.
this is just a prototype which can be use in many projects to make them more interactive

but,before reading this post please read me as this post use cursor example.without cursor knowledge you will not understand the program.
gif animation of drag and drop
download exe + sourcecode
so the program is like this
there is a while loop running and will only break if any key is pressed using kbhit()

when a user right clicks and the position of the cursor is inside the box then the variable "move" is set to "yes"

and another condition after that check if the variable "move" is set to "yes" or not
if "yes" then the program checks that has the mouse moved or not
if the mouse also moved then it calculates and moves the box accordingly

this is done by taking a difference in position of cursor and box and if cursor has moved the difference is added to the cursor position and set to the box position.

that's all going on inside the program.
#include<graphics.h> #include<conio.h> #include<process.h> #include<stdio.h> #include<dos.h> #include<stdlib.h>
union REGS in,out; struct mouse{int x,y,b;}; void mouse_ini(); int mouse_show(); int mouse_hide(); void mouse_get(mouse *); void mouse_set(int,int); void mouse_restrict(int,int,int,int); void main() {
/*---graphics initialization request auto detection */
int gdriver=DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "path to bgi folder on computer"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error-occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } mouse_ini(); /*-actual program from here--*/ setbkcolor(56); setfillstyle(SOLID_FILL,2); setcolor(63); /*setting for initial position and width,height*/ int x=50,y=50,w=50,h=50,xdiff,ydiff,tmpx,tmpy; mouse_hide(); bar(x,y,x + w,y + h); mouse_show(); char move; mouse mus; do{ mouse_get(&mus); if(move == 'n' && mus.b == 1 && mus.x > x && mus.x < x + w && mus.y > y && mus.y < y + h) { xdiff = mus.x - x; ydiff = mus.y - y; move = 'y'; } else if(mus.b == 0) move = 'n'; mouse_get(&mus); if(move == 'y') { tmpx = x; tmpy = y; x = mus.x - xdiff; y = mus.y - ydiff; if(tmpx != x || tmpy != y) { mouse_hide(); cleardevice(); bar(x,y,x + w,y + h); mouse_show(); } } else outtextxy(10,10,"click the rectangle and drag it,press any key from keyboard to exit"); }while(!kbhit()); //while keyboard is not hit } /*to initialize the mouse when program starts*/ void mouse_ini() { if(!mouse_show()) { printf("error in initializing mouse"); printf("Press any key to halt:"); getch(); exit(1); } else mouse_restrict(0,0,getmaxx(),getmaxy()); } /*to enable cursor or show cursor to user*/ int mouse_show() {; return int86(51,&in,&out); } /*to disable cursor or hide cursor to user*/ int mouse_hide() {; return int86(51,&in,&out); }
/*get current mouse information b for button x for x-axis u for y-axis */
void mouse_get(mouse *mus) {; int86(51,&in,&out); mus->b=out.x.bx; mus->; mus->y=out.x.dx; }
/*when you want to get the mouse position and button use this method mouse mus; mouse_get(&mus); */ /*set new position for the cursor*/
void mouse_set(int x,int y) {;; in.x.dx=y; int86(51,&in,&out); } /*restrict the mouse pointer between the specific positions*/ void mouse_restrict(int x_left,int y_top,int x_right,int y_bottom) {;; in.x.dx=x_right; int86(51,&in,&out);;; in.x.dx=y_bottom; int86(51,&in,&out); }

Labels: , , , , , , , , ,


At July 18, 2011 at 9:27 PM , Blogger raj said...

good keep going

At July 19, 2011 at 5:35 AM , Blogger kuldeep dhaka said...

thanx raj....


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home