How to dynamically set the mouse pointer in a dialog box-based program

Source: Internet
Author: User

This article aims to demonstrate how to use a single mouse pointer on the button (or other control) of the dialog box. There are two ways to change the pointer in Windows Programming: one is to provide a full cursor pointer (HCURSOR) for the WNDCLASS structure when the main window class of the application is registered ), another method is to process the WM_SETCURSOR message in the program to set the mouse cursor. Standard MFC should use the first method to automatically set the cursor pointer to an arrow when registering the main window. If you want to change the cursor pointer, You can reset the mouse pointer by reloading the message WM_SETCURSOR processing function in the main window or subwindow.

// handle WM_SETCURSOR in button class BOOL CMyButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT msg) {::SetCursor(m_hMyCursor);return TRUE;}

When you move the cursor over the button, the mouse is not captured. Windows sends a WM_SETCURSOR message to the button. From the above OnSetCursor code, we can see that it passes a parameter that is the window handle (pWnd)-that is, the window pointed by the mouse pointer, here refers to the button itself; the second parameter passed by OnSetCursor is nHitTest. This is a test code that starts with HTXXX and is used for WM_NCHITTEST messages; the third parameter passed by OnSetCursor is the message ID of the mouse message that triggers the event-for example, WM_MOUSEMOVE. WM_SETCURSOR is a message specifically used to set the mouse pointer. After the mouse pointer is set, it should be set to TRUE to Prevent Windows from performing default processing.

The Processing Mechanism of WM_SETCURSOR is as follows. If a parent window exists, the default window process first sends the WM_SETCURSOR message to the parent window. If the parent window processes the WM_SETCURSOR message (TRUE is returned ), windows does not do anything redundant, and the message processing ends. If the parent window does not process the WM_SETCURSOR message (that is, FALSE is returned), Windows will allow the child window to process WM_SETCURSOR. If the child window does not do any processing (FALSE is returned), Windows will use the full-path optical pointer, if no full cursor pointer exists, the arrow pointer is used.

If you use a dynamic cursor pointer in a program, you must decide whether to process the WM_SETCURSOR message in the subwindow or the WM_SETCURSOR message in the parent window. The two methods have their own advantages and disadvantages, depending on the specific situation. Generally, objects are allowed to determine their own behavior attributes-that is, it is best to process them in the subwindow. The subwindow in this article is a button. This requires a new button class, which has its own message ing and message processing process. You can use the Class Wizard to generate a new button class, but there are too many things to do. If you already have your own button class, you must process the WM_SETCURSOR message in your button class. If you don't have your own button class and want to be lazy, you can process the WM_SETCURSOR message in the dialog box, just don't tell the object-oriented experts that I told you to do this.

This article comes with a simple dialog box-based program, MyCursor. This example demonstrates two methods to change the cursor. If you move the mouse over the button ("OK" and "Cancel"), the cursor changes to a blue finger, which is implemented by processing OnSetCursor in the dialog box. In addition, if you move the mouse pointer over an underlined hyperlink, the cursor pointer becomes a commonly used finger in another Windows system. It is implemented in the window subclass. CStaticLink is a common reusable class. Among them, the large majority code processing of CStaticLink: OnSetCursor extracts hand pointers from the win32hlp.exe executable program, which has little to do with the content about the cursor settings in this article, I have not listed all of them. If you are interested in details, you can download the sample program from the beginning of this article ...... finally, I wish you a pleasant programming!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.