如下例,
例1:
# include <Windows.h> # include <stdio.h> # pragma comment (lib, "Winmm.lib")//导入Winmm.lib库 int main(void) { system("color 6f"); PlaySound("000.wav", NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); //第七行 system("pause"); return 0; }在上面的例子中,system(“pause”)的功能是冻结屏幕(即不会让一些程序的运行结果一闪而逝);
system(“color 6f”)是改变背景颜色和前景文字,具体情况如下图:
另外,在第七行,写成 PlaySound("000.wav", NULL, SND_FILENAME); 也可以,只是这样音乐播放完后就停止了,如向上例中所写,可以使音乐循环播放,但要注意,音乐需要wav格式,音乐需要放在Debug文件中。
如果想播放多个歌曲,直接把上面程序的第8行代码继续向下复制即可;而调用系统时间,下面也给出了,不过,下面是把背景音乐和调用当前时间写成了两个函数,如下所示
例2:
# include <Windows.h> # include <time.h> # include <stdio.h> # pragma comment (lib, "Winmm.lib")//导入Winmm.lib库 void dqsj(); void bjyy(); int main(void) { system("color 2f"); dqsj(); bjyy(); printf("\n\n播放完音乐后\n\n"); dqsj(); system("pause"); return 0; } void dqsj() { char * wday[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; time_t t; struct tm *p; t = time(NULL); p = gmtime(&t); printf("现在时间是:"); printf("%d年%2d月%2d日", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday); printf(" %s ", wday[p->tm_wday]); printf("%02d:%02d:%02d\n", (8+p->tm_hour), p->tm_min, p->tm_sec); } void bjyy() { PlaySound("青鸟飞鱼 - 此生不换.wav", NULL, SND_FILENAME); PlaySound("王筝-我们都是好孩子.wav", NULL, SND_FILENAME); PlaySound("愿得一人心.wav", NULL, SND_FILENAME); PlaySound("诛仙剑.wav", NULL, SND_FILENAME); PlaySound("萧人凤-仙剑问情.wav", NULL, SND_FILENAME);// | SND_ASYNC|SND_LOOP PlaySound("仙剑奇侠传-千年缘.wav", NULL, SND_FILENAME); PlaySound("任贤齐-诛仙恋.wav", NULL, SND_FILENAME); PlaySound("林俊杰-爱不会绝迹.wav", NULL, SND_FILENAME); PlaySound("梁静茹-勇气.wav", NULL, SND_FILENAME); PlaySound("秦时明月-谁主沉浮.wav", NULL, SND_FILENAME); }
这儿有音乐格式转化器——点击打开链接
【所有代码均在windows系统下VC++6.0下运行通过】
(如有错误,敬请指正)