2021-02-24
作者:互联网
你在学习c++中想要的
大多数情况和大多数学校都是让你学习C,然后你需要在控制台操作
那么怎么来改变这个控制台呢?
改变控制台相关参数
由于大多数人使用windows操作系统,我们在代码中需要包括windows.h头文件,直接调用现有的函数来进行操作
获取屏幕鼠标的位置
#include<stdio.h>
#include<windows.h>
int main()
{
POINT p;
POINT p1;
GetCursorPos(&p);
printf("%ld %ld\n",p.x,p.y);
while(1)
{
p1=p;
GetCursorPos(&p);
if(p.x!=p1.x || p.y!=p1.y ){
printf("%ld %ld\n",p.x,p.y);
}
//Sleep(1000);
}
return 0;
}
让你的黑窗口弹出一个提示框
#include <Windows.h>
#include <tchar.h> //支持通用字符集的一些操作头文件,使用双字节或者大字符集时可以不包含它
int _tmain() {
MessageBox(NULL, _T("可以更改提示图标和按钮哦~~~"), _T("这是标题哈"), MB_OK |MB_ICONERROR );
return 0;
}
注1: 你看到这种_T(“一段文件”),tchar.h是为了让代码的移植性更好,以便于自动适应不同的字符集
比如
获取和修改控制台的标题
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <strsafe.h>
int main(void)
{
char s1[255];
char s2[255] = "这是另外一个标题嘞";
GetConsoleTitle(s1, 255);
printf("获取控制台标题:%s\n", s1);
SetConsoleTitle(s2);
GetConsoleTitle(s1, 255);
printf("获取控制台标题:%s\n", s1);
return 0;
}
很简单的两个api(都不想写下去了,太简单了 ( ̄_ ̄|||))
其他的比如
在控制台中获取鼠标
#include <windows.h>
#include <stdio.h>
int main(void)
{
// 获取标准输入输出设备句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;
INPUT_RECORD mouseRec;
DWORD res;
COORD crPos, crHome = { 0, 0 };
printf("[Cursor Position] X: %2lu Y: %2lu\n", 0, 0); // 初始状态
while (1)
{
ReadConsoleInput(hIn, &mouseRec, 1, &res);
if (mouseRec.EventType == MOUSE_EVENT)
{
if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
{
break; // 左键双击 退出循环
}
}
crPos = mouseRec.Event.MouseEvent.dwMousePosition;
GetConsoleScreenBufferInfo(hOut, &bInfo);
SetConsoleCursorPosition(hOut, crHome);
printf("[Cursor Position] X: %2lu Y: %2lu", crPos.X, crPos.Y);
SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);
switch (mouseRec.Event.MouseEvent.dwButtonState)
{
case FROM_LEFT_1ST_BUTTON_PRESSED: // 左键 输出A
FillConsoleOutputCharacter(hOut, 'A', 1, crPos, &res);
break; // 如果使用printf输出,则之前需要先设置光标的位置
case RIGHTMOST_BUTTON_PRESSED: // 右键 输出a
FillConsoleOutputCharacter(hOut, 'a', 1, crPos, &res);
break;
default:
break;
}
}
}
CloseHandle(hOut); // 关闭标准输出设备句柄
CloseHandle(hIn); // 关闭标准输入设备句柄
return 0;
}
注1:这个代码csdn很多人都有,放在这里是为了怕找不到
注2:不写基础的了,写的有点累,很多都是以后用不到,或者找一下就会的
设置_获取光标信息
#include <Windows.h>
#include <tchar.h> //支持通用字符集的一些操作头文件,使用双字节或者大字符集时可以不包含它
#include <stdio.h>
int _tmain() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ifo = {60,true};
SetConsoleCursorInfo(hOut, &ifo);
system("pause");
GetConsoleCursorInfo(hOut, &ifo);
printf("%d,%d", ifo.dwSize,ifo.bVisible);
return 0;
}
检测控制台的鼠标按键信息
#include <windows.h>
#include <stdio.h>
HANDLE hStdin;
DWORD fdwSaveOldMode;
VOID ErrorExit(LPSTR);
VOID KeyEventProc(KEY_EVENT_RECORD);
VOID MouseEventProc(MOUSE_EVENT_RECORD);
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
int main(VOID)
{
DWORD cNumRead, fdwMode, i;
INPUT_RECORD irInBuf[128];
int counter = 0;
// Get the standard input handle.
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
ErrorExit("GetStdHandle");
// Save the current input mode, to be restored on exit.
if (!GetConsoleMode(hStdin, &fdwSaveOldMode))
ErrorExit("GetConsoleMode");
// Enable the window and mouse input events.
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (!SetConsoleMode(hStdin, fdwMode))
ErrorExit("SetConsoleMode");
// Loop to read and handle the next 100 input events.
while (counter++ <= 100)
{
// Wait for the events.
if (!ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead)) // number of records read
ErrorExit("ReadConsoleInput");
// Dispatch the events to the appropriate handler.
for (i = 0; i < cNumRead; i++)
{
switch (irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT: // mouse input
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
break;
case FOCUS_EVENT: // disregard focus events
case MENU_EVENT: // disregard menu events
break;
default:
ErrorExit("Unknown event type");
break;
}
}
}
// Restore input mode on exit.
SetConsoleMode(hStdin, fdwSaveOldMode);
return 0;
}
VOID ErrorExit(LPSTR lpszMessage)
{
fprintf(stderr, "%s\n", lpszMessage);
// Restore input mode on exit.
SetConsoleMode(hStdin, fdwSaveOldMode);
ExitProcess(0);
}
VOID KeyEventProc(KEY_EVENT_RECORD ker)
{
printf("Key event: ");
if (ker.bKeyDown)
printf("key pressed\n");
else printf("key released\n");
}
VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
{
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
printf("Mouse event: ");
switch (mer.dwEventFlags)
{
case 0:
if (mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
printf("left button press \n");
}
else if (mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
{
printf("right button press \n");
}
else
{
printf("button press\n");
}
break;
case DOUBLE_CLICK:
printf("double click\n");
break;
case MOUSE_HWHEELED:
printf("horizontal mouse wheel\n");
break;
case MOUSE_MOVED:
printf("mouse moved\n");
break;
case MOUSE_WHEELED:
printf("vertical mouse wheel\n");
break;
default:
printf("unknown\n");
break;
}
}
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
{
printf("Resize event\n");
printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
}
注1:这是微软官方的代码,所以就是分享给大家的
标签:02,24,break,case,2021,printf,hOut,include,EVENT 来源: https://blog.csdn.net/qq_45837462/article/details/114041388