Capturing Mouse Input Data
Introduction
Welcome back to System Events and Integration. We are now in Lesson 2 of the course, so we already have one message-based program behind us. As you may recall from the previous lesson, a WinAPI window does not keep asking for work in a manual loop; instead, Windows sends messages, and we respond to them.
In this lesson, we will make that idea more interactive. We will capture the position of a left mouse click, store the coordinates, and repaint the window so the click becomes visible. By the end, our window will draw a small marker where the user clicked and show the exact and values inside the client area.
From Mouse Click To Visible Result
Before we write code, it helps to see the flow clearly. When the user presses the left mouse button, Windows sends the message WM_LBUTTONDOWN. That message carries the mouse position inside lParam. We do not usually draw right there. Instead, we save the data, request a repaint, and let WM_PAINT handle the drawing.
One detail matters a lot here: the position inside lParam is packed. The and values are stored together, so we should extract them with GET_X_LPARAM and GET_Y_LPARAM from windowsx.h. Those macros handle the signed conversion correctly, which makes them safer than manually pulling apart the bits.
Preparing Headers And Saved Coordinates
We begin with the headers and a pair of stored coordinates. These values need to stay available after the click happens, so we place them outside the window procedure as static globals.
The windowsx.h header provides the mouse coordinate macros this lesson depends on. We initialize g_clickX and g_clickY to -1 to serve as a simple signal that no click has been recorded yet.
Building The Message Handler
Next, we set up the WindowProc function, which is the heart of the program. Just like our timer example, this function receives messages one by one and decides what to do with each of them.
This structure should feel familiar. WM_LBUTTONDOWN handles input, WM_PAINT handles drawing, and WM_DESTROY ends the application cleanly. Any message we do not process ourselves is passed to DefWindowProcW, which keeps the window behaving like a normal WinAPI window.
