About Mikazo Tech Blog

My name is Mike, and this blog is my way of saving people time. If someone has a specific problem that I've encountered before, hopefully these posts will save them the extraneous Googling I had to go through to solve the same problem. Also, when I have something to say about technology today, I will post my thoughts here. If this blog has helped you out, even a little bit, vote on the poll below, or let me know by sending me an email. I'm always open to exchanging links with other blogs or websites that share a similar interest.

Tuesday, February 24, 2009

Windows Programming in C++: Mouse Coordinates without DirectInput

So I've been programming a 2D game in DirectX and C++ as a project and I've spent hours and hours fiddling around with how to get the mouse coordinates so I could draw my own cursor on the screen.

My first attempt at this was implementing DirectInput, which is no small task. Turns out, there's an easier way with just a few lines of code. Inside the Windows message loop, you can watch for WM_MOUSEMOVE messages, then act upon them.

Code snippet:


#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
while(TRUE) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break;
if (msg.message == WM_MOUSEMOVE) {
newX = GET_X_LPARAM(msg.lParam);
newY = GET_Y_LPARAM(msg.lParam);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}

2 comments:

Arafat Hossain said...

Nice article. Very helpful for programmer.

Stephen said...

I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.


Alanna

http://www.craigslistsimplified.info

Donate to Mikazo Tech Blog