Home > database >  Getting a "expected a ')' " error in win32 api c
Getting a "expected a ')' " error in win32 api c

Time:11-15

I have defined ID_BUTTON as 1 and when I try to run the code:

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

I get an error saying "expected a ')' "

It works fine if I put NULL instead of "(HMENU) ID_BUTTON", what am I missing?

#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);

}

CodePudding user response:

#define ID_BUTTON 1;

You defined the ID_BUTTON macro as "1;". Macros work as nothing more than a search/replace function. Therefore, after macro expansion, the relevant line now reads:

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

The syntax error should now be obvious. Just drop the semicolon. Macro directives are not C statements that must be followed by a semicolon. They live in their own little bubble of the C universe.

  • Related