HOWTO: Change the Mouse Pointer for a Window in MFCLast reviewed: July 10, 1997Article ID: Q131991 |
The information in this article applies to:
SUMMARYIn a Windows-based application, a window is always created based on a window class. The window class identifies several characteristics of the windows based on it, including the default mouse pointer (cursor). In some cases, an application may want to change the pointer associated with certain windows that it creates. This article describes three methods an MFC application can use to display different pointers at different times.
MORE INFORMATIONHere are some situations when you might want an MFC application to display different pointers at different times:
Three MethodsHere are three ways an application can change the mouse pointer in a window:
Code to Illustrate the Three MethodsThe following code shows by example how to change the mouse pointer of a CView derived class window by using the three methods. m_ChangeCursor is a member variable of CMyView class and is of type BOOL. It indicates whether a different pointer type needs to be displayed.
Method OneChange the mouse pointer for the CMyView object by overriding CWnd::OnSetCursor() function. Use ClassWizard to establish the message map function CMyView::OnSetCursor() for Windows message WM_SETCURSOR and supply the body of the function as follows:
BOOL CMyView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { if ( m_ChangeCursor ) { ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT)); return TRUE; } return CView::OnSetCursor(pWnd, nHitTest, message); } Method TwoRegister your own window class containing the desired mouse pointer using either the AfxRegisterClass() or AfxRegisterWndClass() function. Then create the view window based on the registered window class. For more information on registering window classes in MFC, please see MFC Tech Note 1, "Window Class Registration."
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { cs.lpszClass = AfxRegisterWndClass( CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, // use any window styles AfxGetApp()->LoadStandardCursor(IDC_WAIT), (HBRUSH) (COLOR_WINDOW + 1)); // background brush return CView::PreCreateWindow(cs) } Method ThreeCall the BeginWaitCursor() and EndWaitCursor() functions to change the mouse pointer. NOTE: CWinApp::DoWaitCursor(1) and CWinApp::DoWaitCursor(-1) work similarly to BeginWaitCursor() and EndWaitCursor(), respectively.
void CMyView::PerformLengthyOperation() { BeginWaitCursor(); // or AfxGetApp()->DoWaitCursor(1) //... EndWaitCursor(); // or AfxGetApp()->DoWaitCursor(-1) } NOTE: If calls to BeginWaitCursor() and EndWaitCursor() are not in the same handler, you must override OnSetCursor as follows: BOOL CMyView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { if (m_ChangeCursor) { RestoreWaitCursor(); return TRUE; } return CView::OnSetCursor(pWnd, nHitTest, message); }In this example, set m_ChangeCursor to TRUE just before the call to BeginWaitCursor(), and set it back to FALSE after the call to EndWaitCursor(). |
Keywords : kbprg kbui MfcUI kbhowto
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |