Here's an example. Your program displays several columns of alphabetically sorted files similar to the File Manager file windows. The file list starts at the top of the client area, which is cxClient pixels wide and cyClient pixels high; each character is cyChar pixels high. The filenames are stored in a sorted array of pointers to character strings called szFileNames.
Let's assume that the columns are cxColWidth pixels wide. The number of files you can fit in each column is:
nNumInCol = cyClient / cyChar ;
You receive a mouse click message with the coordinates cxMouse and cyMouse derived from lParam. You can determine which column of filenames the user is pointing to by using the formula:
nColumn = cxMouse / cxColWidth ;
The position of the filename in relation to the top of the column is:
nFromTop = cyMouse / cyChar ;
Now you can calculate an index to the szFileNames array:
nIndex = nColumn * nNumInCol + nFromTop ;
Obviously, if nIndex exceeds the number of files in the array, the user is clicking on a blank area of the display.
In many cases, hit-testing is more complex than this example suggests. For instance, it can become very messy in a word processing program that uses variable font sizes (such as WRITE). When you display something to the client area, you must determine the coordinates for each item you display. In hit-testing calculations, you must go backward from the coordinates to the object. However, if the objects you display are strings, then going backward involves finding the character position within the string.