Visual Studio C++ PlaySound标识符未定义

ghhkc1vu  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(201)

我正在浏览Charles Petzold - Programming Windows(5th Edition)第3章。我有一个错误,说明PlaySound函数未定义。在那里我需要包含某种头文件来使用这个函数?

/*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
HELLOWIN.C −− Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"),
            szAppName, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindow(szAppName, // window class name
        TEXT("The Hello Program"), // window caption
        WS_OVERLAPPEDWINDOW, // window style
        CW_USEDEFAULT, // initial x position
        CW_USEDEFAULT, // initial y position
        CW_USEDEFAULT, // initial x size
        CW_USEDEFAULT, // initial y size
        NULL, // parent window handle
        NULL, // window menu handle
        hInstance, // program instance handle
        NULL); // creation parameters
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

字符串
更多评论:playsound应该是法律的功能基于链接:https://learn.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    switch (message)
    {
    case WM_CREATE:
        PlaySound(TEXT("HelloWin.wav"), NULL, SND_FILENAME | SND_ASYNC);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        DrawText(hdc, TEXT("Hello, Windows 98!"), -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

cpjpxq1n

cpjpxq1n1#

链接到库并包含头文件:

#include <Windows.h>
#include <iostream>
#pragma comment(lib, "Winmm.lib")
#include <mmsystem.h>

int main()
{
    PlaySound(TEXT("HelloWin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    std::getchar();

    return 0;
}

字符串

pwuypxnk

pwuypxnk2#

这是一个很好的代码示例,用于播放当前现代API中没有提到的声音。这是为想要使用此代码的用户准备的:
Chap02的Petold代码包含这部分代码:这里我们有两个字符串用于播放sound( file name, ...)DrawText(hdc, "text to draw") .... )这需要修改const char = old ansii字符串类型的两个字符串。
1.如前所述,使用TEXT("solved the problem") 错误状态LPCWSTR L(..),type,哪个不起作用?但TEXT确实起作用,并与标题一起编译,如回复中所述。
1.第二个字符串错误要求使用'DrawTextW()'现在放置LPCWSTR L(...),没有工作。DrawTextW(hdc, LPCWSTR (  "My First Window" )确实接受了,'LPCWSTR(“...”), but without , ....L(“...”)`,
代码是从Chap02文件中读取的.c,并使用“写字板”作为文本文件打开,因为它没有长文本的分页,然后应用于编译的winproc...

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;
    switch (iMsg)
    {
        case WM_CREATE:
            //PlaySound("hellowin.wav", NULL, SND_FILENAME | SND_ASYNC);
            //LPCWSTR message1 = L("hellowin.wav");
            PlaySound(TEXT( "hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            // LPCWSTR message = L"My First Window";
            DrawTextW(hdc, 
                      LPCWSTR (  "My First Window" ), 
                      -1, 
                      &rect, 
                      DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hwnd, &ps);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, iMsg, wParam, 
    lParam);
}

字符串

相关问题