Ask the C++ Pro 10-Minute Solutions

How to Change the Mouse Pointer without Flicker: a 10-Minute Solution
By Jonathan Wood

Microsoft knowledgebase article Q131991 describes three techniques to change the mouse cursor in an MFC application. One method involves overriding CWnd::PreCreateWindow() to register your own window class that has the mouse pointer you want. This method works well if you want to use that mouse pointer all the time.

To dynamically change the mouse pointer, the article suggests overriding CWnd:: OnSetCursor. This works but, unfortunately, causes an annoying flicker as MFC sets its own mouse pointer and then your code sets it back again.

If you have an application that needs to change between several pointers and you don't want any flicker, here's a technique that worked well for me. The first step is to override PreCreateWindow as the Microsoft article suggests. However, instead of specifying our own mouse pointer here, specify a NULL pointer. This prevents Windows or MFC from doing anything at all with the mouse pointer.

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
  // Create our own class which has no cursor 
  // so that we can change it as needed
  if (cs.lpszClass == NULL)
	  cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
  return CScrollView::PreCreateWindow(cs);
}
Because the window class does not have any prespecified mouse pointer, we effectively eliminate the flicker. (Note that this example also creates a window class with no background brush, which you'll need to paint the background yourself. To specify a background brush, just pass it as the third argument to AfxRegisterWndClass.)

No flicker is nice, but now we've got no mouse pointer either! Never fear. An easy solution is to set the pointer in the OnMouseMove handler. This works out to be a convenient place to set the pointer if you need to change it based on where the mouse is within your window.

void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
  // Set cursor to indicate current operation
  if (m_nOperation == OPERATION_1)
    ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
  else if (m_nOperation == OPERATION_2)
    ::SetCursor(AfxGetApp()->LoadStandardCursor( ??? ));
  else   // Normal pointer
    ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
}
It's a little extra work, but if your application calls for different mouse pointers without flicker, here's a way to get the job done.

 
Other 10-Minute Solutions
 How to Change the Mouse Pointer without Flicker
 Setting Full Row Selection in ListView Control
 Automating Type Conversions with stringstream Objects
 Improving Memory Reallocation with Vectors
 How to Use <fstream> Classes for File I/O
 Casting About for Safe Typecasting
 Overloading Operator + the Right Way
 How to Create Persistent Objects
 Making Linked Lists More User-Friendly
 Preventing Glitches in Signal Processing
 Forcing Object Allocation on the Free-store
 Using String-Based Data Validation
 Implementing the 'Resource Acquisition Is Initialization' Idiom
 Simple Locks for Data Files
 Template Specializations
 Exception Handling
 Using Bit Fields in Data Optimization
 Using the Transform() Algorithm to Change a String's Case
 Use RTTI for Dynamic Type Identification
 Choosing the Right swap () Implementation
 Take Charge and Initialize Your Own Data
 Share Data Among Objects Using the Monostate Design Pattern
 String Manipulation Made Easy with std::string Algorithms
 Using typedef to Curb Miscreant Code
 Managing Objects' Construction Order
 Bitwise Operators: Combining Efficiency and Ease of Use
 Use Function Adapters to Extend Generic Algorithms' Usage
 Simplify Callback Dispatching with Enumerated Indexes
 Streamline Your Bulk I/O Operations with Stream Iterators
 Optimize Your Member Layout
 Preserve Code Safety with Conversion Operators
 Modify Your Base Class Interface in Derived Classes
 Tackle Common Programming Tasks Using the New <tuple> Library
 Use Local Classes for Proper Cleanup in Exception-enabled Apps
 Use multimap to Create Associative Containers with Duplicate Keys
 Enforcing Compile-time Constraints
 Facilitate Directory Operations with the <dirent.h> and <dir.h> Libraries
 Spruce Up Your Built-in Arrays
 Target 32- and 64-bit Platforms Together with a Few Simple Datatype Changes
 Restrict Object Allocation to Specific Memory Types
 Use the Pimpl Idiom to Reduce Compilation Time and Enhance Encapsulation
 Automate Resource Management with shared_ptr
 The Quick and Dirty Way to Add
 Pointing to Class Members
 Detecting Keystrokes While Your Application is Busy
 Linked Lists
 Programming the System Tray
 Create a "Universal" DLL
 Convert Path to Long Path Name
 Constructing an Object at a Pre-Determined Memory Position
 Declaring Classes and Member Functions in a Namespace
 Using the auto_ptr Class Template to Facilitate Dynamic Memory Management
 Using the random_shuffle() Algorithm to Randomize a Sequence of Elements
 Defining a Function Object
 Implementing the Singleton Design Pattern
 Declaring Function Pointers and Implementing Callbacks
 Overloading Operator << for a User-Defined Type
 Implementing a Stopwatch Class for Performance Measurements
 Creating and Accessing Environment Variables
 Executing an Object's Member Function in a Separate Thread
 Creating Heterogeneous Containers
 Overriding New and Delete
 Time and Date Manipulation
  Defining Functions with a Variable Argument List
 Optimize Abstract Operations with Function Templates




Sponsored Links


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers