February 16, 2018

Mouse programming in Turbo C/C++



   In GUI environment, mouse is very important for user interaction. But MS DOS is non-graphical command line based operating system. However it supports mouse interfacing system calls. DOS implements mouse interfacing subsystem with the installed mouse driver through interrupt 0x33


Mouse can be used in text mode as well as in graphics mode. Usually it is used in graphics mode. To use mouse in graphics mode first change over to graphics mode. Learn graphics programming in dos mode from here


The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 0x33. The functions are listed bellow  


Interrupt Service Purpose
0x33
 
0
 
 Reset mouse and get status
 Call with AX = 0
 Returns:  AX = FFFFh If mouse support is available
                AX = 0 If mouse support is not available 
0x33 1
 Show mouse pointer
 Call with AX = 1 
 Returns: Nothing
0x33 2
 Hide mouse pointer
 Call with AX = 2
 Returns: Nothing
0x33 3
 Get mouse position and button status
 Call with AX = 3
 Returns: BX = mouse button status
 Bit   Significance
  0     button not pressed
  1     left button is pressed
  2     right button is pressed
  4     center button is pressed      
 CX = x coordinate
 DX = y coordinate
0x33 4
 Set mouse pointer position 
 Call with AX = 4
 CX = x coordinate
 DX = y coordinate
 Returns: Nothing
0x33 7
 Set horizontal limits for pointer
 Call with AX = 7
 CX = minimum x coordinate
 DX = maximum x coordinate
 Returns: Nothing
0x33 8
 Set vertical limits for pointer
 Call with AX = 8
 CX = minimum y coordinate
 DX = maximum y coordinate
 Returns: Nothing
   

DOS graphics mode mouse interfacing program



     #include <iostream.h>
    #include <conio.h>
    #include <graphics.h>
    #include <dos.h>
    #include <stdlib.h>

    int MouseXpos, MouseYpos, MouseBtn;
    union REGS in,out;

    int InstallMouse()
    {
        // Initialize the mouse
        in.x.ax = 0;
        int86(0x33,&in,&out);
        return out.x.ax;
    }
    int ShowMouse()
    {
        // Show mouse pointer
        in.x.ax = 1;
        int86(0x33,&in,&out);
        return 1;
    }
    int HideMouse()
    {
        // Hide mouse pointer
        in.x.ax = 2;
        int86(0x33,&in,&out);
        return 1;
    }
    void GetMouseStatus()
    {
        // Get mouse position and button status
        in.x.ax = 3;
        int86(0x33,&in,&out);
        MouseXpos = out.x.cx;
        MouseYpos = out.x.dx;
        MouseBtn = out.x.bx;
    }
    int GetButton()
    {
        // Get mouse button status
        return MouseBtn;
    }

    int GetPosX()
    {
        // Get mouse X-Coordinate
        return MouseXpos;
    }

    int GetPosY()
    {
        // Get mouse Y-Coordinate
        return MouseYpos;
    }

    void SetMousePosi(int &xpos, int &ypos)
    {
        // Set mouse pointer position 
        in.x.ax = 4;
        in.x.cx = xpos;
        in.x.dx = ypos;
        int86(0x33,&in,&out);
    }

    void RestrictMousePtr(int x1, int y1, int x2, int y2)
    {
        // Set horizontal limits for pointer
        in.x.ax = 7;
        in.x.cx = x1;
        in.x.dx = x2;
        int86(0x33,&in,&out);

        // Set vertical limits for pointer
        in.x.ax = 8;
        in.x.cx = y1;
        in.x.dx = y2;
        int86(0x33,&in,&out);

    }

    void main()
    {
        int gd=DETECT, gm, errorcode;
        int xpos, ypos;

        clrscr();

        if(!InstallMouse())
        {
            cout<<"\n\n\t Mouse driver not loaded.";
            cout<<"\n\n\n\t Press any key to exit...";
            getch();
            exit(1);
        }

        cout<<"\n\n\t Mouse driver detected.";
        cout<<"\n\n\n\t Press any key to continue.";
        getch();

         // initialize graphics and local variables
        initgraph(&gd,&gm,"c://turboc3//bgi");

        // read result of initialization
        errorcode = graphresult();

        if (errorcode != grOk)// an error occurred
        {
            cout<<"Graphics error :: "<<grapherrormsg(errorcode);
            cout <<"\n press any key to halt: ";
            getch();
            exit(1);// terminate with an error code
        }

        xpos = getmaxx()/2;
        ypos = getmaxy()/2;

        // draw rectangle

        setcolor(4);
        rectangle(100,100,getmaxx()-100,getmaxy()-100);
        RestrictMousePtr(100,100,getmaxx()-100,getmaxy()-100);
        SetMousePosi(xpos,ypos);
        ShowMouse();
        do{
            GetMouseStatus();
            gotoxy(5,3);
            cout<<"Mouse X-Coordinate : "<<GetPosX();
            gotoxy(5,4);
            cout<<"Mouse Y-Coordinate : "<<GetPosY();
            gotoxy(5,5);
            cout<<"Button Pressed     : "<<GetButton();
            gotoxy(30,5);
            cout<<"                                  ";
            gotoxy(30,5);

            switch(GetButton())
            {
                case 0:
                cout<<"(Button not pressed)";
                break;
                case 1:
                cout<<"(Left button pressed)";
                break;
                case 2:
                cout<<"(Right button pressed)";
                break;
                case 4:
                cout<<"(Middle button pressed)";
                break;
            }
        }while(!kbhit());
        HideMouse();
        closegraph();
    }


Output

Graphics Mouse - Output

Graphics Mouse - Output

Graphics Mouse - Output



 DOS text mode mouse interfacing program




     #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>

    int MouseXpos, MouseYpos, MouseBtn;
    union REGS in,out;

    int InstallMouse()
    {
        // Initialize the mouse
        in.x.ax = 0;
        int86(0x33,&in,&out);
        return out.x.ax;
    }
    int ShowMouse()
    {
        // Show mouse pointer
        in.x.ax = 1;
        int86(0x33,&in,&out);
        return 1;
    }
    int HideMouse()
    {
        // Hide mouse pointer
        in.x.ax = 2;
        int86(0x33,&in,&out);
        return 1;
    }
    void GetMouseStatus()
    {
        // Get mouse position and button status
        in.x.ax = 3;
        int86(0x33,&in,&out);
        MouseXpos = out.x.cx;
        MouseYpos = out.x.dx;
        MouseBtn = out.x.bx;
    }
    int GetButton()
    {
        // Get mouse button status
        return MouseBtn;
    }

    int GetPosX()
    {
        // Get mouse X-Coordinate
        return MouseXpos;
    }

    int GetPosY()
    {
        // Get mouse Y-Coordinate
        return MouseYpos;
    }

    void SetMousePosi(int &xpos, int &ypos)
    {
        // Set mouse pointer position 
        in.x.ax = 4;
        in.x.cx = xpos;
        in.x.dx = ypos;
        int86(0x33,&in,&out);
    }

    void RestrictMousePtr(int x1, int y1, int x2, int y2)
    {
        // Set horizontal limits for pointer
        in.x.ax = 7;
        in.x.cx = x1;
        in.x.dx = x2;
        int86(0x33,&in,&out);

        // Set vertical limits for pointer
        in.x.ax = 8;
        in.x.cx = y1;
        in.x.dx = y2;
        int86(0x33,&in,&out);

    }

    void rectangle(int x,int y,int l, int b)
    {
        int i,j,k,m;
        gotoxy(x,y);
        cout<<(char)218;
        for(i=x+1;i<=l-1;i++)
        {
            gotoxy(i,y);
            cout<<(char)196;
        }
        gotoxy(l,y);
        cout<<(char)191;
        for(j=y+1;j<=b-1;j++)
        {
            gotoxy(l,j);
            cout<<(char)179;
        }
        gotoxy(l,b);
        cout<<(char)217;
        for(k=l-1;k>=x+1;k--)
        {
            gotoxy(k,b);
            cout<<(char)196;
        }
        gotoxy(x,b);
        cout<<(char)192;
        for(m=b-1;m>=y+1;m--)
        {
            gotoxy(x,m);
            cout<<(char)179;
        }
    }

   
    void main()
    {
        int xpos, ypos;

        clrscr();

        if(!InstallMouse())
        {
            cout<<"\n\n\t Mouse driver not loaded.";
            cout<<"\n\n\n\t Press any key to exit...";
            getch();
            exit(1);
        }

        cout<<"\n\n\t Mouse driver detected.";
        cout<<"\n\n\n\t Press any key to continue.";
        getch();

        clrscr();
        xpos = 200;
        ypos = 100;

        // draw rectangle

       
        rectangle(8,10,70,20);
        RestrictMousePtr(56,75,555,153);
        SetMousePosi(xpos,ypos);
        ShowMouse();
        do{
            GetMouseStatus();
            gotoxy(5,3);
            cout<<"                                     ";
            gotoxy(5,3);
            cout<<"Mouse X-Coordinate : "<<GetPosX();
            gotoxy(5,4);
            cout<<"                                     ";
            gotoxy(5,4);
            cout<<"Mouse Y-Coordinate : "<<GetPosY();
            gotoxy(5,5);
            cout<<"                                     ";
            gotoxy(5,5);
            cout<<"Button Pressed     : "<<GetButton();
            gotoxy(30,5);
            cout<<"                                  ";
            gotoxy(30,5);

            switch(GetButton())
            {
                case 0:
                cout<<"(Button not pressed)";
                break;
                case 1:
                cout<<"(Left button pressed)";
                break;
                case 2:
                cout<<"(Right button pressed)";
                break;
                case 4:
                cout<<"(Middle button pressed)";
                break;
            }
        }while(!kbhit());
        HideMouse();
       
    }

Output


Text Mode Mouse - Output

Text Mode Mouse - Output

Text Mode Mouse - Output






1 comment:

  1. wonderful application of mouse handling,thx for helping

    ReplyDelete