Eliminate Blinking Cursor, MFC Style

Dear Dr. GUI:

I am using Microsoft Visual C++ 1.0 to develop an image processing application. Basically, I started my work by modifying code from an MFC sample, DIBLOOK. The application has a tool palette. If the user has selected a tool from the palette, the cursor will change to the selected tool shape (such as an eyedropper or magnifying glass) when the cursor is moved into the view. To achieve this, I called SetCursor in CDibView::OnMouseMove. The problem I run into is the cursor blinks between IDC_ARROW and IDC_EYEDROP when the mouse is moved. I suspect this is caused by the system restoring the previous shape of the class cursor of the view. What should I do to change the class cursor of windows created by the framework?

Michael Feng

Dr. GUI replies:

Well, Michael, you correctly guessed that the system is drawing the cursor registered with the window class before drawing yours; this is what is giving you the cursor "blink." Rather than call ::SetCursor from your view's OnMouseMove function, I would suggest overriding the OnSetCursor function in your CView derived class.

By using Spy to watch mouse messages sent to your view object (select Mouse And Input Messages from the Options menu), you will notice that WM_SETCURSOR and WM_MOUSEMOVE are sent in tandem whenever you move the mouse in the window's view. From this information, you can surmise that your code is calling the default implementation of OnSetCursor before you set the cursor again in OnMouseMove. Fortunately this is a pretty easy problem to fix, because you only need to make a message map entry for WM_SETCURSOR, via ClassWizard, and move your ::SetCursor call to the resultant message handler, CViewDerivedClass::OnSetCursor. If you'd like to retain the default cursor shape for child windows, such as the I-bar in edit controls and the arrow for check boxes and buttons, you should return FALSE from this function. To maintain the cursor shape you've set when you move over child windows, return TRUE. There you go!! Flash-free cursors!!

For specific implementation issues, please review the VIEWPROP sample in the January 1995 Development Library.