when using a Windows application developed by VC Program , you can customize the mouse and cursor on the window in four steps, manually edit cursor, define cursor variables, load cursor variables, and control cursor behavior. The detailed description of each step is as follows:
(1) manually edit cursor. Add a cursor in the resource editor, and VC will automatically assign it an ID (for example, idc_cursor1), then manually edit the shape of the cursor, edit it, and save it. You will see that there is a file name entry in the cursor attribute box that indicates the file path of the cursor Resource (RES \ cursor1.cur)
(2) defines the cursor variable. Defines a hcursor type variable mycursor to store the cursor object.
hcursor mycursor;
(3) load the cursor variable. Add the Code of the cursor to the function that you need to change the mouse cursor to load the defined cursor resource to the previous cursor variable, if you want to automatically display the custom mouse cursor at the beginning of the program, load the cursor in the initialization function (for example, oninitdialog in the dialog box initialization function. There are many ways for VC to load cursor. You can use cwinapp: loadcursor to load it, or you can use loadcursor in Windows API to load it. However, both methods can only change the shape of cursor, but cannot change the size. If you need to change the size, you can only use LoadImage. The Code is as follows:
mycursor = (hcursor) LoadImage (null, "Res/cursor1.cur", image_cursor, lr_loadfromfile );
This is a complicated Windows API function with six parameters. You can load bitmap, icon, and cursor to the memory. The function prototype is as follows:
handle LoadImage (
hinstance hinst,
| lpctstr lpszname,
uint utype,
int cxdesired,
int cydesired,
uint fuload);
The following describes only the usage of parameters related to loading cursor:
Hinst: indicates the module for which the resource is to be loaded. (I am not quite sure about the exact function of the module, whether it is a program, a process, or a library ), however, you can set this parameter to null.
Lpszname: identifies the resource to be loaded. If the fuload parameter is set to lr_loadfromfile, this parameter is the resource file name, such as a bitmap file or an icon file, and also includes a cursor file.
Utype: indicates the type of the loaded resource. The value can be image_bitmap. The bitmap can be loaded; image_cursor. The cursor can be loaded; image_icon can be loaded.
Cxdesired: the width of the cursor. The value is an integer in pixels.
Cydesired: the length of the cursor. The value is an integer in pixels.
Fuload: The method for loading resources. When the value is lr_loadfromfile, it refers to loading from a file.
(4) control the cursor behavior. The final control of cursor behavior should be implemented through the onsetcursor function. Use classwizard to change the window object of the mouse cursor (CWIN subclass) and add the onsetcursor function to process the on_wm_setcursor message of the window. When the program runs, as long as the mouse enters the window area, Windows will send the on_wm_setcursor message to the window object and trigger your onsetcursor function. Therefore, you can add the logic to control the mouse and cursor in the onsetcursor function. For example, you can simply display your personalized cursor setcursor (mycursor). You can also add some complicated logic, for example, the personalized cursor is displayed in a certain area and the standard cursor is displayed in other areas, which is determined by actual needs.