在本文中,我们将讨论鼠标编程的一些用例:
在图形模式下显示鼠标指针:
为了显示鼠标指针,首先使用initgraph()函数启用图形模式,如果初始化成功,则调用initMouse()函数检查鼠标是否可用;如果initMouse()函数返回不等于0,则它将返回ffffh然后调用showMouse()函数,并以图形方式在控制台窗口上显示光标。以下是使用的功能:
- initMouse():用于初始化鼠标。
- showMouse():在输出屏幕上显示鼠标指针。
以下是相同的程序:
C
// C program to display Mouse
// pointer in Graphics Mode
#include
#include
#include
#include
union REGS in, out;
// Function to display the mouse
// pointer
void showMouse()
{
// Set service AX = 1 for
// displaying mouse
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to initialize the mouse
// pointer
int initMouse()
{
// Set service AX = 0 for
// detecting mouse
in.x.ax = 0;
int86(0x33, &in, &out);
return out.x.ax;
}
// Driver Code
void main()
{
int status, gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm,
"C:\\TURBOC3\\BGI");
// Get the status of mouse pointer
status = initMouse();
// Check mouse is avilable or not
if (status == 0)
printf("Mouse support not"
" available.\n");
else {
printf("Display mouse");
showMouse();
}
getch();
// Close the graphics
closegraph();
}
C++
// C++ program to display Mouse
// pointer in Graphics Mode
#include
#include
#include
#include
#include
using namespace std;
union REGS in, out;
// Function to display the mouse
// pointer
void showMouse()
{
// Set service AX = 1 for
// displaying mouse
in.x.ax = 1;
int86(0X33, &in, &out);
}
// Function to initialize the mouse
// pointer
int initMouse()
{
// Set service AX = 0 for
// detecting mouse
in.x.ax = 0;
int86(0x33, &in, &out);
return out.x.ax;
}
// Driver Code
void main()
{
int status, gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Get the status of mouse pointer
status = initMouse();
// Check if mouse is avilable or not
if (status == 0)
cout << "Mouse support not"
<< " available.\n";
else {
cout << "Display mouse";
showMouse();
}
getch();
// Close the graphics
closegraph();
}
C
// C program for hiding Mouse Pointer
#include
#include
#include
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);
}
// 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.\n");
else {
for (i = 0; i <= 10; i++) {
// Pause execution for .5 sec
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
// Close the graphics
closegraph();
}
C++
// C++ program for hiding Mouse Pointer
#include
#include
#include
#include
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);
}
// 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.\n";
else {
for (i = 0; i <= 10; i++) {
// Pause execution for .5 sec
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
// Close the graphics
closegraph();
}
C
// C program to determine the current
// mouse position and button clicked
#include
#include
#include
#include
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 get the current clicked
// mouse position on the screen
void getMousePosition(int* click,
int* x, int* y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
// Get the coordinates
*click = out.x.bx;
// Update the x and y coordinates
*x = out.x.cx;
*y = out.x.dx;
}
// 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.\n");
else {
showMouse();
// Get the current mouse position
getMousePosition(&click, &x, &y);
while (!kbhit()) {
getMousePosition(&click, &x, &y);
gotoxy(1, 1);
printf("X = %d, Y = %d", x, y);
if (click == 1)
printf("\nLeft click at "
"position = %d, %d\t",
x, y);
if (click == 2)
printf("\nRight click at "
"position = %d, %d\t",
x, y);
}
}
getch();
// Close the graphics
closegraph();
}
C++
// C program to determine the current
// mouse position and button clicked
#include
#include
#include
#include
#include
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 get the current clicked
// mouse position on the screen
void getMousePosition(int* click,
int* x, int* y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
// Get the coordinates
*click = out.x.bx;
// Update the x and y coordinates
*x = out.x.cx;
*y = out.x.dx;
}
// 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.\n");
else {
showMouse();
// Get the current mouse position
getMousePosition(&click, &x, &y);
while (!kbhit()) {
getMousePosition(&click, &x, &y);
gotoxy(1, 1);
printf("X = %d, Y = %d", x, y);
if (click == 1)
cout << "\nLeft click at "
<< "position = "
<< x << ", " << y;
if (click == 2)
cout << "\nRight click at "
<< "position = "
<< x << ", " << y;
}
}
getch();
// Close the graphics
closegraph();
}
输出:
隐藏鼠标指针:
要隐藏鼠标指针,首先检查鼠标的状态(如果可用),然后简单地控制进入500毫秒的for循环使延迟,然后鼠标出现,然后在500毫秒后消失,并在500毫秒后再次显示。该序列将继续直到循环结束。延迟函数已用于向您显示hidemouse()函数的影响。
- initmouse():用于初始化鼠标。
- showmouse():在输出屏幕上显示鼠标指针。
- hidemouse():隐藏鼠标指针
以下是相同的程序:
C
// C program for hiding Mouse Pointer
#include
#include
#include
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);
}
// 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.\n");
else {
for (i = 0; i <= 10; i++) {
// Pause execution for .5 sec
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
// Close the graphics
closegraph();
}
C++
// C++ program for hiding Mouse Pointer
#include
#include
#include
#include
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);
}
// 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.\n";
else {
for (i = 0; i <= 10; i++) {
// Pause execution for .5 sec
delay(500);
showMouse();
delay(500);
hideMouse();
}
}
getch();
// Close the graphics
closegraph();
}
输出:
确定当前的鼠标位置和单击的按钮:
在此程序中,使用以下功能打印鼠标的当前位置以及在哪个位置单击鼠标的哪个键:
- initmouse():用于初始化鼠标。
- showmouse():在输出屏幕上显示鼠标光标。
- getmouseposition():分别获取鼠标坐标和BX , CX , DX寄存器中所按下的键。
- 如果BX = 1左键单击
- 如果BX = 2,则单击右键。
- 如果按下BX = 3中央按钮
- CX =鼠标X坐标
- DX =鼠标Y坐标
以下是相同的程序:
C
// C program to determine the current
// mouse position and button clicked
#include
#include
#include
#include
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 get the current clicked
// mouse position on the screen
void getMousePosition(int* click,
int* x, int* y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
// Get the coordinates
*click = out.x.bx;
// Update the x and y coordinates
*x = out.x.cx;
*y = out.x.dx;
}
// 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.\n");
else {
showMouse();
// Get the current mouse position
getMousePosition(&click, &x, &y);
while (!kbhit()) {
getMousePosition(&click, &x, &y);
gotoxy(1, 1);
printf("X = %d, Y = %d", x, y);
if (click == 1)
printf("\nLeft click at "
"position = %d, %d\t",
x, y);
if (click == 2)
printf("\nRight click at "
"position = %d, %d\t",
x, y);
}
}
getch();
// Close the graphics
closegraph();
}
C++
// C program to determine the current
// mouse position and button clicked
#include
#include
#include
#include
#include
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 get the current clicked
// mouse position on the screen
void getMousePosition(int* click,
int* x, int* y)
{
in.x.ax = 3;
int86(0X33, &in, &out);
// Get the coordinates
*click = out.x.bx;
// Update the x and y coordinates
*x = out.x.cx;
*y = out.x.dx;
}
// 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.\n");
else {
showMouse();
// Get the current mouse position
getMousePosition(&click, &x, &y);
while (!kbhit()) {
getMousePosition(&click, &x, &y);
gotoxy(1, 1);
printf("X = %d, Y = %d", x, y);
if (click == 1)
cout << "\nLeft click at "
<< "position = "
<< x << ", " << y;
if (click == 2)
cout << "\nRight click at "
<< "position = "
<< x << ", " << y;
}
}
getch();
// Close the graphics
closegraph();
}
输出:
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。