Tooltips in MFC Views
Some years ago I decided to write a simple application to manage my genealogic family tree.
I realized that it would have been impossible to represent my tree and my wife tree in the conventional form of a tree, I decided to write a window exe to manage all data and relations of the person that would update instantly (or so) the schema view in a simple manner.
At the end of the process I would have produced each piece of the schema by cutting wood by cnc machine I produced for this scope. The software will help to create gcode for this.
In this instructable I won't explain how I did use of MFC to create the schema, but only how to realize a simple tooltip generator by hand, without the use of standard Microsoft classes.
Supplies
For this project I used Microsoft Visual Studio version 2022. It's free downloadable and usable from microsoft.com, by choosing the community edition.
In the initial config you must enable visual c++ module with mfc applications.
MFC stands for Microsoft Foundation Class and it's a complete set of libraries freely usable (by 1995) that consent to create standard win32 applications, like word processors.
Simply speaking, Microsoft encapsulated win32 controls and libraries in the form of a complete set of c++ classes. You extends your classes and apply for your applications.
My recent project was a genealogic tree generator, that accepts names and other data of my family members and creates a panel that contains all persons posed in a elliptical form. My objective was to concretely realize the frame using wood that I can cut by my cnc and hang it to the wall.
The software also helps me to take an idea of the dimensions, given that my family is composed of several hundreds of members and this will need a big wall to be hanged up.
During code writing I realized that I would have need the capability to see the names of the components simply moving the mouse over a person, on the frame. In fact, every time I click on a person the application shows all the data of the person selected and it also shows all the family linked directly to the person, by highlighting them on the schema.
For this reason, I would need a method to explore the table without selecting a person, simply passing with mouse over it.
Simply speaking I would need a classic tooltip.
Searching in the mfc classes I expected to find something useful but I found only some classes usable in other contests, like tooltips linked to controls in a form, or tooltips for menu items.
I found no clear examples available for my use case so I decided to write something by my self.
This is not complicated, since I have been writing the first applications with mfc since 1995, and I know the framework in a decent manner and the documentation is clear and complete.
The first pass was to apply for a little timer to be activated every time someone moves the mouse and then stills over a position. To detect mouse move over a view you must intercept the WM_MOUSEMOVE event of window.
This can be done adding a map to message maps of the view (into the view.cpp file), like this:
and defining the method in .h file of the view:
Now you can write the method in cpp file, like this:
The variable m_idtimer maintains the value returned by SetTimer instruction, and must be used to kill the timer every time the mouse stops moving. You need to save the current position of the mouse, in a variable called m_lastmovepoint; that point will be used later to place the tooltip window near mouse position.
After that, you must activate a new timer specifying duration (1000 ms). This produce a sort of interrupt that calls a method of the view called onTimer.
That method must be declared in .h file:
and implemented in .cpp file:
Here I read the saved position of the cursor when the timer started (m_lastmovepoint) and I use it to search in Person array for the person situated in that position and I put its data into the tooltip window in the form of a simple CString.
Now the window can be positioned (near the person) and showed, using SetWindowPos method.
The SWP_NOSIZE parameter is used to indicate the window doesn't need to be resized, since thw same method can be used to move or to size a window.
The variable m_wndtooltip is the reference variable to a cwnd window that's initialized in the OnInitialUpdate() method of the view class:
The class name STATIC is used here to create a label window, that is a window used to write a static text.