需要MFC支持。
示例代码:
// Sample2.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h”
#include “console.h”
#pragma comment(lib,“console.lib”)
#include
#include
#include
#include
int circulation=1;
int interface_num=0;
extern COORD mouse;
void MainInterface(); // 0
void SetQuestion(); // 1
void Answer(); // 2
void HistoryScore(); // 3
char *Date_Time();
void SaveScore(char *,int,int);
char *SelectFile(HWND hwnd);
class PlayMusic
{
private:
TCHAR fileName[MAX_PATH+100];
TCHAR shortName[MAX_PATH];
TCHAR cmd[MAX_PATH+250];
char buf1[260];
public:
int volume; // 范围: 1~1000
PlayMusic(void);
PlayMusic(char mu[]);
~PlayMusic(void);
void OpenEquipment();
void CloseEquipment();
void AssignCurrentMusic(char mu[]);
void Play();
void Stop();
void SetVolume();
void RepeatPlay();
};
PlayMusic::PlayMusic(void)
{
OpenEquipment();
}
PlayMusic::PlayMusic(char mu[])
{
OpenEquipment();
wsprintf(fileName,"%s",mu);
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
}
PlayMusic::~PlayMusic(void)
{
CloseEquipment();
}
void PlayMusic::OpenEquipment()
{
wsprintf(cmd,“open %s”,“waveaudio”);
mciSendString(cmd,buf1,sizeof(buf1),NULL);
}
void PlayMusic::CloseEquipment()
{
wsprintf(cmd,“close %s”,“waveaudio”);
mciSendString(cmd,buf1,sizeof(buf1),NULL);
}
void PlayMusic::AssignCurrentMusic(char mu[])
{
wsprintf(fileName,"%s",mu);
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
}
void PlayMusic::Play()
{
wsprintf(cmd,“play %s”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::Stop()
{
wsprintf(cmd,“stop %s”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::SetVolume()
{
wsprintf(cmd,“setaudio %s volume to %d”,shortName,volume);
mciSendString(cmd,"",NULL,NULL);
}
void PlayMusic::RepeatPlay()
{
wsprintf(cmd,“play %s repeat”,shortName);
mciSendString(cmd,"",NULL,NULL);
}
int main()
{
Screen screen;
screen.SetTitle(“记单词”);
screen.ShowOrHideCursor(0);
Event event;
event.AddMouseMode();
while(circulation)
{
screen.Clear_screen();
switch(interface_num)
{
case 0:
MainInterface();
break;
case 1:
SetQuestion();
break;
case 2:
Answer();
break;
case 3:
HistoryScore();
break;
}
}
event.RestoreMode();
//screen.Pause();
screen.Close_handle();
return 0;
}
void MainInterface()
{
File file;
file.Getcwd();
if(_access("word.txt",0)==-1)
{
FILE *dhb;
fopen_s(&dhb,"word.txt","a");
fclose(dhb);
}
if(_access("score.txt",0)==-1)
{
FILE *dhb;
fopen_s(&dhb,"score.txt","a");
fclose(dhb);
}
Screen screen;
screen.SetSize(80,25);
Button EXIT_button=Button(50,16,"退出");
EXIT_button.Display_button();
Button button1[2]={Button(35,8,"出题"),Button(35,10,"答题")};
button1[0].Display_button();
button1[1].Display_button();
Button score_button=Button(20,16,"历史成绩");
score_button.Display_button();
Button Music_button=Button(33,16,"背景音乐");
Music_button.Display_button();
Event event;
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(EXIT_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
circulation=0;
return;
}
if(button1[0].MousePosition_At_button()
&&event.Left_Button_Pressed())
{
interface_num=1;
return;
}
if(button1[1].MousePosition_At_button()
&&event.Left_Button_Pressed())
{
interface_num=2;
return;
}
if(score_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
interface_num=3;
return;
}
if(Music_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
HWND cmd=GetConsoleWindow();
char *szFileName=SelectFile(cmd);
if(szFileName!=NULL)
{
_chdir(file.path0);
CopyFile(szFileName,"background_music.mp3",false);
}
event.FlushInputBuffer();
}
}
if(event.Key_Event()&&event.Key_Down())
{
char ch=event.Get_ch();
WORD vk=event.Get_vk();
if(ch=='q'||vk==VK_ESCAPE)
{
circulation=0;
return;
}
}
}
}
void SetQuestion()
{
Screen screen;
screen.SetSize(60,20);
Button RETURN_button=Button(25,13,"返回");
RETURN_button.Display_button();
Event event;
Button OK_button=Button(15,13,"确定");
OK_button.Display_button();
screen.DisplayText(20,1,"添加");
screen.DisplayText(10,5,"单词");
screen.DisplayText(10,7,"中文");
Textbox textbox[2]={Textbox(16,5,30),Textbox(16,7,30)};
for(int i=0;i<2;i++)
{
textbox[i].Attribute(2,1,1);
}
char *word,*chinese;
Button Clear_button=Button(34,13,"清空历史记录");
Clear_button.Display_button();
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(RETURN_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
interface_num=0;
return;
}
if(textbox[0].MousePosition_At_textbox()
&&event.Left_Button_Pressed())
{
word=textbox[0].InputString(true);
}
if(textbox[1].MousePosition_At_textbox()
&&event.Left_Button_Pressed())
{
chinese=textbox[1].InputString(true);
}
if(OK_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
if(textbox[0].Non_empty&&textbox[1].Non_empty)
{
errno_t err;
FILE *dhb;
err=fopen_s(&dhb,"word.txt","a");
if(err!=0)
{
printf("%s File open error,code: %d","word.txt",err); /*提示打开不成功*/
}
fprintf_s(dhb,"%-30s %-30s\n",word,chinese);
fclose(dhb);
textbox[0].Clear_textbox();
textbox[1].Clear_textbox();
}
}
if(Clear_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
File file;
if(!file.File_is_empty("word.txt"))
{
HWND cmd=GetConsoleWindow();
if(MessageBox(cmd,TEXT("Are you sure to empty?"),TEXT("清空历史记录"),MB_YESNO | MB_ICONQUESTION )==IDYES)
{
FILE *fp;
fopen_s(&fp,"word.txt","w");
fclose(fp);
}
}
event.FlushInputBuffer();
}
}
}
}
void Answer()
{
char *date_time=Date_Time();
Screen screen;
screen.SetSize(80,25);
screen.Paint_screen(14);
Button Mode_button[2]={Button(60,12,"[ ]顺序"),Button(60,13,"[ ]随机")};
for(int i=0;i<2;i++)Mode_button[i].Display_button();
int Mode=0;
Mode_button[Mode].FillCharacter('*');
Button NEXT_button=Button(60,17,"下一个");
NEXT_button.Display_button();
Button OK_button=Button(42,22,"确定");
OK_button.Display_button();
Button BACK_button=Button(48,22,"后退");
BACK_button.Display_button();
Button RETURN_button=Button(60,22,"返回");
RETURN_button.Display_button();
Window window=Window(2,1,50,20);
window.window_textcolor_num=6;
window.Paint_window(0);
Window window1=Window(60,1,15,5);
window1.window_textcolor_num=7;
window1.Paint_window(0);
Textbox Result_textbox=Textbox(2,22,30);
//Result_textbox.textbox_textcolor_num=4;
Result_textbox.Paint_textbox();
File file;
int totallines=file.TotalLines("word.txt");;
int line=-1;
char wc[240]="apple 苹果";
char word[30],chinese[30];
struct Character
{
int x;
int y;
char ch;
}ch_array[30];
int ch_array_length;
char result[30]={0};
srand((unsigned)time(0));
int sum=0,ok_num=0;
bool valid=false;
//
PlayMusic play("background_music.mp3");
play.RepeatPlay();
play.volume=450;
play.SetVolume();
//
Event event;
event.WriteEvent_button(NEXT_button);
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(RETURN_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
char score[30];
int sco=(int)((float)ok_num/sum*100);
sprintf_s(score,30,"共答: %d,答对: %d,得分: %d。",sum,ok_num,sco);
HWND hwnd=GetConsoleWindow();
MessageBox(hwnd,TEXT(score),TEXT("成绩"),MB_OK);
SaveScore(date_time,sum,ok_num);
play.Stop();
interface_num=0;
return;
}
for(int i=0;i<2;i++)
{
if(Mode_button[i].MousePosition_At_button()&&event.Left_Button_Pressed())
{
Mode_button[Mode].FillCharacter(' ');
Mode=i;
Mode_button[Mode].FillCharacter('*');
}
}
if(NEXT_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
Result_textbox.Clear_textbox();
memset(result,0,sizeof(result));
screen.SetTextColorAndTextBackgroundColor(6,14);
screen.MoveCursorTo(36,22);
screen.Write(" ");
window.Clear_window();
window1.Clear_window();
valid=true;
sum++;
switch(Mode)
{
case 0:
line++;
if(line==totallines)line=0;
break;
case 1:
line=(int)(rand()%totallines);
break;
}
char *str=file.ReadFileLine("word.txt",line);
if(str!=NULL)strcpy_s(wc,240,str);
sscanf_s(wc,"%s %s",word,30,chinese,30);
window1.DisplayStringEx_window(chinese);
window.DisplayString_window(20,10,word);
Sleep(3000);
window.Clear_window();
ch_array_length=strlen(word);
for(int i=0;i
loop:
ch_array[i].x=window.cx0+1+(int)(rand()%(window.wide-3));
ch_array[i].y=window.cy0+1+(int)(rand()%(window.heigh-3));
//printf("%d %d\n",ch_array[i].x,ch_array[i].y);
for(int j=i-1;j>=0;j--)
{
if(ch_array[i].x==ch_array[j].x&&ch_array[i].y==ch_array[j].y)goto loop;
}
screen.SetTextColorAndTextBackgroundColor(6,0);
screen.MoveCursorTo(ch_array[i].x,ch_array[i].y);
putchar(ch_array[i].ch);
Sleep(100);
}
event.FlushInputBuffer();
}
event.Get_mouse_position();
for(int i=0;i0)
{
loop1:
ch_array[ch_array_length].x=window.cx0+1+(int)(rand()%(window.wide-3));
ch_array[ch_array_length].y=window.cy0+1+(int)(rand()%(window.heigh-3));
for(int j=ch_array_length-1;j>=0;j–)
{
if(ch_array[ch_array_length].xch_array[j].x&&ch_array[ch_array_length].ych_array[j].y)goto loop1;
}
ch_array[ch_array_length].ch=result[length-1];
screen.SetTextColorAndTextBackgroundColor(6,0);
screen.MoveCursorTo(ch_array[ch_array_length].x,ch_array[ch_array_length].y);
putchar(ch_array[ch_array_length].ch);
ch_array_length++;
result[length-1]='\0';
Result_textbox.DisplayString_textbox(result);
}
}
if(OK_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
if(valid)
{
if(strcmp(word,result)==0)
{
screen.SetTextColorAndTextBackgroundColor(3,14);
screen.MoveCursorTo(36,22);
screen.Write("OK!");
ok_num++;
PlaySound(TEXT("yes.wav"), NULL, SND_FILENAME | SND_ASYNC);
}
else
{
screen.SetTextColorAndTextBackgroundColor(2,14);
screen.MoveCursorTo(36,22);
screen.Write("NO!");
PlaySound(TEXT("no.wav"), NULL, SND_FILENAME | SND_ASYNC);
}
valid=false;
}
}
}
}
}
void HistoryScore()
{
Screen screen;
screen.SetSize(60,20);
screen.DisplayText(8,3,"日期 时间 共答 答对 得分");
char *filename="score.txt";
Window window=Window(5,4,50,10);
window.Attribute1(false,false,0);
window.DisplayFile_MultiPageWindow("score.txt");
screen.SetTextColorAndTextBackgroundColor(3,0);
screen.MoveCursorTo(30,15);
printf("%d/%d",window.page,window.pages);
window.MultiPageButtonPageDown.button_color_num=2;
window.MultiPageButtonPageDown.Display_button();
window.MultiPageButtonPageUp.button_color_num=2;
window.MultiPageButtonPageUp.Display_button();
Button RETURN_button=Button(40,18,"返回");
RETURN_button.Display_button();
Button Clear_button=Button(15,18,"清空历史记录");
Clear_button.Display_button();
Event event;
while(1)
{
event.ReadEvent();
if(event.Mouse_Event())
{
if(RETURN_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
interface_num=0;
return;
}
if(Clear_button.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
FILE *fp;
fopen_s(&fp,"score.txt","w");
fclose(fp);
window.DisplayFile_MultiPageWindow("score.txt");
}
if(window.MultiPageButtonPageDown.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
window.MultiPageWindowPageTurn_file(DOWN,"score.txt");
screen.MoveCursorTo(30,15);
printf("%d/%d",window.page,window.pages);
}
if(window.MultiPageButtonPageUp.MousePosition_At_button()
&&event.Left_Button_Pressed())
{
window.MultiPageWindowPageTurn_file(UP,"score.txt");
screen.MoveCursorTo(30,15);
printf("%d/%d",window.page,window.pages);
}
}
}
}
char *Date_Time()
{
time_t take_off;
struct tm timeinfo;
char *date_time=new char[20];
take_off = time(NULL);
localtime_s(&timeinfo,&take_off);
strftime(date_time,20,"%Y-%m-%d %H:%M:%S",&timeinfo);
return date_time;
}
void SaveScore(char *date_time,int sum,int ok_num)
{
FILE *fp;
int sco=(int)((float)ok_num/sum*100);
fopen_s(&fp,"score.txt","a");
fprintf(fp,"%s %3d %3d %3d\n",date_time,sum,ok_num,sco);
fclose(fp);
}
// Select.cpp
#include “stdafx.h”
#include
#include
char *SelectFolder(HWND hwnd)
{
char *strPath = NULL;
BROWSEINFO bInfo;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.hwndOwner =hwnd;
bInfo.pidlRoot=CSIDL_DESKTOP;
bInfo.lpszTitle = _T("作者:王力");
bInfo.ulFlags = BIF_RETURNONLYFSDIRS;
LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框
if(lpDlist != NULL) //用户按了确定按钮
{
strPath=new TCHAR[256]; //用来存储路径的字符串
SHGetPathFromIDList(lpDlist, strPath);//把项目标识列表转化成字符串
}
return strPath;
}
char *SelectFile(HWND hwnd)
{
OPENFILENAME ofn;
char *szFileName=new char[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = '\0';
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "mp3 Files (*.mp3)\0*.mp3\0All Files (*.*)\0*.*\0\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "mp3";
ofn.lpstrTitle="作者:王力";
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
return szFileName;
}
else return NULL;
}
运行效果: