cdosmouse-positionborland-c

Trying to set mouse cursor possition in borland C


I have a program which prints the current mouse cursor position into the console.

I want to create bounds in which the mouse can go. ( 10,10) and (20,20)

So here is my code so far:

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int x,y;

struct REGPACK reg;

void getmousepos()
{
    reg.r_ax=0x03;
    intr(0x33,&reg);
    x=reg.r_cx/8;
    y=reg.r_dx/8;
}

int main()
{
    clrscr();
    _setcursortype(_NOCURSOR);
    reg.r_ax=0x01;
    intr(0x33,&reg);

    do
    {
        getmousepos();
        printf("                        ");
        printf("                        ");
        window(1,1,79,24);
        printf("Current position : %3d, %3d",x,y);

        if(x < 10)
        //set the x cursor possition to 10
        if(x > 20)
        //set the x cursor possition to 20
        if(y < 10)
        //set the y cursor to 10
        if(y > 20)
        //set the y cursor to 20

    }while(!kbhit());

    return 1;
}

Is there any way of setting reg.r_cx and reg.r_dx registers with the cordinates i want, and then calling intr() with the proper interrupt ?


Solution

  • You do that simply the same way as you have done it in getMouse():

    void setMousePosition(int x, int y)
    {
      reg.r_ax=0x04;
      reg.r_cx = x;
      reg.r_dx = y;
      intr(0x33,&reg);
    }
    

    Alternative you can use range limiter:

    void setMouseHorizontalRange(int min, int max)
    {
      reg.r_ax=0x07;
      reg.r_cx = min;
      reg.r_dx = max;
      intr(0x33,&reg);
    }
    
    void setMouseVerticalRange(int min, int max)
    {
      reg.r_ax=0x08;
      reg.r_cx = min;
      reg.r_dx = max;
      intr(0x33,&reg);
    }