Code:
void Window::Register(){
WNDCLASS wndclass;
wndclass.style=NULL;
wndclass.lpfnWndProc=::WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=sizeof(Window*);
wndclass.hInstance=TApp::hInstance;
wndclass.hIcon=LoadIcon(TApp::hInstance,"small");
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(60,80,110));
wndclass.lpszMenuName="CMDLGAPMENU"; //menu name in ressource
wndclass.lpszClassName=name;
if(!RegisterClass(&wndclass))exit(FALSE);
}
LRESULT Window::WndProc(UINT iMessage,WPARAM wParam,LPARAM lParam){
switch(iMessage){
case WM_COMMAND:
switch(GET_WM_COMMAND_ID(wParam,lParam)){
case CM_EXIT:
DestroyWindow(hwnd);
break;
case CM_U_FILEOPEN:
CMUFileOpen();
return 0L;
case CM_U_HELPABOUT:
MessageBox(hwnd,"Music Score Writer","About Pautas",MB_OK);
return 0L;
default:break;
}
break;
case WM_CREATE:
szName[0]=0;
SoundSize=0;
idTimer=0;
index=0;
wavBuf=new unsigned char[BUFSIZE];
break;
case WM_TIMER:
DrawData();
break;
case WM_PAINT:
break;
case WM_DESTROY:
ReleaseDC(hwnd,dc);
if(idTimer)KillTimer(hwnd,idTimer);
delete wavBuf;
if(szName){
_lclose(hFile);
szName[0]=0;
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,iMessage,wParam,lParam);
}
return 0;
}
void Window::CMUFileOpen(){
if(szName){
_lclose(hFile);
szName[0]=0;
}
OPENFILENAME ofnTemp;
DWORD Errval; // Error value
char buf[5]; // Error buffer
char Errstr[50]="GetOpenFileName returned Error #";
char szTemp[] = "Wave Files (*.wav)\0*.wav\0";
ofnTemp.lStructSize = sizeof( OPENFILENAME );
ofnTemp.hwndOwner = hwnd; // An invalid hWnd causes non-modality
ofnTemp.hInstance = 0;
ofnTemp.lpstrFilter = (LPSTR)szTemp; // See previous note concerning string
ofnTemp.lpstrCustomFilter = NULL;
ofnTemp.nMaxCustFilter = 0;
ofnTemp.nFilterIndex = 1;
ofnTemp.lpstrFile = (LPSTR)szName; // Stores the result in this variable
ofnTemp.nMaxFile = sizeof( szName );
ofnTemp.lpstrFileTitle = NULL;
ofnTemp.nMaxFileTitle = 0;
ofnTemp.lpstrInitialDir = NULL;
ofnTemp.lpstrTitle = "OPENFILENAME"; // Title for dialog
ofnTemp.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
ofnTemp.nFileOffset = 0;
ofnTemp.nFileExtension = 0;
ofnTemp.lpstrDefExt = "*";
ofnTemp.lCustData = 0L;
ofnTemp.lpfnHook = NULL;
ofnTemp.lpTemplateName = NULL;
if(GetOpenFileName( &ofnTemp ) != TRUE){
Errval=CommDlgExtendedError();
if(Errval!=0){ // 0 value means user selected Cancel
sprintf(buf,"%ld",Errval);
strcat(Errstr,buf);
MessageBox(hwnd,Errstr,"WARNING",MB_OK|MB_ICONSTOP);
}
}
if(szName){
if((hFile=_lopen(szName,READ))!=HFILE_ERROR){
_lread(hFile,&wavHdr,sizeof(wavHdr));
if(wavHdr.RIFF!=0x46464952L || // RIFF
wavHdr.wid!=0x45564157L || // WAVE header
wavHdr.fid!=0x20746d66L){ // FMT
_lclose(hFile);szName[0]=0;
MessageBox(hwnd,"Not a valid Wave File","WARNING",MB_OK|MB_ICONSTOP);
}
else {
SoundSize=wavHdr.dlen;
idTimer=SetTimer(hwnd,idTimer,10,NULL);
LoadData();
}
}
}
}
// If data pointers are near pointers
#if defined(__SMALL__) || defined(__MEDIUM__)
inline Window *GetPointer( HWND hWnd )
{
return (Window *) GetWindowWord( hWnd, 0 );
}
inline void SetPointer( HWND hWnd, Window *pWindow )
{
SetWindowWord( hWnd, 0, (WORD) pWindow );
}
// else pointers are far
#elif defined(__LARGE__) || defined(__COMPACT__) || defined(__FLAT__)
inline Window *GetPointer( HWND hWnd )
{
return (Window *) GetWindowLong( hWnd, 0 );
}
inline void SetPointer( HWND hWnd, Window *pWindow )
{
SetWindowLong( hWnd, 0, (LONG) pWindow );
}
#else
#error Choose another memory model!
#endif
LRESULT CALLBACK _export WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam){
Window *pWindow=GetPointer(hWnd);
if(pWindow==0){
if(iMessage==WM_CREATE){
LPCREATESTRUCT lpcs;
lpcs=(LPCREATESTRUCT)lParam;
pWindow=(Window*)lpcs->lpCreateParams;
SetPointer(hWnd,pWindow);
return pWindow->WndProc(iMessage,wParam,lParam);
}
else
return DefWindowProc(hWnd,iMessage,wParam,lParam);
}
else
return pWindow->WndProc(iMessage,wParam,lParam);
}
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow ){
TApp::hInstance = hInstance;
TApp::hPrevInstance = hPrevInstance;
TApp::nCmdShow = nCmdShow;
if ( ! TApp::hPrevInstance ) {
Window::Register();
}
Window MainWnd;
return TApp::MessageLoop();
}