Change of cursor

xiaoxiao2021-03-06  43

The Windows program and DOC programs are very different, and Windows has a bright graphics interface and a simple operation, and the mouse is the most important operating means in the Windows program.

Have a friend to consult with me how to modify the shape shape, this is a simple question, but when a friend is further, more in-depth operation mechanism, I found out more of the problems in it, returning home. Get some conclusions. Here is some modifications to the shape of the mouse under Windows, as well as some related APIs, talk about your own summary, I hope to help beginners can help.

Operating system: Windows2000.

Development environment: Visual C 6.0, MFC.

Example Related Clices: Document View Structure, CDOCTestView.

1: Modify method

Overload the onmousemove function in the CVIEW.

Overload method:

"View" -> "classwizard" -> Right side Messages column Select "WM_MOUSEMOVE", then click "Add Function", click "OK".

Add code:

Add the following code in cdoctestview :: onmousemove ():

Hcursor hcur = loadingcursor (null, idc_cross);

:: setCursor (HCUR);

Mechanism analysis:

The system defaults to the ONMOUSEMOVE function each time, you will reuse the cursor that comes with the program to re-draw the cursor, so it is necessary to modify the cursor in onMouseMove to make the cursor.

2: Two modifications

Overload the OnsetCursor function in CVIEW.

Overload method:

"View" -> "ClassWizard" -> Right side Messages column Select "WM_SETCURSOR", then click "Add Function", click "OK".

Add code:

Modify OnsetCursor as follows:

Bool cdoctestview :: Onsetcursor ()

{

Hcursor hcur = loadingcursor (null, idc_cross);

:: setCursor (HCUR);

Return True;

}

Mechanism analysis:

When the system is set to set the cursor, a WM_SETCURSOR message will be sent, which will trigger an OnsetCursor function, so you can add a modified cursor in this function.

3: Modification method three

Heavy dates the PrecreateWindow function in CVIEW.

Overload method:

"View" -> "classwizard" -> Right side Messages column Select "PreCreateWindow", then click "Add Function", click "OK".

Add code:

Add the following code in the CDOCTestView :: PrecreateWindow function:

Cs.lpszclass = AFXREGISTERWNDCLASS (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW,

LoadCursor (NULL, IDC_CROSS),

(Hbrush) (Color_Window 1));

Mechanism Analysis: When the default window is created, the field's properties are described, and the registration mechanism is used to register this window attribute to the system, and the AFXREGIsterWndClass can change the properties of the form itself, and re-it Register. 4: Modifying method four modified mouse shape in any function

In theory, use the setCursor (...) function, you should modify the mouse shape at any time, but the actual situation is not the case. Each time you call onMousemove (ie, the system is moved), the system calls the program default original one The cursor is re-drawing the mouse shape. So, if you modify the cursor in other places, you will find that this doesn't work, because each mouse moves, it returns to the original shape. Another API is required here. Implement this function setClasslong ().

You can modify the cursor shape at any part to modify the cursor shape:

SetClasslong (this-> getsafehwnd (),

GCL_HCURSOR,

(Long) loadcursor (null, idc_cross);

Comprehensive comparison:

The method three is more suitable for one-time replacement of the default cursor, and is not suitable for multiple frequent replacement.

Method 1. Method 2, multiple transformations of the cursor can be realized, but there is a need to add additional variables to the function to control the display as different cursors.

Method 4 is the most flexible way, can replace the mouse shape in any function at any time.

Note 1: LoadStandardCursor usage method.

LoadStandardCursor is used to load the cursor of the system, the call mode is as follows:

Hcursor hcursor = (hcursor) AFXGetApp () -> LoadStandardCursor (IDC_CROSS);

:: setCursor (HCURSOR);

2: loadcursor usage

HCURSOR LOADCURSOR (Hinstance Hinstance, // Handle To Application Instance

LPCTSTR LPCURSorname); // name or resource Identifier

LpCursorname refers to the name of the cursor.

Hinstance describes a module handle containing a cursor. This module can be an executable file or empty. When the module handle points to a file, LoadCursor gets cursors from this file. When the module handle is empty, LoadCursor Get the cursor from the system.

So if you want to load the cursor of the system, you can use the following code:

Hcursor hcur = loadingcursor (null, idc_cross);

:: setCursor (HCUR);

If you want to load the cursor you draw in your resource, you can use the following code:

Hcursor hcur = loadingcursor (AfxGetInstanceHandle (),

MakeintResource (IDC_CURSOR1));

:: setCursor (HCUR);

2004-12-28 by ricky

转载请注明原文地址:https://www.9cbs.com/read-74912.html

New Post(0)