Win32 API Mouse interaction

Windows API
The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. All Windows programs except console programs must interact with the Windows API regardless of the language.

Win32 API
The Win32 API is the 32-bit API for modern versions of Windows. The API consists of functions implemented, as with Win16, in system DLLs. The core DLLs of Win32 are kernel32.dll, user32.dll, and gdi32.dll. Win32 was introduced with Windows NT.

Words of wisdom
Dealing with the Win32 API appears to be a regression since it takes us to the last century, that is, when programming with such API we are writing code that resembles the code written 15 years ago or more. Regression was the feeling I felt when the teacher said we'd study this API. Nevertheless, there are billions of lines of code that need maintenance because great part of these lines are used in legacy systems. So you see that learning this API is fundamental even today. This is in contrast with the mainframe computers dilemma. Even today there are a bunch of companies that still use them because of legacy systems. The feeling of regression was substituted by a enthusiasm one in the end.

Mouse interaction app
One coursework related to the Object Oriented Systems discipline I had to develop was a program that draws on the screen by free hand with the mouse assistance. The program must monitor the mouse movement, indicating its position (x, y) in the upright corner of the application window, in the format (xxx, yyy). Pressing the left mouse button it draws and pressing the right mouse button, it wipes off the screen content.

Some valuable tips that the teacher gave:
Use the events WM_MOUSE, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP and the function SetPixel()

lword(lParam) has 0 x
hiword(lParam) has 0 Y

I originally implemented this program using DevC++. Today while composing this post I just created a new Win32 Project application using the Microsoft Visual Studio C++ Express Edition. Although the implementation differs a little bit, it wasn't difficult to port it to the Microsoft programming environment. I'll provide both implementations at the end of the post.

Let's see the mouse app main block of code. It's inside the WndProc function that is responsible for processing the messages for the main window. The code is commented so I think it's unnecessary to add more to it.

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - Process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - Post a quit message and return
// WM_LBUTTONDOWN - Left mouse button clicked
// WM_RBUTTONDOWN - Right mouse button clicked
// WM_LBUTTONUP - Left mouse button released
// WM_RBUTTONUP - Right mouse button released
// WM_MOUSEMOVE - Controls the mouse movement
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;

switch(message) //Handle the messages
{
case WM_DESTROY:

PostQuitMessage(0); // Send a WM_QUIT to the message queue

break;

case WM_PAINT:
// TODO: Add any drawing code here...
hDC = GetDC(hWnd);

BeginPaint(hWnd, &paintStruct);

EndPaint(hWnd, &paintStruct);

break;

// Left button event used to print the screen
case WM_LBUTTONDOWN:

flag = true;

// Black color
newColor = RGB(0, 0, 0);

xMouse = LOWORD(lParam);

yMouse = HIWORD(lParam);

SetPixel(hDC, xMouse, yMouse, newColor);

break;

// Right button event used to erase the screen content
case WM_RBUTTONDOWN:

flag = true;

// White color
newColor = RGB(255, 255, 255);

xMouse = LOWORD(lParam);

yMouse = HIWORD(lParam);

SetPixel(hDC, xMouse, yMouse, newColor);

break;

// Sets the flag to false so that we know the left mouse button was released
case WM_LBUTTONUP:

flag = false;

break;

// Sets the flag to false so that we know the right mouse button was released
case WM_RBUTTONUP:

flag = false;

break;

// Controls the mouse movement and shows its current position on the Window title
case WM_MOUSEMOVE:

if(flag)
{
xMouse = LOWORD(lParam);

yMouse = HIWORD(lParam);

SetPixel(hDC, xMouse, yMouse, newColor);

sprintf_s(strTitle, " x=%d y=%d", xMouse, yMouse);

SetWindowText(hWnd, strTitle);

SetWindowText(hlabel, strTitle);
}
else
{
xMouse = LOWORD(lParam);

yMouse = HIWORD(lParam);

sprintf_s(strTitle, " x=%d y=%d", xMouse, yMouse);

SetWindowText(hWnd, strTitle);

SetWindowText(hlabel, strTitle);
}

break;

case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch(wmId)
{
case IDM_ABOUT:

DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

break;

case IDM_EXIT:

DestroyWindow(hWnd);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);
}

break;

default: // For messages that we don't deal with

return DefWindowProc (hWnd, message, wParam, lParam);
}

return 0;
}

Reference
To get more insight regarding the Win32 API, go to the Win32 Development site at the Microsoft Development Network: http://msdn.microsoft.com/en-us/library/aa139672.aspx

Dev-C++ and Visual Studio Projects
DevC++ project
http://leniel.googlepages.com/Win32APIMouseInteractAppDevCPlusPlus.zip

Visual Studio Win32 project
http://leniel.googlepages.com/Win32APIMouseInteractAppVCPlusPlus.zip