在win32 api c++中获取“预期a ')'“错误

k2fxgqgv  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(140)

我已经将ID_BUTTON定义为1,当我尝试运行代码时:

CreateWindow(L"Button", L"TEst", style, monitor.right / 2 - 100, 200, 100, 50, m_hWnd, (HMENU) ID_BUTTON, NULL, NULL);

我收到一个错误消息,表示“预期为')'“
如果我用NULL代替“(HMENU)ID_BUTTON”,它就能正常工作,我遗漏了什么?

#include "Window.h"

#define ID_BUTTON 1;
RECT monitor; // deminsions of monitor

Window::Window() : m_hInst(GetModuleHandle(nullptr)) //creates the window
{
    WNDCLASS wc = {};

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = m_hInst;
    wc.lpszClassName = ClassName;
    wc.lpfnWndProc = WindProc;

    RegisterClass(&wc);
    
    DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;

    GetWindowRect(GetDesktopWindow(), &monitor);

    m_hWnd = CreateWindow(ClassName, WindowTitle, style, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
    ShowWindow(m_hWnd, SW_MAXIMIZE);
}

Window::~Window()
{
    UnregisterClass(ClassName, m_hInst);
}

LRESULT CALLBACK WindProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) // gets input from user
{
    switch (msg) {
    case WM_CREATE:
        AddControls(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_WINDOWPOSCHANGED:
        std::cout << "1";
        break;
    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }
    return 1;
}

bool Window::ProcessMessage()
{
    MSG msg = {};
    while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
        if (msg.message == WM_QUIT)
            return false;

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    
    return true;
}

void AddControls(HWND m_hWnd)
{
    DWORD style = WS_VISIBLE | WS_CHILD | SS_CENTER;

    HWND title = CreateWindow(L"static", L"Welcome", style, monitor.right/2 - 100, 100, 200, 100, m_hWnd, NULL, NULL, NULL);
    SendMessage(title, WM_SETFONT, WPARAM(CreateFont(50, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
        OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial")), true);
    CreateWindow(L"Button", L"TEst", style, monitor.right / 2 - 100, 200, 100, 50, m_hWnd, (HMENU) ID_BUTTON, NULL, NULL);

}
taor4pac

taor4pac1#

#define ID_BUTTON 1;

您已将ID_BUTTON宏定义为“1;“。宏的作用只是搜寻/取代函数。因此,在宏展开之后,相关行现在为:

CreateWindow(L"Button", L"TEst", style, monitor.right / 2 - 100, 200, 100,
50, m_hWnd, (HMENU) 1;, NULL, NULL);

语法错误现在应该很明显了。只要去掉分号就行了。宏指令不是必须后跟分号的C语句。它们生活在C宇宙中自己的小泡泡里。

相关问题