Practical Learning Logo

Changing a Cursor in a Dialog Box

 

Introduction

Some applications, like drawing programs, use various mouse cursors depending on what is going on. Unlike most other libraries, the MFC doesn't make it easy to change a cursor. We will just view an example of doing that.

  1. Start Microsoft Visual C++ .Net or MS Visual Studio .Net and create a new MFC Application named ChangeCursor
  2. Create it as Dialog Based
  3. Design or import two cursors. We will consider that one of cursors has an ID as IDC_CURSOR1 and the other has an ID as IDC_CURSOR2
  4. Add two buttons to the dialog box. Change the ID of the first to IDC_FIRST_BTN and its Caption to First. Change the ID of the other to IDC_SECOND_BTN and its Caption to Second
  5. Add another button to the dialog box. Change its ID to IDC_DEFAULT_BTN and its Caption to Default
  6. Double-click the first button and implement its event as follows:
     
    void CChangeCursorDlg::OnBnClickedFirstBtn()
    {
    	// TODO: Add your control notification handler code here
    	SetClassLong(m_hWnd,
    		      GCL_HCURSOR,
    	                     (LONG)LoadCursor(AfxGetInstanceHandle(),
      				  MAKEINTRESOURCE(IDC_CURSOR1)));
    }
  7. Double-click the second button and implement its event as follows:
     
    void CChangeCursorDlg::OnBnClickedSecondBtn()
    {
    	// TODO: Add your control notification handler code here
    	SetClassLong(m_hWnd,
    	                      GCL_HCURSOR,
    	                     (LONG)LoadCursor(AfxGetInstanceHandle(),
     				  MAKEINTRESOURCE(IDC_CURSOR2)));
    }
  8. Double-click the Default button and implement its event as follows:
     
    void CChangeCursorDlg::OnBnClickedDefaultBtn()
    {
    	// TODO: Add your control notification handler code here
    	SetClassLong( m_hWnd,
    	                       GCL_HCURSOR,
    	                      (LONG)LoadCursor(NULL, IDC_ARROW) );
    }
  9. Test the application
     
    Changing Cursors
  10. Close
 
 

Home Copyright © 2002-2005 FunctionX, Inc.