鍵盤互動(4)
文章目錄
- 一.引言
- 二.完善哪些內容
- 1.游標
- ①代碼
- ②函式介紹
- 2.色彩
- ①代碼
- ②函式介紹
- ③案例實作
- 3.聲音
- ①實作
- ②注意點
一.引言
作為一個初學者,當你厭倦了枯燥乏味,非黑即白的控制臺界面,想不想讓它變得五彩繽紛?
當你沉浸在debug的海洋心煩意亂,這時如果你喜歡的音樂從你的程式中蹦出,想必會有如同仙樂耳暫明的感覺吧…
這些,可以在在一個小小的控制臺實作嗎?
那就,一起來,完善這個單調的控制臺界面吧👉
二.完善哪些內容
在我上一篇文章中,移動句號的程序中,你可能會發現有游標在閃,那么怎么來消除掉這個游標呢?windows提供了函式
還有上面的想讓界面變得多彩,想讓程式張嘴唱歌,都可以實作
1.游標
①代碼
游標通過gotoxy可以移動到坐標處
下面兩個函式可以實作游標的隱藏與顯示
//隱藏游標
void hide_cursor()
{
HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(h_GAME,&cursor_info);
cursor_info.bVisible=false; //不顯示游標
SetConsoleCursorInfo(h_GAME,&cursor_info);
}
//顯示游標
void show_cursor()
{
HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(h_GAME,&cursor_info);
cursor_info.bVisible=true; //顯示游標
SetConsoleCursorInfo(h_GAME,&cursor_info);
}
②函式介紹
在網上找到了這位大佬的文章,寫的非常詳細,大家可以參看https://www.cnblogs.com/szitcast/p/10923499.html#00-%E7%9B%AE%E5%BD%95
2.色彩
①代碼
//設定文本顏色
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
②函式介紹
? 可以參看百度百科https://baike.baidu.com/item/SetConsoleTextAttribute/570121?fr=aladdin
③案例實作
#include<windows.h>
#include<iostream>
using namespace std;
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
int main()
{
int i;
for(i=0;i<128;++i)
{
color(i);
cout<<i<<"--hello!"<<endl;
}
return 0;
}
3.聲音
①實作
我當時參看了這幾位大佬的文章
https://blog.csdn.net/Legends_Never_Die/article/details/81030169?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161676391916780357257283%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=161676391916780357257283&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-81030169.pc_search_result_before_js&utm_term=codeblocks%E6%92%AD%E6%94%BE%E9%9F%B3%E4%B9%90&spm=1018.2226.3001.4187
https://blog.csdn.net/qq_40456669/article/details/82748901?ops_request_misc=&request_id=&biz_id=102&utm_term=PlaySound&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-7-82748901.pc_search_result_before_js&spm=1018.2226.3001.4187
下面是我的代碼
#include<iostream>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
int main()
{
PlaySound(TEXT("C:\\Users\\86171\\Desktop\\temp\\ADle.wav"),NULL,SND_FILENAME | SND_ASYNC);
cin.get();
return 0;
}
②注意點
一定要是wav檔案
給大家推薦個免費的音頻格式轉換(不止音頻轉換功能)的網站😏
https://www.media.io/online-tools.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/274496.html
標籤:其他
上一篇:iOS 判斷是否插了耳機
下一篇:H264碼流分析和打包RTP程序
