File:  [WindowsNT SDKs] / mstools / samples / playit / playit.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:20:38 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-jun-1992, HEAD
Microsoft Windows NT Build 297 06-28-1992

#include <windows.h>
#include <mmsystem.h>
#include <string.h>
#include <stdio.h>

#if !defined(NT)
    #if defined (NOMM)
    // define 'NOMM' if you want to build this for a non-sound machine
        #define SND_SYNC    0
        #define SND_FILENAME    0
        #define SND_ALIAS       0
        BOOL APIENTRY PlaySound(LPSTR lpszName, HANDLE hModule, UINT flags)
        {
            MessageBox (GetFocus(), lpszName, "Beeeeeep", MB_OK);
            return TRUE;
        }
    #else
        #include <mmsystem.h>
        // Win32 has some extra flags that it is using...
        #define SND_FILENAME 0
        #define SND_ALIAS 0
    #endif
    // Here are some 'types' that Win32 uses that would be handy for Win16
    #define UINT    unsigned int
    #define INT     int
    #define APIENTRY    far pascal
    // The Win16 call is different from the Win32 one
    #define PlaySound(szFile, hMod, flags) sndPlaySound (szFile, flags)
#endif
#include <commdlg.h>
#include "playit.h"

char szAppName[] = "PlayIt";

HWND    hwndMain;
HWND    hwndChild;
HMENU   hmenuRes;
UINT    idMaxResource = IDM_RESOURCE;
WORD    TimerID     = 1;    /* number used for timer-id */
CHAR    szBuffer[200];    /* buffer for stringtable stuff */

long FAR PASCAL WndProc (HWND, UINT, UINT, LONG);
BOOL FAR PASCAL AboutDlgProc (HWND, UINT, UINT, LONG);
BOOL APIENTRY GetFileName(LPSTR);
BOOL FillResMenu (HMENU);
long FAR PASCAL ChildWndProc (HWND, UINT, UINT, LONG);

int APIENTRY WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
    HANDLE  hInstance, hPrevInstance;
    LPSTR   lpszCmdLine;
    int     nCmdShow;
{
    MSG     msg;
    WNDCLASS    wndclass;
    HMENU   hmenuApp;
	 PSTR       szTooMany;
	 CHAR       TempString[100];

#if defined (NT)
    nCmdShow = SW_SHOWNORMAL;
    hInstance = GetModuleHandle( NULL );
#endif

    if (!hPrevInstance) {
        wndclass.style      = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc    = (WNDPROC) WndProc;
        wndclass.cbClsExtra     = 0;
        wndclass.cbWndExtra     = 0;
        wndclass.hInstance      = hInstance;
        wndclass.hIcon      = LoadIcon (hInstance, szAppName);
        wndclass.hCursor    = LoadCursor (NULL, IDC_ARROW);
        wndclass.hbrBackground  = (HBRUSH)(COLOR_APPWORKSPACE+1);
        wndclass.lpszMenuName   = szAppName;
        wndclass.lpszClassName  = szAppName;

        if (!RegisterClass (&wndclass)) {
            return FALSE;
        }
        wndclass.style      = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc    = (WNDPROC) ChildWndProc;
        wndclass.cbClsExtra     = 0;
        wndclass.cbWndExtra     = 10;
        wndclass.hInstance      = hInstance;
        wndclass.hIcon      = LoadIcon (hInstance, szAppName);
        wndclass.hCursor    = LoadCursor (NULL, IDC_ARROW);
        wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName   = (HMENU) NULL;
        wndclass.lpszClassName  = "SoundChild";

        if (!RegisterClass (&wndclass)) {
            return FALSE;
        }



	 }

    hwndMain = CreateWindow (szAppName, szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, 300, 200,
        NULL, (HMENU) NULL, hInstance, NULL);

	 /* create non-visible child window pop up*/
    hwndChild = CreateWindow ("SoundChild", "SoundChild",
        WS_CHILD | WS_CAPTION,
        CW_USEDEFAULT, 0, 200, 100,
        hwndMain, (HMENU)NULL, hInstance, NULL);


    if (!SetTimer(hwndChild, TimerID, (UINT) 5000, 0L))
        {
        /* Windows only supports 16 public timers */
        if(!(szTooMany = (PSTR)LocalAlloc(LPTR, 160))){
            strcpy(TempString, " Too Many Timers Allocated ");
            szTooMany = TempString;
        }
        else{
            LoadString(hInstance, IDS_TOOMANY, (LPSTR)szTooMany, 160);
        }
        MessageBox((HWND)NULL, (LPSTR)szTooMany, (LPSTR)szBuffer, MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
        return(FALSE);
    }
		  		  
    hmenuApp = GetMenu (hwndMain);
    hmenuRes = CreatePopupMenu();
    if (FillResMenu(hmenuRes)) {
        if (InsertMenu (hmenuApp, GetMenuItemCount(hmenuApp)-1,
        MF_BYPOSITION | MF_POPUP, (UINT)hmenuRes, (LPSTR)"&Resources")) {
            DrawMenuBar (hwndMain);
        }
    }

    ShowWindow (hwndMain, nCmdShow);
    UpdateWindow (hwndMain);

    while (GetMessage (&msg, NULL, 0, 0)) {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    return msg.wParam;

    lpszCmdLine; // Just to resolve reference
}


BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
{
    RECT    rChild, rParent;
    int     wChild, hChild, wParent, hParent;
    int     wScreen, hScreen, xNew, yNew;
    HDC     hdc;

    GetWindowRect (hwndChild, &rChild);
    wChild = rChild.right - rChild.left;
    hChild = rChild.bottom - rChild.top;

    GetWindowRect (hwndParent, &rParent);
    wParent = rParent.right - rParent.left;
    hParent = rParent.bottom - rParent.top;

    hdc = GetDC (hwndChild);
    wScreen = GetDeviceCaps (hdc, HORZRES);
    hScreen = GetDeviceCaps (hdc, VERTRES);
    ReleaseDC (hwndChild, hdc);

    xNew = rParent.left + ((wParent - wChild) /2);
    if (xNew < 0) {
        xNew = 0;
    } else if ((xNew+wChild) > wScreen) {
        xNew = wScreen - wChild;
    }

    yNew = rParent.top  + ((hParent - hChild) /2);
    if (yNew < 0) {
        yNew = 0;
    } else if ((yNew+hChild) > hScreen) {
        yNew = hScreen - hChild;
    }

    return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}


BOOL APIENTRY AboutDlgProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
{
    INT wmId;

    switch (msg) {
        case WM_INITDIALOG:
            CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER));
            return (TRUE);

        case WM_COMMAND:
#if defined (NT)
            wmId = LOWORD(wParam);
#elif defined (WIN16)
            wmId = wParam;
#endif
        switch (wmId) {
            case IDOK:
            case IDCANCEL:
                EndDialog(hwnd, TRUE);
                return (TRUE);
        }
        break;
    }
    return (FALSE);

    /* Just For Reference */
    lParam;
}

UINT BeepFlags (UINT wParam)
{
    UINT flags = MB_OK;

    switch (wParam) {
        case IDM_SYNC_ICONHAND:
        case IDM_SYNC_ICONQUESTION:
        case IDM_SYNC_ICONEXCLAMATION:
        case IDM_SYNC_ICONASTERISK:
            flags |= MB_TASKMODAL;
            break;
    }

    switch (wParam) {
        case IDM_ICONHAND:
        case IDM_SYNC_ICONHAND:
            flags |= MB_ICONHAND;
            break;

        case IDM_ICONQUESTION:
        case IDM_SYNC_ICONQUESTION:
            flags |= MB_ICONQUESTION;
            break;

        case IDM_ICONEXCLAMATION:
        case IDM_SYNC_ICONEXCLAMATION:
            flags |= MB_ICONEXCLAMATION;
            break;

        case IDM_ICONASTERISK:
        case IDM_SYNC_ICONASTERISK:
            flags |= MB_ICONASTERISK;
            break;

        default:
            break;
    }
    return flags;
}



long FAR PASCAL WndProc (hwnd, iMessage, wParam, lParam)
     HWND   hwnd;
     UINT   iMessage;
     UINT   wParam;
     LONG   lParam;
{
static char szSoundName[80];
static UINT sndFlags;
    HANDLE  hInst;
    FARPROC lpDlgProc;
    UINT    wmId, wmEvent;
    char    szBuf[25];
    char    szErr[25];
    UINT    flags;
    DWORD   err, i;

#if defined (WIN16)
    hInst = GetWindowWord (hwnd, GWW_HINSTANCE);
#elif defined (NT)
    hInst = GetModuleHandle (NULL);
#endif

    switch (iMessage) {
        case WM_CREATE:
            szSoundName[0] = (char)0;
            break;

        case WM_LBUTTONDOWN:
            if (lstrlen(szSoundName) > 0) {
                PlaySound (szSoundName, hInst, sndFlags);
            }
            break;

        case WM_COMMAND:
#if defined (NT)
            wmId = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
#elif defined (WIN16)
            wmId = wParam;
            wmEvent = HIWORD(lParam);
#endif

            switch (wmId) {
                case IDM_OPEN:
                    if (GetFileName (szSoundName)) {
                        sndFlags = SND_ASYNC | SND_FILENAME;

                        /* SND_FILENAME doesn't seem to work, so...
                        WriteProfileString ("SOUNDS", "PLAYIT", szSoundName);
                        sndFlags = SND_ASYNC | SND_ALIAS;
                        strcpy (szSoundName, "PLAYIT");
                        */


                        sndFlags = SND_SYNC | SND_FILENAME;
                        // SND_FILENAME is currently having problems
                        // with path slashes... so lets switch in
                        // something that it can handle for now...
                        for (i=0; i<strlen(szSoundName); i++) {
                            if (szSoundName[i] == '\\')
                                szSoundName[i] = '/';
                        }

                        if (!PlaySound(szSoundName, hInst, sndFlags)) {
                            err = GetLastError();
                            sprintf (szErr, "Error: %i [0x%x]", err, err);
                            MessageBox (GetFocus(), szErr, szSoundName, MB_OK);
                        }

                    }
                    break;

                case IDM_ABOUT:
                    lpDlgProc = MakeProcInstance((FARPROC) AboutDlgProc, hInst);
		    DialogBox(hInst, "AboutBox", hwnd, lpDlgProc);
                    FreeProcInstance(lpDlgProc);
                    break;

                case IDM_HELP_CONTENTS:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"CONTENTS");
                    break;
                case IDM_HELP_INDEX:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"INDEX");
                    break;
                case IDM_HELP_OVERVIEW:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"OVERVIEW");
                    break;
                case IDM_HELP_GLOSSARY:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"GLOSSARY");
                    break;
                case IDM_HELP_TUTORIAL:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"TUTORIAL");
                    break;
                case IDM_HELP_DEMO:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"DEMO");
                    break;
                case IDM_HELP_HELP:
                    WinHelp (hwnd, "PlayIt.HLP", HELP_KEY, (DWORD)"HELP");
                    break;


                case IDM_EXIT:
                    DestroyWindow(hwnd);
                    break;


                default:
						  if (wmId >= IDM_RESOURCE && wmId <= idMaxResource) {
                        if (hmenuRes) {
                            GetMenuString (hmenuRes, wmId, szSoundName, sizeof(szBuf),
                                MF_BYCOMMAND);
									  sndFlags = SND_SYNC | SND_ALIAS;
									  switch(szSoundName[6]){
										 
											case 'H':
											case 'h':
									 
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_ICONHAND, NULL), (LONG)szSoundName);	
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_UNHIDE, NULL), (LONG)szSoundName);
												break;

											case 'Q':
											case 'q':

												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_ICONQUESTION, NULL), (LONG)szSoundName);	
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_UNHIDE, NULL), (LONG) szSoundName);
												break;

											case 'E':
											case 'e':

												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_ICONEXCLAMATION, NULL), (LONG)szSoundName);	
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_UNHIDE, NULL), (LONG) szSoundName);
												break;

											case 'A':
											case 'a':

												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_ICONASTERISK, NULL), (LONG)szSoundName);	
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_UNHIDE, NULL), (LONG) szSoundName);
												break;

											default:  
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_SYSTEMDEFAULT, NULL), (LONG) szSoundName);	
												SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_UNHIDE, NULL), (LONG) szSoundName);
									 }                                 
                            if (!PlaySound (szSoundName, hInst, sndFlags)) {
                                //err = GetLastError ();
                                //sprintf (szErr, "Error: %i [0x%x]", err, err);
                                MessageBox (hwnd, "ERROR", "PlaySound() Called SYNC failed", MB_OK);
                            }
									 SendMessage(hwndChild, WM_COMMAND, MAKELONG(IDM_HIDE, NULL), (LONG) szSoundName);     

                        }
                    }
                    break;

            }
            break;

        case WM_DESTROY:
            if (hwnd == hwndMain) {
					 DestroyWindow(hwndChild);
                PostQuitMessage (0);
            }
            break;

        default:
            return DefWindowProc (hwnd, iMessage, wParam, lParam);
    }
    return 0L;

    hInst; //Just to resolve reference
    flags; // ditto
}

#define ID_TEST 321
#define ID_FN   1152
#define ID_CANCEL IDCANCEL

BOOL FAR PASCAL PlaySoundFileHook (HWND hdlg, UINT msg, UINT wParam, LONG lParam)
{
    HWND hwndTest;
    HWND hwndCancel;
    char szTmp[256];
    UINT cy, i;
    RECT rect;
    HANDLE hInst;
    WORD wId;

    hInst = GetModuleHandle (NULL);

    switch (msg) {
        case WM_INITDIALOG:

            /* Determine proper placement of the 'TEST' button */
            hwndCancel = GetDlgItem (hdlg, ID_CANCEL);
            if (hwndCancel!=0) {
                GetWindowRect (hwndCancel, &rect);
                cy = rect.bottom - rect.top;
                rect.top = rect.bottom + cy;
                rect.bottom = rect.top +cy;
                ScreenToClient (hdlg, (LPPOINT)&rect.left);
                ScreenToClient (hdlg, (LPPOINT)&rect.right);

                hwndTest = CreateWindow ("BUTTON", "&Test",
                    BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP,
                    rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
                    hdlg, (HMENU)ID_TEST, hInst, 0L);
                if (hwndTest==0) {
                    MessageBox (GetFocus(), "No Test Button", "Open", MB_OK);
                } else {
                    ShowWindow (hwndTest, SW_SHOW);
                }

            } else {
                MessageBox (GetFocus(), "Unable to find cancel button", "OpenFile", MB_OK);
            }

            break;

        case WM_COMMAND:
            wId = LOWORD(wParam);
            switch (wId) {
                case ID_TEST:
                    /* The 'Viewer >>' button was pressed. */
                    GetWindowText (GetDlgItem(hdlg, ID_FN), szTmp, sizeof(szTmp));
                    for (i=0; i<(UINT)strlen(szTmp); i++) {
                        if (szTmp[i] == '\\')
                            szTmp[i] = '/';
                    }

                    PlaySound(szTmp, NULL, SND_ASYNC | SND_FILENAME );

            }
        break;
    }
    return (FALSE);

    lParam; // Just to reference it
}



BOOL APIENTRY GetFileName(LPSTR lpstr)
{
    OPENFILENAME ofn;
    char szFilterSpec [128] =               /* file type filters */
         "Sound Files(*.WAV)\0*.WAV\0";

#define MAXFILENAME 256
    char szFileName[MAXFILENAME];
    char szFileTitle[MAXFILENAME];

    lstrcpy(szFileName, "");   /* these need be NULL*/
    lstrcpy(szFileTitle, "");

    /* fill in non-variant fields of OPENFILENAME struct. */
    ofn.lStructSize     = sizeof(OPENFILENAME);
    ofn.hwndOwner       = GetFocus();
    ofn.lpstrFilter     = szFilterSpec;
    ofn.lpstrCustomFilter   = NULL;
    ofn.nMaxCustFilter  = 0;
    ofn.nFilterIndex    = 0;
    ofn.lpstrFile       = szFileName;
    ofn.nMaxFile        = MAXFILENAME;
    ofn.lpstrInitialDir = "..\\WAV";
    ofn.lpstrFileTitle  = szFileTitle;
    ofn.nMaxFileTitle   = MAXFILENAME;
    ofn.lpstrTitle      = "Open Sound File";
    ofn.lpstrDefExt     = "WAV";
    ofn.Flags           = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY
                          | OFN_PATHMUSTEXIST | OFN_ENABLEHOOK;
    ofn.lpfnHook        = (LPOFNHOOKPROC) PlaySoundFileHook;

    /* Use standard open dialog */
    if (!GetOpenFileName ((LPOPENFILENAME)&ofn)){
        *lpstr = NULL;
        return FALSE;
    }

    lstrcpy(lpstr, ofn.lpstrFile);
    return TRUE;

}


#define BUFSIZE 1024

BOOL FillResMenu (HMENU hMenu)
{
    HANDLE hBuf;
    LPSTR lpBuf, lpString;
    UINT cnt, idMenu;
	 CHAR	tempstring[30];

    hBuf = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, BUFSIZE);
	 if(!hBuf){
		 return(FALSE);
	 }
    lpBuf = GlobalLock (hBuf);

    cnt = GetProfileString ("sounds", NULL, "", lpBuf, BUFSIZE);
    idMenu = IDM_RESOURCE;
    if (cnt>0) {
        lpString = lpBuf;
        while (*lpString) {
				strcpy(tempstring, lpString);	/* this code is a hack for a user bug*/
				strcat(tempstring, " ");
            AppendMenu (hMenu, MF_STRING, idMenu++, tempstring);
            lpString += lstrlen(lpString) +1;
        }
    }
    idMaxResource = idMenu-1;

    GlobalUnlock (hBuf);
    GlobalFree (hBuf);

    return (idMaxResource > IDM_RESOURCE);
}


long FAR PASCAL ChildWndProc (hwnd, iMessage, wParam, lParam)
     HWND   hwnd;
     UINT   iMessage;
     UINT   wParam;
     LONG   lParam;
{



	 WORD wmId;
	 WORD wmEvent;
	 HDC	hDc;
	 HICON hIcon;
	 PAINTSTRUCT ps;
	 static BOOL bWindowDone;
	 static BOOL bHideWindow;
	 
    switch (iMessage) {

		  case WM_PAINT:
			  hDc = BeginPaint(hwnd, &ps);
			  if((hIcon = (HICON) GetWindowLong(hwnd, 0))){
					DrawIcon(ps.hdc, 80, 20, hIcon);
			  }
			  EndPaint(hwnd, &ps);
			  bWindowDone = TRUE;
			  break;


		  case WM_TIMER:
			  /* timer set, because USER coalesces the UNHIDEHIDE, such that
			     window never showed.  This way, I never issue SW_HIDE, until, the window
				  has been shown*/
				  
			  if(bHideWindow == TRUE && bWindowDone == TRUE){
				  bWindowDone = FALSE;
				  bHideWindow = FALSE;
				  SetWindowLong(hwnd, 0, 0);
				  ShowWindow(hwnd, SW_HIDE); 
			  }
		  	  break;
			  
        case WM_COMMAND:
#if defined (NT)
            wmId = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
#elif defined (WIN16)
            wmId = wParam;
            wmEvent = HIWORD(lParam);
#endif

            switch (wmId) {
					case IDM_ICONHAND:
						SetWindowText(hwnd, (LPSTR) lParam);
						hIcon = LoadIcon(NULL, IDI_HAND);
						SetWindowLong(hwnd, 0, (LONG) hIcon);
						break;
						
					case IDM_ICONQUESTION:
						SetWindowText(hwnd, (LPSTR) lParam);
						hIcon = LoadIcon(NULL, IDI_QUESTION);
						SetWindowLong(hwnd, 0, (LONG) hIcon);
						break;
						
					case IDM_ICONEXCLAMATION:
						SetWindowText(hwnd, (LPSTR) lParam);
						hIcon = LoadIcon(NULL, IDI_EXCLAMATION);
						SetWindowLong(hwnd, 0, (LONG) hIcon);
						break;
						
					case IDM_ICONASTERISK:   
						SetWindowText(hwnd, (LPSTR) lParam);
						hIcon = LoadIcon(NULL, IDI_ASTERISK);
						SetWindowLong(hwnd, 0, (LONG) hIcon);
						break;

					case IDM_SYSTEMDEFAULT:		/* for System Default*/
						SetWindowText(hwnd, (LPSTR) lParam);
						SetWindowLong(hwnd, 0, 0);
						break;    /* NO ICON*/
						
					case IDM_HIDE:
						bHideWindow = TRUE;
						break;

					case IDM_UNHIDE:
						ShowWindow(hwnd, SW_SHOW);
						break;
						
            }
				break;

        default:
            return DefWindowProc (hwnd, iMessage, wParam, lParam);
    }
    return 0L;


}


unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.