home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / audio / midiplyr / midiplyr.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  242 lines

  1. /*****************************************************************************
  2. *
  3. *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. *  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  5. *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR
  6. *  A PARTICULAR PURPOSE.
  7. *
  8. *  Copyright (C) 1993 - 1997 Microsoft Corporation. All Rights Reserved.
  9. *
  10. ******************************************************************************
  11. *
  12. * MIDIPlyr.C
  13. *
  14. * Initialization code for the polymessage MIDI playback app.
  15. *
  16. *****************************************************************************/
  17.  
  18. #pragma warning(disable:4756)
  19.  
  20. #define _INC_SHELLAPI
  21. #include <windows.h>
  22. #undef _INC_SHELLAPI
  23.  
  24. #include <shellapi.h>
  25. #include <windowsx.h>
  26. #include <mmsystem.h>
  27. #include <commdlg.h>
  28. #include <commctrl.h>
  29. #include <ctype.h>
  30.  
  31. #include "debug.h"
  32.  
  33. #include "MIDIPlyr.H"
  34.  
  35. PUBLIC  char BCODE      gszMWndClass[]      = "MIDIPlyrMWnd";
  36. PUBLIC  char BCODE      gszTWndClass[]      = "MIDIPlyrTWnd";
  37. PUBLIC  HINSTANCE       ghInst              = NULL;
  38. PUBLIC  PSEQ            gpSeq               = NULL;
  39. PUBLIC  char            gszUntitled[80]     = "";
  40. PUBLIC  char            gszAppLongName[80]  = "";
  41. PUBLIC  char            gszAppTitleMask[80] = "";
  42. PUBLIC  char            grgszTimeFormats[N_TIME_FORMATS][CB_TIME_FORMATS] = {0};
  43. PUBLIC  RECT            grcTWnd             = {0, 0, 0, 0};
  44. PUBLIC  int             gnTimeFormat        = 0;
  45.  
  46. PRIVATE BOOL FNLOCAL InitApp(HINSTANCE hInst);
  47. PRIVATE BOOL FNLOCAL InitInstance(HINSTANCE hInst, int nCmdShow);
  48. PRIVATE VOID FNLOCAL TerminateInstance(VOID);
  49.  
  50. /*****************************************************************************
  51. *
  52. * WinMain
  53. *
  54. * Called by C startup code.
  55. *
  56. * HANDLE hInst              - Instance handle of this instance
  57. * HANDLE hPrevInst          - Instance handle of previous instance or
  58. *                             NULL if we are the first instance
  59. * LPSTR lpstrCmdLine        - Any command line arguments
  60. * int nCmdShow              - Code for ShowWindow which tells us what state
  61. *                             to initially show the main application window.
  62. *
  63. * Initialize application if first instance.
  64. * Initialize instance.
  65. * Stay in main message processing loop until exit.
  66. *
  67. *****************************************************************************/
  68. int PASCAL WinMain(
  69.     HANDLE                  hInst,
  70.     HANDLE                  hPrevInst,
  71.     LPSTR                   lpstrCmdLine,
  72.     int                     nCmdShow)
  73. {
  74.     MSG                     msg;
  75.  
  76.     if (hPrevInst == NULL)
  77.         if (!InitApp(hInst))
  78.             return 0;
  79.  
  80.     if (!InitInstance(hInst, nCmdShow))
  81.     {
  82.         TerminateInstance();
  83.         return 0;
  84.     }
  85.  
  86.     while (GetMessage(&msg, NULL, 0, 0))
  87.     {
  88.         TranslateMessage(&msg);
  89.         DispatchMessage(&msg);
  90.     }
  91.  
  92.     TerminateInstance();
  93.  
  94.     return (msg.wParam);
  95. }
  96.  
  97. /*****************************************************************************
  98. *
  99. * InitApp
  100. *
  101. * Called for one-time initialization if we are the first app instance.
  102. *
  103. * HANDLE hInst              - Instance handle of this instance
  104. *
  105. * Returns TRUE on success.
  106. *
  107. * Register the window class for the main window and the time window.
  108. *
  109. *****************************************************************************/
  110. BOOL FNLOCAL InitApp(
  111.     HINSTANCE               hInst)
  112. {
  113.     WNDCLASS                wc;
  114.  
  115.     InitCommonControls();
  116.  
  117.     /* Don't specify CS_HREDRAW or CS_VREDRAW if you're going to use the
  118.     ** commctrl status or toolbar -- invalidate the (remaining) client
  119.     ** area yourself if you want this behavior. This will allow the child
  120.     ** control redraws to be much more efficient.
  121.     */
  122.     wc.style =          0;
  123.     wc.lpfnWndProc =    MWnd_WndProc;
  124.     wc.cbClsExtra =     0;
  125.     wc.cbWndExtra =     0;
  126.     wc.hInstance =      hInst;
  127.     wc.hIcon =          LoadIcon(hInst, MAKEINTRESOURCE(ID_ICON));
  128.     wc.hCursor =        LoadCursor(NULL, IDC_ARROW);
  129.     wc.hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
  130.     wc.lpszMenuName =   MAKEINTRESOURCE(ID_MENU);
  131.     wc.lpszClassName =  (LPCSTR)gszMWndClass;
  132.  
  133.     RegisterClass(&wc);
  134.  
  135.     wc.style =          CS_HREDRAW|CS_VREDRAW;
  136.     wc.lpfnWndProc =    TWnd_WndProc;
  137.     wc.cbClsExtra =     0;
  138.     wc.cbWndExtra =     0;
  139.     wc.hInstance =      hInst;
  140.     wc.hIcon =          LoadIcon(hInst, MAKEINTRESOURCE(ID_ICON));
  141.     wc.hCursor =        LoadCursor(NULL, IDC_ARROW);
  142.     wc.hbrBackground =  (HBRUSH)(COLOR_WINDOW+1);
  143.     wc.lpszMenuName =   NULL;
  144.     wc.lpszClassName =  (LPCSTR)gszTWndClass;
  145.  
  146.     RegisterClass(&wc);
  147.  
  148.     return TRUE;
  149. }
  150.  
  151. /*****************************************************************************
  152. *
  153. * InitInstance
  154. *
  155. * Called once per instance of application.
  156. *
  157. * HANDLE hInst              - Instance handle of this instance
  158. * int nCmdShow              - Code for ShowWindow which tells us what state
  159. *                             to initially show the main application window.
  160. * Returns TRUE on success.
  161. *
  162. * Initialize debug library.
  163. * Save the instance handle.
  164. * Load global resource strings.
  165. * Allocate and initialize the global sequencer structure.
  166. * Create the main window.
  167. * Add time formats to the options menu.
  168. * Show the main window.
  169. *
  170. *****************************************************************************/
  171. BOOL FNLOCAL InitInstance(
  172.     HINSTANCE               hInst,
  173.     int                     nCmdShow)
  174. {
  175.     HWND                    hWnd;
  176.     int                     idx;
  177.  
  178.     DbgInitialize(TRUE);
  179.  
  180.     ghInst = hInst;
  181.  
  182.     LoadString(hInst, IDS_APPTITLEMASK, gszAppTitleMask, sizeof(gszAppTitleMask));
  183.     LoadString(hInst, IDS_APPNAME,      gszAppLongName,  sizeof(gszAppLongName));
  184.     LoadString(hInst, IDS_UNTITLED,     gszUntitled,     sizeof(gszUntitled));
  185.  
  186.     for (idx = 0; idx < N_TIME_FORMATS; idx++)
  187.     {
  188.         *grgszTimeFormats[idx] = '\0';
  189.         LoadString(hInst,
  190.                    IDS_TF_FIRST+idx,
  191.                    grgszTimeFormats[idx],
  192.                    sizeof(grgszTimeFormats[idx]));
  193.     }
  194.  
  195.     if ((gpSeq = (PSEQ)LocalAlloc(LPTR, sizeof(SEQ))) == NULL)
  196.         return FALSE;
  197.  
  198.     gpSeq->cBuffer  = C_MIDI_BUFFERS;
  199.     gpSeq->cbBuffer = CB_MIDI_BUFFERS;
  200.  
  201.     if (seqAllocBuffers(gpSeq) != MMSYSERR_NOERROR)
  202.         return FALSE;
  203.  
  204.     hWnd = CreateWindow(
  205.         gszMWndClass,
  206.         NULL,
  207.         WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
  208.         CW_USEDEFAULT,CW_USEDEFAULT,
  209.         CW_USEDEFAULT,CW_USEDEFAULT,
  210.         HWND_DESKTOP,
  211.         NULL,
  212.         hInst,
  213.         NULL);
  214.  
  215.     if (hWnd == (HWND)NULL)
  216.         return FALSE;
  217.  
  218.     gpSeq->hWnd = hWnd;
  219.  
  220.     ShowWindow(hWnd, nCmdShow);
  221.  
  222.     return TRUE;
  223. }
  224.  
  225. /*****************************************************************************
  226. *
  227. * TerminateInstance
  228. *
  229. * Release any resources for the current instance
  230. *
  231. *****************************************************************************/
  232. VOID FNLOCAL TerminateInstance(
  233.     VOID)
  234. {
  235.     if (gpSeq != NULL)
  236.     {
  237.         seqFreeBuffers(gpSeq);
  238.         LocalFree((HLOCAL)gpSeq);
  239.         gpSeq = NULL;
  240.     }
  241. }
  242.