Recently, I had a project that required the user to be able to click on the screen (in the client window) and
get info about the item he clicked on. This also had to work if the client window had been scrolled. While not
a tough programming problem, I had to spend a couple of hours on the MSDN CD trying to get things sorted out in
my already full brain. To help others who don't have the MSDN CDs (or the time to spend weeding through the MSDN
CDs), I'm publishing some sample code here.
The sample program is a SDI application created in VC6 using AppWizard. No functionality was added to the program
other than the drawing and mouse tracking that I wish to demonstrate.
- Tracking the mouse cursor in a view
- Updating main status bar from a view
- Changing the current cursor
- Converting a point to values relative to various points on the screen
- Clickable regions in a scrolled window
How It Works
The actual drawing of the items on the screen is pretty much a trivial issue, so I won't go over actual mechanics
of the drawing itself. The sample draws six groups of six items which include a group header, and a label and a
horizontal bar for each group sub-item. These bars represent a fictional percentage value used solely for the purpose
of making the output resemble something slightly useful.
As each item is drawn, I save the label and the bounding rectangle coordinates for that item into a CTypedPtrArray.
Once the screen is drawn, the real fun starts.
Tracking The Mouse
The program tracks and displays the mouse's current position in the status bar. Three values are displayed -
the actual client window position (as calculated form the top/left corner of the client window), the desktop position
(relative to the top/left corner of the windows desktop), and finally, the logical client window position (relative
to the scrolled top/left corner of the client window). This last position is the one we use for the rest of the
When the user left-clicks in the client window, the program cycles through all of the saved regions in the CTypedPtrArray,
and if the point is contained within the previously saved rectangle coordinates, the program displays a message
box, telling the user which area he clicked on.
As an added bonus, the cursor changes to a cross whenever it is positioned within a "clickable" area,