home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sdk_win.ddi / SAMPLE / DLLMIDI / DLLMIDI.C next >
Encoding:
C/C++ Source or Header  |  1991-06-13  |  19.0 KB  |  534 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: DLLMidi.c
  4.  
  5.     PURPOSE: DLLMidi template for Sound Blaster applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain () - calls initialization function, processes message loop
  10.     InitApplication () - initializes window data and registers window
  11.     InitInstance () - saves instance handle and creates main window
  12.     MainWndProc () - processes messages
  13.     About () - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time. The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include "windows.h"        /* required for all Windows applications */
  24. #include "dllmidi.h"        /* specific to this program              */
  25. #include <string.h>
  26. #include <sndblst.h>
  27.  
  28. WORD     wDLLVersion;        /* verion number of DLL                  */
  29. WORD     wSoundBlasterMsg;   /* Sound Blaster message                 */
  30. LPSTR    lpCardName;         /* name of Sound Blaster card            */
  31. HANDLE   hInst;              /* current instance                      */
  32. HWND     hWnd;               /* Main window handle.                   */
  33. BYTE     cNoteNum[8]={60, 62, 64, 65, 67, 69, 71, 72};
  34.  
  35. HWND     hEditWnd;           /*                                       */
  36. RECT     Rect;               /*            Variables                  */
  37. unsigned no_of_lines;        /*               for                     */
  38. unsigned count=0;            /*             display                   */
  39. char     szEditBuf[1024];    /*             purposes                  */
  40. BOOL     fScroll=FALSE;      /*                                       */
  41.  
  42. /****************************************************************************
  43.  
  44.     FUNCTION: WinMain (HANDLE, HANDLE, LPSTR, int)
  45.  
  46.     PURPOSE: calls initialization function, processes message loop
  47.  
  48.     COMMENTS:
  49.  
  50.         Windows recognizes this function by name as the initial entry point 
  51.         for the program.  This function calls the application initialization 
  52.         routine, if no other instance of the program is running, and always 
  53.         calls the instance initialization routine.  It then executes a message 
  54.         retrieval and dispatch loop that is the top-level control structure 
  55.         for the remainder of execution. The loop is terminated when a WM_QUIT
  56.         message is received, at which time this function exits the application 
  57.         instance by returning the value passed by PostQuitMessage(). 
  58.  
  59.         If this function must abort before entering the message loop, it 
  60.         returns the conventional value NULL.  
  61.  
  62. ****************************************************************************/
  63.  
  64. int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  65. HANDLE hInstance;                /* current instance             */
  66. HANDLE hPrevInstance;            /* previous instance            */
  67. LPSTR lpCmdLine;                 /* command line                 */
  68. int nCmdShow;                     /* show-window type (open/icon) */
  69. {
  70.     MSG msg;                     /* message                      */
  71.  
  72.     if (!hPrevInstance)               /* Other instances of app running?   */
  73.     if (!InitApplication (hInstance)) /* Initialize shared things          */
  74.         return (FALSE);               /* Exits if unable to initialize     */
  75.  
  76.     /* Perform initializations that apply to a specific instance */
  77.  
  78.     if (!InitInstance (hInstance, nCmdShow))
  79.         return (FALSE);
  80.  
  81.     if ((wDLLVersion = sbcGetDLLVersion ()) == NULL)
  82.         return (FALSE);
  83.  
  84.     if ((lpCardName = sbcGetCardName ()) == NULL)
  85.         return (FALSE);
  86.  
  87.     wSoundBlasterMsg = RegisterWindowMessage ((LPSTR) "SoundBlaster");
  88.  
  89.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  90.  
  91.     while (GetMessage (&msg,     /* message structure                      */
  92.         NULL,                    /* handle of window receiving the message */
  93.         NULL,                    /* lowest message to examine              */
  94.         NULL))                   /* highest message to examine             */
  95.     {
  96.         TranslateMessage (&msg); /* Translates virtual key codes           */
  97.         DispatchMessage (&msg);  /* Dispatches message to window           */
  98.     }
  99.  
  100.     sbcTerminateDLL ();
  101.  
  102.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  103. }
  104.  
  105.  
  106. /****************************************************************************
  107.  
  108.     FUNCTION: InitApplication (HANDLE)
  109.  
  110.     PURPOSE: Initializes window data and registers window class
  111.  
  112.     COMMENTS:
  113.  
  114.         This function is called at initialization time only if no other 
  115.         instances of the application are running.  This function performs 
  116.         initialization tasks that can be done once for any number of running 
  117.         instances.  
  118.  
  119.         In this case, we initialize a window class by filling out a data 
  120.         structure of type WNDCLASS and calling the Windows RegisterClass() 
  121.         function.  Since all instances of this application use the same window 
  122.         class, we only need to do this when the first instance is initialized.  
  123.  
  124. ****************************************************************************/
  125.  
  126. BOOL InitApplication (hInstance)
  127. HANDLE hInstance;                       /* current instance                   */
  128. {
  129.     WNDCLASS  wc;
  130.  
  131.     /* Fill in window class structure with parameters that describe the       */
  132.     /* main window.                                                           */
  133.  
  134.     wc.style         = NULL;             /* Class style(s).                    */
  135.     wc.lpfnWndProc   = MainWndProc;      /* Function to retrieve messages for  */
  136.                                          /* windows of this class.             */
  137.     wc.cbClsExtra    = 0;                /* No per-class extra data.           */
  138.     wc.cbWndExtra    = 0;                /* No per-window extra data.          */
  139.     wc.hInstance     = hInstance;        /* Application that owns the class.   */
  140.     wc.hIcon         = LoadIcon (hInstance, "dllmidi");
  141.     wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
  142.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  143.     wc.lpszMenuName  = "DLLMidiMenu";    /* Name of menu resource in .RC file. */
  144.     wc.lpszClassName = "DLLMidiWClass";  /* Name used in call to CreateWindow. */
  145.  
  146.     /* Register the window class and return success/failure code. */
  147.  
  148.     return (RegisterClass (&wc));
  149. }
  150.  
  151.  
  152. /****************************************************************************
  153.  
  154.     FUNCTION:  InitInstance (HANDLE, int)
  155.  
  156.     PURPOSE:  Saves instance handle and creates main window
  157.  
  158.     COMMENTS:
  159.  
  160.         This function is called at initialization time for every instance of 
  161.         this application.  This function performs initialization tasks that 
  162.         cannot be shared by multiple instances.  
  163.  
  164.         In this case, we save the instance handle in a static variable and 
  165.         create and display the main program window.  
  166.         
  167. ****************************************************************************/
  168.  
  169. BOOL InitInstance (hInstance, nCmdShow)
  170.     HANDLE          hInstance;          /* Current instance identifier.       */
  171.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  172. {
  173.     /* Save the instance handle in static variable, which will be used in  */
  174.     /* many subsequence calls from this application to Windows.            */
  175.  
  176.     hInst = hInstance;
  177.  
  178.     /* Create a main window for this application instance.  */
  179.  
  180.     hWnd = CreateWindow
  181.     (
  182.         "DLLMidiWClass",                /* See RegisterClass() call.          */
  183.         "DLLMIDI Sample Application",   /* Text for window title bar.         */
  184.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  185.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  186.         CW_USEDEFAULT,                  /* Default vertical position.         */
  187.         CW_USEDEFAULT,                  /* Default width.                     */
  188.         CW_USEDEFAULT,                  /* Default height.                    */
  189.         NULL,                           /* Overlapped windows have no parent. */
  190.         NULL,                           /* Use the window class menu.         */
  191.         hInstance,                      /* This instance owns this window.    */
  192.         NULL                            /* Pointer not needed.                */
  193.     );
  194.  
  195.     /* If window could not be created, return "failure" */
  196.  
  197.     if (!hWnd)
  198.         return (FALSE);
  199.  
  200.     *szEditBuf = '\0';
  201.  
  202.     /* Create a child window */
  203.  
  204.     hEditWnd = CreateWindow("Edit",
  205.     NULL,
  206.     WS_CHILD | WS_VISIBLE |
  207.     ES_MULTILINE |
  208.     ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  209.     0,
  210.     0,
  211.     (Rect.right-Rect.left),
  212.     (Rect.bottom-Rect.top),
  213.     hWnd,
  214.     IDC_EDIT,                          /* Child control i.d. */
  215.     hInst,
  216.     NULL);
  217.  
  218.     if (!hEditWnd)
  219.     {
  220.         DestroyWindow(hWnd);
  221.         return (NULL);
  222.     }
  223.  
  224.     strcpy (szEditBuf, " Time Received    Midi Code");
  225.     SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
  226.  
  227.     /* Make the window visible; update its client area; and return "success" */
  228.  
  229.     ShowWindow (hWnd, nCmdShow);   /* Show the window                        */
  230.     UpdateWindow (hWnd);           /* Sends WM_PAINT message                 */
  231.     return (TRUE);                 /* Returns the value from PostQuitMessage */
  232. }
  233.  
  234. /*****************************************************************************
  235.  
  236.     FUNCTION: DisplayMidiCode (LONG)
  237.  
  238.     PURPOSE: Display MIDI code and time-stamp received
  239.  
  240. *****************************************************************************/
  241.  
  242. void DisplayMidiCode (wMidiCode, dwTimeStamp)
  243. WORD wMidiCode;
  244. DWORD dwTimeStamp;
  245. {
  246.     char szTmpBuf[64];
  247.     char *ptr;
  248.  
  249.     wsprintf (szTmpBuf, "\r         %06lx               %02x", dwTimeStamp, wMidiCode);
  250.     strcat (szEditBuf, szTmpBuf);
  251.     SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
  252.  
  253.     if (fScroll)
  254.     {
  255.         ptr = strchr (szEditBuf+28, '\r');
  256.         strcpy (szEditBuf+27, ptr);
  257.     }
  258.     else
  259.     {
  260.         count++;
  261.         if (count > no_of_lines) fScroll = TRUE;
  262.     }
  263. }
  264.  
  265. /****************************************************************************
  266.  
  267.     FUNCTION: MainWndProc (HWND, unsigned, WORD, LONG)
  268.  
  269.     PURPOSE:  Processes messages
  270.  
  271.     MESSAGES:
  272.  
  273.     WM_COMMAND    - application menu (About dialog box)
  274.     WM_DESTROY    - destroy window
  275.  
  276.     COMMENTS:
  277.  
  278.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  279.     current instance address of the About() function.  Then call Dialog
  280.     box which will create the box according to the information in your
  281.     dllmidi.rc file and turn control over to the About() function.  When
  282.     it returns, free the intance address.
  283.  
  284. ****************************************************************************/
  285.  
  286. long FAR PASCAL MainWndProc (hWnd, message, wParam, lParam)
  287. HWND hWnd;                    /* window handle                   */
  288. unsigned message;             /* type of message                 */
  289. WORD wParam;                  /* additional information          */
  290. LONG lParam;                  /* additional information          */
  291. {
  292.     unsigned i;
  293.     char *ptr;
  294.     HDC hDC;
  295.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  296.     TEXTMETRIC textmetric;
  297.  
  298.     switch (message)
  299.     {
  300.         case WM_COMMAND:       /* message: command from application menu */
  301.             switch (wParam)
  302.             {
  303.                 case IDM_ABOUT:
  304.                     lpProcAbout = MakeProcInstance (About, hInst);
  305.  
  306.                     DialogBox(hInst,         /* current instance         */
  307.                         "AboutBox",          /* resource to use          */
  308.                         hWnd,                /* parent handle            */
  309.                         lpProcAbout);        /* About() instance address */
  310.  
  311.                     FreeProcInstance (lpProcAbout);
  312.                     break;
  313.                 case IDM_INPUT:
  314.                     StartInputMidi ();
  315.                     break;
  316.                 case IDM_STOP:
  317.                     StopInputMidi ();
  318.                     break;
  319.                 case IDM_OUTPUT:
  320.                     OutputMidi ();
  321.                     break;
  322.                 default:
  323.                     return (DefWindowProc (hWnd, message, wParam, lParam));
  324.             }
  325.             break;
  326.         case WM_SIZE:
  327.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  328.             hDC = GetDC (hWnd);
  329.             GetTextMetrics (hDC, &textmetric);
  330.             ReleaseDC (hWnd, hDC);
  331.             GetClientRect(hWnd, &Rect);
  332.             if (fScroll)
  333.                 count = no_of_lines;
  334.             no_of_lines = (Rect.bottom-Rect.top-textmetric.tmExternalLeading)/
  335.                 (textmetric.tmExternalLeading+textmetric.tmHeight)-3;
  336.             if (fScroll)
  337.             {
  338.                 for (i=no_of_lines-1; i<count; i++)
  339.                 {
  340.                     ptr = strchr (szEditBuf+28, '\r');
  341.                     strcpy (szEditBuf+27, ptr);
  342.                 }
  343.                 SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
  344.                 fScroll = FALSE;
  345.                 count++;
  346.             }
  347.             break;
  348.         case WM_DESTROY:          /* message: window being destroyed */
  349.             PostQuitMessage(0);
  350.             break;
  351.  
  352.         default:
  353.             if (message == wSoundBlasterMsg)
  354.             {
  355.                 switch (wParam)
  356.                 {
  357.                     case SBM_CALLBACK:
  358.                         sbcCallBack (lParam);
  359.                         break;
  360.                     case SBM_MIDI_IN:
  361.                         DisplayMidiCode ((WORD) lParam & 0xff, (DWORD) lParam >> 8);
  362.                         break;
  363.                 }
  364.             }
  365.             else                  /* Passes it on if unproccessed    */
  366.             {
  367.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  368.             }
  369.     }
  370.     return (NULL);
  371. }
  372.  
  373. /*****************************************************************************
  374.  
  375.     FUNCTION: StartInputMidi (void)
  376.  
  377.     PURPOSE: Starts to input MIDI code
  378.  
  379. *****************************************************************************/
  380.  
  381. void StartInputMidi (void)
  382. {
  383.     WORD wReturnCode;
  384.  
  385.     if (wReturnCode = midiOpenInputDevice (hWnd))
  386.         DisplayErrorCode (wReturnCode);
  387.     else
  388.     {
  389.         if (wReturnCode = midiStartInput ())
  390.             DisplayErrorCode (wReturnCode);
  391.     }
  392. }
  393.  
  394. /*****************************************************************************
  395.  
  396.     FUNCTION: StopInputMidi (void)
  397.  
  398.     PURPOSE: Cease to input MIDI code
  399.  
  400. *****************************************************************************/
  401.  
  402. void StopInputMidi ()
  403. {
  404.     WORD wReturnCode;
  405.  
  406.     if (wReturnCode = midiStopInput ())
  407.         DisplayErrorCode (wReturnCode);
  408.     else
  409.         midiCloseInputDevice ();
  410. }
  411.  
  412. /*****************************************************************************
  413.  
  414.     FUNCTION: OutputMidi (void)
  415.  
  416.     PURPOSE: Output an octave of notes starting from middle "C"
  417.  
  418. *****************************************************************************/
  419.  
  420. void OutputMidi ()
  421. {
  422.     WORD wReturnCode;
  423.     WORD wNote, wDelayTime;
  424.  
  425.     if (wReturnCode = midiOpenOutputDevice (hWnd))
  426.         DisplayErrorCode (wReturnCode);
  427.     else
  428.     {
  429.         midiOutputShortMessage (0xC0, 0x8, 0);
  430.         midiOutputShortMessage (0xC1, 0x88, 0);
  431.  
  432.         for (wNote=0; wNote<8; wNote++)
  433.         {
  434.             midiOutputShortMessage (0x90, cNoteNum[wNote], 0x40);
  435.             midiOutputShortMessage (0x91, cNoteNum[wNote], 0x40);
  436.  
  437.             for (wDelayTime=0; wDelayTime<30000; wDelayTime++);
  438.  
  439.             midiOutputShortMessage (0x80, cNoteNum[wNote], 0x40);
  440.             midiOutputShortMessage (0x81, cNoteNum[wNote], 0x40);
  441.         }
  442.         midiCloseOutputDevice ();
  443.     }
  444. }
  445.  
  446. /****************************************************************************
  447.  
  448.     FUNCTION: DisplayErrorCode (WORD)
  449.  
  450.     PURPOSE: Display error code
  451.  
  452. ****************************************************************************/
  453.  
  454. void DisplayErrorCode (error)
  455. WORD error;
  456. {
  457.     char szTmpBuf[20];
  458.  
  459.     wsprintf (szTmpBuf, "Error code = %d", error);
  460.     MessageBeep (0);
  461.     MessageBox (NULL, szTmpBuf, "Error...", MB_ICONINFORMATION | MB_OK);
  462. }
  463.  
  464. /****************************************************************************
  465.  
  466.     FUNCTION: About (HWND, unsigned, WORD, LONG)
  467.  
  468.     PURPOSE:  Processes messages for "About" dialog box
  469.  
  470.     MESSAGES:
  471.  
  472.     WM_INITDIALOG - initialize dialog box
  473.     WM_COMMAND    - Input received
  474.  
  475.     COMMENTS:
  476.  
  477.     No initialization is needed for this particular dialog box, but TRUE
  478.     must be returned to Windows.
  479.  
  480.     Wait for user to click on "Ok" button, then close the dialog box.
  481.  
  482. ****************************************************************************/
  483.  
  484. BOOL FAR PASCAL About (hDlg, message, wParam, lParam)
  485. HWND hDlg;                                /* window handle of the dialog box */
  486. unsigned message;                         /* type of message                 */
  487. WORD wParam;                              /* message-specific information    */
  488. LONG lParam;
  489. {
  490.     HDC     hDC;
  491.     HDC     hMemoryDC;
  492.     char    szTmpBuf[128];
  493.     BITMAP  Bitmap;
  494.     HBITMAP hBmSBLogo;
  495.     HBITMAP hOldBitmap;
  496.  
  497.     switch (message)
  498.     {
  499.         case WM_INITDIALOG:           /* message: initialize dialog box */
  500.             wsprintf (szTmpBuf, "Hardware Card: %s", lpCardName);
  501.             SetDlgItemText (hDlg, IDT_CARDNAME, szTmpBuf);
  502.             wsprintf (szTmpBuf, "DLL Version: %d.%02d", HIBYTE (wDLLVersion),
  503.                                                         LOBYTE (wDLLVersion));
  504.             SetDlgItemText (hDlg, IDT_DLLVERSION, szTmpBuf);
  505.             return (TRUE);
  506.  
  507.         case WM_COMMAND:                      /* message: received a command */
  508.             if (wParam == IDOK                /* "OK" box selected?          */
  509.                     || wParam == IDCANCEL)    /* System menu close command?  */
  510.             {
  511.                 EndDialog (hDlg, TRUE);       /* Exits the dialog box        */
  512.                 return (TRUE);
  513.             }
  514.             break;
  515.         case WM_PAINT:
  516.             hBmSBLogo = LoadBitmap (hInst, "sbct");
  517.             GetObject (hBmSBLogo, sizeof (BITMAP), (LPSTR) &Bitmap);
  518.             hDC = GetDC (hDlg);
  519.             hMemoryDC = CreateCompatibleDC (hDC);
  520.             hOldBitmap = SelectObject (hMemoryDC, hBmSBLogo);
  521.             if (hOldBitmap)
  522.             {
  523.                 BitBlt (hDC, 3, 3, Bitmap.bmWidth, Bitmap.bmHeight,
  524.                         hMemoryDC, 0, 0, SRCCOPY);
  525.                 SelectObject (hMemoryDC, hOldBitmap);
  526.             }
  527.             DeleteDC (hMemoryDC);
  528.             ReleaseDC (hDlg, hDC);
  529.             DeleteObject (hBmSBLogo);
  530.             break;
  531.     }
  532.     return (FALSE);                  /* Didn't process a message    */
  533. }
  534.