win32 调用多媒体函数PlaySound()

必须引入此头文件

#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")

 1 /*------------------------------------------------------------
 2    HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
 3                  (c) Charles Petzold, 1998
 4   ------------------------------------------------------------*/
 5 
 6 #include "StdAfx.h"
 7 #include <windows.h>
 8 #include <mmsystem.h>
 9 #pragma comment(lib, "WINMM.LIB")
10  
11 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
12 
13 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
14                     PSTR szCmdLine, int iCmdShow)
15 {
16      static TCHAR szAppName[] = TEXT ("HelloWin") ;
17      HWND         hwnd ;
18      MSG          msg ;
19      WNDCLASS     wndclass ;
20 
21      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
22      wndclass.lpfnWndProc   = WndProc ;
23      wndclass.cbClsExtra    = 0 ;
24      wndclass.cbWndExtra    = 0 ;
25      wndclass.hInstance     = hInstance ;
26      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
27      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
28      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
29      wndclass.lpszMenuName  = NULL ;
30      wndclass.lpszClassName = szAppName ;
31 
32      if (!RegisterClass (&wndclass))
33      {
34           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
35                       szAppName, MB_ICONERROR) ;
36           return 0 ;
37      }
38      
39      hwnd = CreateWindow (szAppName,                  // window class name
40                           TEXT ("The Hello Program"), // window caption
41                           WS_OVERLAPPEDWINDOW,        // window style
42                           CW_USEDEFAULT,              // initial x position
43                           CW_USEDEFAULT,              // initial y position
44                           CW_USEDEFAULT,              // initial x size
45                           CW_USEDEFAULT,              // initial y size
46                           NULL,                       // parent window handle
47                           NULL,                       // window menu handle
48                           hInstance,                  // program instance handle
49                           NULL) ;                     // creation parameters
50      
51      ShowWindow (hwnd, iCmdShow) ;
52      UpdateWindow (hwnd) ;
53      
54      while (GetMessage (&msg, NULL, 0, 0))
55      {
56           TranslateMessage (&msg) ;
57           DispatchMessage (&msg) ;
58      }
59      return msg.wParam ;
60 }
61 
62 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
63 {
64      HDC         hdc ;
65      PAINTSTRUCT ps ;
66      RECT        rect ;
67      
68      switch (message)
69      {
70      case WM_CREATE:
71           PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
72           return 0 ;
73           
74      case WM_PAINT:
75           hdc = BeginPaint (hwnd, &ps) ;
76           
77           GetClientRect (hwnd, &rect) ;
78           
79           DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
80                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
81           
82           EndPaint (hwnd, &ps) ;
83           return 0 ;
84           
85      case WM_DESTROY:
86           PostQuitMessage (0) ;
87           return 0 ;
88      }
89      return DefWindowProc (hwnd, message, wParam, lParam) ;
90 }

需要注意根目录下存在音频文件

hellowin.wav
原文地址:https://www.cnblogs.com/galoishelley/p/3598235.html