• 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
  • 相关阅读:
    codevs 4511 信息传递(NOIP2015 day1 T2)
    caption标签,为表格添加标题和摘要
    用css样式,为表格加入边框
    table标签,认识网页上的表格
    认识div在排版中的作用
    使用ol,添加图书销售排行榜
    使用ul添加列表
    使用<pre>标签为你的网页加入大段代码
    想加入一行代码吗?使用<code>标签
    <address>标签,为网页加入地址信息
  • 原文地址:https://www.cnblogs.com/galoishelley/p/3598235.html
Copyright © 2020-2023  润新知