📄 main.cpp
字号:
//多媒体作业:wav播放器
//0213202班 赵子铭
//filename main.cpp
//the main part of the player
#pragma comment( lib, "Winmm.LIB");
#define WIN32_LEAN_AND_MEAN
#define UNTITLED "(untitled)"
#define BUTTON_BASE_ID 100
#define NUM_BUTTONS 3
#define WINDOW_CLASS_NAME "WINCLASS1"
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <mmsystem.h> // very important and include WINMM.LIB too!
#include <Commdlg.h>
#include <cstdio>
#include <cstdlib>
#include <Cderr.h>
#include <cmath>
#include "resourse.h"
#include "WavHead.h"
using namespace std;
static char szAppName[] = "Ziming's raw wave player" ;
static HWND hDlgModeless ;
// Functions in openfile.Cpp
void PopFileInitialize (HWND) ;
BOOL PopFileOpenDlg (HWND, PTSTR, PTSTR) ;
BOOL PopFileSaveDlg (HWND, PTSTR, PTSTR) ;
BOOL PopFileRead (HWND, PTSTR) ;
BOOL PopFileWrite (HWND, PTSTR) ;
long PopFileLength (FILE *file);
void DoCaption (HWND hwnd, char *szTitleName)
{
char szCaption[64 + _MAX_FNAME + _MAX_EXT] ;
wsprintf (szCaption, "%s - %s", szAppName,
szTitleName[0] ? szTitleName : UNTITLED) ;
SetWindowText (hwnd, szCaption) ;
}
// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance
MSG msg; // generic message
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc; // handle to a device context
static int cxClient, cyClient;
static char szFileName[_MAX_PATH] ;
static char szTitleName[_MAX_FNAME + _MAX_EXT] ;
static HMMIO hfile=NULL;
static Cwavefilehead buffer;
unsigned char*c;
short *c1;
static DWORD ds;
static HPEN hpen,old_hpen;
int i,x0,y0;
HMENU hMenu;
hMenu = GetMenu (hwnd) ;
TCHAR str[160];
switch(msg)
{
case WM_CREATE:
{
PopFileInitialize (hwnd) ;// return success
EnableMenuItem (hMenu, MENU_EDIT_ID_INFOR, MF_GRAYED) ;
return(0);
}
case WM_SIZE:
{
cxClient = LOWORD(lparam);//客户区的宽度
cyClient = HIWORD(lparam);//客户区的高度
return(0);
}
case WM_COMMAND:
{
switch(LOWORD(wparam))
{
case MENU_FILE_ID_EXIT:
{
// terminate window
PostQuitMessage(0);
return(0);
}
case MENU_HELP_ABOUT:
{
TCHAR about[1000];
wsprintf (about, "0213202 Zhao Ziming developed with WINAPI:\n\nprogram provides GUI and file_select dialogue\nuser can only choose the .wav file\nprogram provides the waveform\nprogram can play the wav file\nprogram provides the information about the file\n");
MessageBox(hwnd, about,
"About Program",
MB_OK );
return(0);
}
case MENU_HELP_USAGE:
{
TCHAR usage[1000];
wsprintf (usage, "usage:\nplease open a file before click 'information' button\n\nreference:\n<Programming Windows>\n<Programming Windows with MFC>\n");
MessageBox(hwnd, usage,
"About Program",
MB_OK );
return(0);
}
case MENU_EDIT_ID_INFOR:
{
wsprintf(str,"the wav file has %d channel(s)\nSamples per second:%d\nSample rate * block align:%d\nBit per samples:%d\n",
buffer.channel,buffer.sampl,buffer.bytepersecblockalign,buffer.bitpersamples);
MessageBox(hwnd, str,
"Information about the file",
MB_OK );
return(0);
}
case MENU_FILE_ID_OPEN:
{
PopFileOpenDlg (hwnd, szFileName, szTitleName);
EnableMenuItem (hMenu, MENU_EDIT_ID_INFOR, MF_ENABLED) ;
FILE *file ;
hfile = mmioOpen(szFileName,NULL,MMIO_READ);
if (NULL == (file = fopen (szFileName, "r")))
return FALSE ;
fread (&buffer, sizeof( Cwavefilehead ), 1, file) ;
fclose(file);
//UpdateWindow (hwnd) ;
RECT rc;
GetClientRect(hwnd,&rc);
InvalidateRect(hwnd,&rc,TRUE);
PlaySound(szFileName,NULL,SND_FILENAME | SND_ASYNC);
DoCaption (hwnd, szTitleName) ;
return (0);
}
case MENU_FILE_ID_STOP:
{
PlaySound(NULL,NULL, SND_ASYNC);
return(0);
}
} // end switch wparam
}// end WM_COMMAND
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
//hfile = mmioOpen(szFileName,NULL,MMIO_READ);
if(hfile==NULL)
return 0;
mmioSeek(hfile,4,SEEK_SET);
mmioRead(hfile,(char*)&ds,4);
hpen = CreatePen(PS_SOLID,1,RGB(255,0,0));
old_hpen = (HPEN)SelectObject(hdc,hpen);
if(buffer.bitpersamples==8)
{
c= new unsigned char[ds];
mmioSeek(hfile,44,SEEK_SET);
mmioRead(hfile,reinterpret_cast<HPSTR>(c),ds);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, ds, 500, NULL);
SetViewportExtEx(hdc, 650, 256, NULL);
SetViewportOrgEx(hdc,cxClient*0.2,cyClient*0.5,NULL);
for (i=0;i<ds-4;i++)
{
x0=i;
y0=(int)(c[i]);
MoveToEx(hdc,x0, 0, NULL);
LineTo(hdc,x0,y0-128);
}
}
if(buffer.bitpersamples==16)
{
c1= new short[ds/2];
mmioSeek(hfile,44,SEEK_SET);
mmioRead(hfile,reinterpret_cast<HPSTR>(c1),ds);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, ds/2, 10000, NULL);
SetViewportExtEx(hdc, 650, 256, NULL);
SetViewportOrgEx(hdc,cxClient*0.2,cyClient*0.5,NULL);
for (i=0;i<(ds/2-4);i++)
{
x0=i;
y0=c1[i];
MoveToEx(hdc,x0, 0, NULL);
LineTo(hdc,x0,y0-128);
} SelectObject(hdc,old_hpen);
}
DeleteObject(hpen);
DoCaption (hwnd, szTitleName) ;
EndPaint(hwnd,&ps);
return(0);
}
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}
} // end switch
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class
HWND hwnd; // generic window handle
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW ;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_WAVPLAYER));
winclass.hCursor = NULL;
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = "SoundMenu";
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_WAVPLAYER));
// save hinstance in global
hinstance_app = hinstance;
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"Ziming's raw wave player", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MAXIMIZE ,
0,0,
400,400,
NULL,
NULL,
hinstance,// instance of this application
NULL)))
return(0);
// save main window handle
main_window_handle = hwnd;
ShowWindow (hwnd, ncmdshow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return(msg.wParam);
} // end WinMain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -