Примеры использования программирования с помощью мыши на C / C ++ | Комплект 2

Опубликовано: 16 Декабря, 2021

В этой статье мы обсудим некоторые варианты использования программирования с помощью мыши:

Ограничить указатель мыши :

Указатель мыши можно ограничить конкретным прямоугольником. Идея состоит в том, чтобы создать функцию с именем restrictmouse (), которая принимает четыре параметра, которые содержат координату X и координату Y. Первая точка указывает на верхнюю часть прямоугольника, а вторая точка указывает на низ прямоугольника. Ниже приведены функции, используемые для того же:

  • initmouse (): используйте для инициализации мыши.
  • showmouse (): показывает указатель мыши на экране вывода.
  • restrictmouse (): используется для установки горизонтального и вертикального ограничения указателя мыши путем установки следующих параметров. AX = 7 для горизонтали и AX = 8 для вертикали.

Ниже представлена программа для того же:

C

// C program to restrict the mouse
// pointer
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
// Function to display the mouse
// pointer using graphics
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to restrict the mouse
// pointers
void restrictMouse( int x1, int y1,
int x2, int y2)
{
// Set Horizontal limit
in.x.ax = 7;
in.x.cx = x1;
in.x.dx = x2;
int86(0X33, &in, &out);
// Set Vertical limit
in.x.ax = 8;
in.x.cx = y1;
in.x.dx = y2;
int86(0X33, &in, &out);
}
// Driver Code
void main()
{
int status, i, gd = DETECT, gm;
// Initialize graphics
initgraph(&gd, &gm, "C:\TURBOC3\BGI" );
// Get the status of the mouse
status = initMouse();
// Check if mouse is available or not
if (status == 0)
printf ( "Mouse support "
"not available. " );
else {
showMouse();
// Draw rectangle for displaying
// the boundary
rectangle(100, 70, 400, 200);
restrictMouse(100, 70, 400, 200);
}
getch();
// Close the graphics
closegraph();
return 0;
}

C ++

// C++ program to restrict the mouse
// pointer
#include <bits/stdc++.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
using namespace std;
union REGS in, out;
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
// Function to display the mouse
// pointer using graphics
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to restrict the mouse
// pointers
void restrictMouse( int x1, int y1,
int x2, int y2)
{
// Set Horizontal limit
in.x.ax = 7;
in.x.cx = x1;
in.x.dx = x2;
int86(0X33, &in, &out);
// Set Vertical limit
in.x.ax = 8;
in.x.cx = y1;
in.x.dx = y2;
int86(0X33, &in, &out);
}
// Driver Code
void main()
{
int status, i, gd = DETECT, gm;
// Initialize graphics
initgraph(&gd, &gm, "C:\TURBOC3\BGI" );
// Get the status of the mouse
status = initMouse();
// Check if mouse is available or not
if (status == 0)
cout << "Mouse support "
<< "not available. " ;
else {
showMouse();
// Draw rectangle for displaying
// the boundary
rectangle(100, 70, 400, 200);
restrictMouse(100, 70, 400, 200);
}
getch();
// Close the graphics
closegraph();
return 0;
}


Выход:

Рисунок от руки :

В следующей программе используются некоторые подфункции, которые уже обсуждались ранее, и показано, как их можно использовать для написания полезных программ, таких как рисование от руки. Ниже приведены используемые функции:

  • initmouse (): используйте для инициализации мыши.
  • showmouse (): показывает указатель мыши на экране вывода.
  • hidemouse (): используется для скрытия мыши во время рисования.
  • getmouseposition (): выбирает текущее положение указателя и рисует линию соответственно.

Ниже представлена программа для того же:

C

// C program to perform Free Hand Drawing
// using mouse programming
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
union REGS in, out;
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
// Function to display the mouse
// pointer using graphics
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
// Set AX=2 to hide mouse
in.x.ax = 2;
int86(0X33, &in, &out);
}
// Function to get the exact position
// of the mouse
getMousePosition( int * x, int * y,
int * click)
{
in.x.ax = 3;
// Get the coordinates
int86(0x33, &in, &out);
// Update the coordinates
*x = out.x.cx;
*y = out.x.dx;
*click = out.x.bx & 1;
}
// Driver Code
void main()
{
int status, i, gd = DETECT, gm,x1,y1,x2,y2;
// Initialize graphics
initgraph(&gd, &gm, "C:\TURBOC3\BGI" );
initMouse();
// kbhit If a key has been pressed
// then it returns a non zero value
// otherwise returns zero(false)
while (!kbhit()) {
// Show the mouse pointer
showMouse();
// Get the mouse position
getMousePosition(&x1, &y1, &click);
x2 = x1;
y2 = y1;
// When mouse is clicked
while (click == 1) {
hideMouse();
// Draw line
line(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
// Get the mouse position
getMousePosition(&x2, &y2, &click);
}
}
getch();
// Close the graphics
closegraph();
return 0;
}

C ++

// C++ program to perform Free Hand
// Drawing using mouse programming
#include <bits/stdc++.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
using namespace std;
union REGS in, out;
// Function to initialize the mouse
// pointer using graphics
int initMouse()
{
in.x.ax = 0;
int86(0X33, &in, &out);
return out.x.ax;
}
// Function to display the mouse
// pointer using graphics
void showMouse()
{
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to hide the mouse
// pointer using graphics
void hideMouse()
{
// Set AX=2 to hide mouse
in.x.ax = 2;
int86(0X33, &in, &out);
}
// Function to get the exact position
// of the mouse
getMousePosition( int * x, int * y,
int * click)
{
in.x.ax = 3;
// Get the coordinates
int86(0x33, &in, &out);
// Update the coordinates
*x = out.x.cx;
*y = out.x.dx;
*click = out.x.bx & 1;
}
// Driver Code
void main()
{
int status, i, gd = DETECT, gm;
// Initialize graphics
initgraph(&gd, &gm, "C:\TURBOC3\BGI" );
initMouse();
// kbhit If a key has been pressed
// then it returns a non zero value
// otherwise returns zero(false)
while (!kbhit()) {
// Show the mouse pointer
showMouse();
// Get the mouse position
getMousePosition(&x1, &y1, &click);
x2 = x1;
y2 = y1;
// When mouse is clicked
while (click == 1) {
hideMouse();
// Draw line
line(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
// Get the mouse position
getMousePosition(&x2, &y2, &click);
}
}
getch();
// Close the graphics
closegraph();
return 0;
}


Выход:

Хотите узнать о лучших видео и практических задачах, ознакомьтесь с Базовым курсом C для базового и продвинутого C.