A
A
Andrey2019-04-19 17:33:11
C++ / C#
Andrey, 2019-04-19 17:33:11

How to implement "CLICK" in C++ DOS?

Programming in DOS in C++.
At a low level, I process the mouse with 33 interrupts.
There is a console that displays "buttons" (just filled pixels with the fillelips() function).
When I click on the "button", a function is executed in the console (for example, circle() ) and a circle is drawn in the console.
In principle, I can imagine how to do all this, but it is necessary to implement such a concept as a "CLICK", that is, LMB pressed and released, only this is considered a click.
I hope I explained clearly)

main.cpp
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <drawing.h>

int main () {

  if (initMouse() == 1) {
    return 0;
  }
  getch();
  int gdriver = DETECT, gmode;
  initgraph (&gdriver, &gmode, "C:\\BORLANDC\\BGI");
  if (graphresult() != grOk) {
    cout << "ERROR. DRIVER NOT DETECTED. PRESS ANY KEY TO EXIT..." << endl;
    return 0;
  }

  showMouse();
  int mX=0, mY=0, mB, prevX, prevY, barX, barY, sizeCursor = 3, colorBrush = 9, bkColor = 9;

  while (1) {			
    if (kbhit()) break;
    prevX=mX;
    prevY=mY;
    mouseState(mX, mY, mB);
    gotoxy (1, 1);
    
    if (mX>=150 && mY>=30) {
      if (mB == 0) {
        setMouseCursor(CURSOR_DEFAULT);
      }
      else if (mB == 1) {
        setMouseCursor(CURSOR_BRUSH);
        brushActive(mX, mY, prevX, prevY, sizeCursor, colorBrush);
      }
      else if (mB == 2) {
        setMouseCursor(CURSOR_ERASER);
        eraserActive(mX, mY, sizeCursor);
      }
      else if (mB == 4) {
        if (colorBrush != 15) {
          colorBrush++;
        }
        else {
          colorBrush = 0;
        }
        
      }
    }

  }

  closegraph ();
  return 0;
}

drawing.h
#ifndef DRAWING_H
#define DRAWING_H
#include <dos.h>
#include <conio.h>
#include <stdio.h>

struct REGPACK regs;

unsigned CURSOR_BRUSH[17] = {0x001C, 0x003F, 0x0073, 0x00B3, 0x01BF, 0x03DE, 0x07E4, 0x0BF8, 0x0BF0, 0x1DE0, 0x1E40, 0x3F80, 0x2E00, 0x5800, 0x6000, 0x6000, 0x0F01};
unsigned CURSOR_ERASER[17] = {0x0FC0, 0x1BE0, 0x3DF0, 0x7EF8, 0xFF70, 0x7FBE, 0x3FD1, 0x1FE1, 0x0FDE, 0x07BC, 0x0378, 0x01F0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0004};
unsigned CURSOR_DEFAULT[17] = {0x0000, 0x0780, 0x0FC0, 0x18E0, 0x3060, 0x2060, 0x0060, 0x0060, 0x00E0, 0x01C0, 0x0300, 0x0300, 0x0300, 0x0000, 0x0300, 0x0300, 0x0707};

void intrMouse(int param) { 
    regs.r_ax = param;
    intr(0x33, &regs);
}

int initMouse() {
  intrMouse(0);
  if (regs.r_ax == 0) {
    printf ("ERROR. MOUSE NOT FOUND. PRESS ANY KEY TO EXIT...");
    return 1;
  }
  else {
    printf ("MOUSE IS PRESENT, AND HAS %d BUTTON", regs.r_bx);
    return 0;
  }
  
}

void showMouse(void) {
  intrMouse(1);
}
void hideMouse(void) {
  intrMouse(2);
}

void mouseState(int &mX, int &mY, int &mB) {
  intrMouse(3);
  mX = regs.r_cx;
  mY = regs.r_dx;
  mB = regs.r_bx;
}

void mousePosition(int mX, int mY) {
  regs.r_cx = mX;
  regs.r_dx = mY;
  intrMouse(4);
}

void brushActive (int &mX, int &mY, int &prevX, int &prevY, int sizeCursor, int &colorBrush) {
  hideMouse();
  setcolor(colorBrush);
  line(prevX, prevY, mX, mY);
  showMouse();
}

void eraserActive (int &mX, int &mY, int sizeCursor) {
  int bkcolor = getbkcolor();
  hideMouse();
  for (int i = mX - sizeCursor; i <= mX + sizeCursor; i++)
    for (int j = mY - sizeCursor; j <= mY + sizeCursor; j++) {
      putpixel (i,j,bkcolor);
    }
  showMouse();
}

void setMouseCursor(unsigned styleCursor[17]) {
  unsigned newStyleCursor[32];
  for(int i=0; i<16; i++) {
    newStyleCursor[i] = (~styleCursor[i]);
    newStyleCursor[i+16] = styleCursor[i];
  }
  regs.r_bx = styleCursor[16]/0x100;
  regs.r_cx = styleCursor[16]%0x100;
  regs.r_es = FP_SEG(&newStyleCursor);
  regs.r_dx = FP_OFF(&newStyleCursor);
  intrMouse(9);
}

#endif

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2019-04-19
@rPman

According to your mind, it is enough for you to track the event of raising the mouse button and consider it a click, but it is more correct to record the dates of the last pressing and releasing the button (for each button there are a couple of variables) and if the difference between them is less than a certain threshold, then consider that there is a click

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question