Hello, I am trying to get the coordinates of the cursor when the user left clicks the mouse. I read that you can use this:

if (WM_LBUTTONDOWN)
{
     int x = LOWORD( LPARAM );
     int y = HIWORD( LPARAM );
}

However, when i compile, it gives me this error:
expected primary-expression before ')' token

I have included <windows.h> (don't really know if i need to), and it still doesn't work. I am using Code::Blocks and MinGW. Any ideas?:?:

Can you show the entire script you're running for this?

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int x, y;
    
    if (WM_LBUTTONDOWN)
    {
        x = LOWORD(LPARAM);
        y = HIWORD(LPARAM);
    }
}

Correct me if I'm wrong, but when running a windows application where you're collecting messages from the event queue, aren't you supposed to use a completely different program entry point?

http://msdn.microsoft.com/en-us/library/ms633559%28VS.85%29.aspx

Actually, upon some sifting I found a potentially useful set of functions in that Microsoft document.

http://msdn.microsoft.com/en-us/library/ms632654%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms632655%28VS.85%29.aspx

Extended documentation for collecting mouse input also from msdn--

http://msdn.microsoft.com/en-us/library/ms645610%28VS.85%29.aspx

Thanks. How do i use WinMain?

Hmm its been awhile since I made a windows-based application.

From what I remember there was a loop running in my WinMain that blocked until a message was sent to the msg queue. Events that fired were collected into a buffer and handled in a linear manner.

It's probably better if another developer on this site answers your question, or you look into programming with the windows api to write your own windows programs.

Usually, each windows-based program has some sort of shell for collecting events/msgs where you can code around it or within it to handle the events. Look for them on google, I'm sure a number of them are around for you to modify and use for your own purpose(s).

This is the auto generated "starting code" made by Dev-C++ when you selected a win32 GUI project.

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

This is just how to make a basic window. In order to get the mouse coords you have to look up what you need from the msdn site and then put it into WindowProcedure function at the bottom of the code I posted.

Personally I hate WINAPI and have just been using glut (for OpenGL) to do windowing for me but I do plan on learning some more WINAPI so I can include features that they miss out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.