Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

[SOLVED] Win32 - Getting Mouse Position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Rhob   Members   -  Reputation: 202

Like
0Likes
Like

Posted 09 May 2009 - 06:58 AM

Hey guys. I just started looking into Win32 API programming, so I'm a total noob here. So far, I've been able to capture the WM_MOUSEMOVE events correctly. My test program outputs the current mouse coordinates to the window, as long as the cursor is in the window. However, the coordinate values sometimes look... funny. Below is the source code for your viewing pleasure. With my luck, there's probably something obvious that I missed. :P Oh by the way, I'm using the Code::Blocks IDE and the MINGW32 compiler.
#include <sstream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>

using namespace std;

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

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

int WINAPI WinMain(HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpszArgument,
                   int nCmdShow)
{
    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 */

    char result[50];        /* Buffer for RegisterClassEx() result output */

    /* 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 colour as the background of the window */
    wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

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

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx(
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template 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, nCmdShow);
//    UpdateWindow(hwnd);

    sprintf(result, "Result from first call to RegisterClassEx is %X", atom);
    MessageBox(hwnd, result, "Hello", MB_OK);

    sprintf(result, "Result from second call to RegisterClassEx is %X", RegisterClassEx(&wincl));
    MessageBox(hwnd, result, "Hello", MB_OK);

    /* 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)
{
    PAINTSTRUCT paintContext;
    HDC deviceContext;
    stringstream output;

    switch(message)                   /* handle the messages */
    {
        case WM_MOUSEMOVE:
            output << GET_X_LPARAM(lParam) << ", " << GET_Y_LPARAM(lParam);
            text = output.str();
            InvalidateRect(hwnd, NULL, false);
            UpdateWindow(hwnd);
            break;
        case WM_LBUTTONDOWN:
            text = "Left button down!";
            UpdateWindow(hwnd);
            break;
        case WM_PAINT:
            deviceContext = BeginPaint(hwnd, &paintContext);
            TextOut(deviceContext, 0, 0, text.c_str(), lstrlen(text.c_str()));
            EndPaint(hwnd, &paintContext);
            break;
        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;
}



[Edited by - RobAU78 on May 9, 2009 6:12:04 PM]

Sponsor:

#2 Evil Steve   Moderators   -  Reputation: 1918

Like
0Likes
Like

Posted 09 May 2009 - 07:10 AM

Quote:
Original post by RobAU78
I just started looking into Win32 API programming, so I'm a total noob here. So far, I've been able to capture the WM_MOUSEMOVE events correctly. My test program outputs the current mouse coordinates to the window, as long as the cursor is in the window. However, the coordinate values sometimes look... funny. Below is the source code for your viewing pleasure. With my luck, there's probably something obvious that I missed. :P
Define "funny"? If you want to capture the mouse input while it's over other windows, you can use the GetCursorPos function to get the cursor position in screen space, and then use ScreenToClient to convert that into client coordinates (Which are the coordinates WM_MOUSEMOVE uses).

#3 Rhob   Members   -  Reputation: 202

Like
0Likes
Like

Posted 09 May 2009 - 07:19 AM

Quote:
Original post by Evil Steve
Define "funny"? If you want to capture the mouse input while it's over other windows, you can use the GetCursorPos function to get the cursor position in screen space, and then use ScreenToClient to convert that into client coordinates (Which are the coordinates WM_MOUSEMOVE uses).


Sorry, you're right.

By "funny" I mean that the mouse coordinates will suddenly appear 10x larger in the window. However, this seems to be related to the coordinates in the other dimension going down to e.g. the two-decimal range...

To be honest, this may not be a problem with the mouse coordinates per se, but with how I'm displaying them on the screen. Perhaps there's something I'm missing with refreshing the text output. Another artifact is that, when I hold the left mouse button down in the window, the text properly becomes "Left button down!", but once I start moving the mouse again, the text coordinates are merely overlaid over that message.

Hope this helps!

#4 theOcelot   Members   -  Reputation: 494

Like
0Likes
Like

Posted 09 May 2009 - 08:21 AM

Mingw is not built into Code::Blocks. I'm really not sure where you got that idea.

#5 Rhob   Members   -  Reputation: 202

Like
0Likes
Like

Posted 09 May 2009 - 08:32 AM

Quote:
Original post by theOcelot
Mingw is not built into Code::Blocks. I'm really not sure where you got that idea.


Actually you're right. I remember that I downloaded Code::Blocks and Mingw. I'll edit my post accordingly.


#6 Rhob   Members   -  Reputation: 202

Like
0Likes
Like

Posted 09 May 2009 - 12:11 PM

Okay, I figured out what I was doing wrong. I needed to change the value of the third parameter for InvalidateRect() to TRUE. This will tell Windows to erase the background of the update region. Once I fixed that, it worked fine.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS