How do you set the coordinates of access points on a Windows cursor generated from an icon file?

I set the custom cursor in my application from the icon file, but the click point is in the wrong coordinates. I set the cursor with

SetClassLongPtr(hwnd, GCL_HCURSOR, reinterpret_cast<LONG_PTR>cursor) 

where the cursor is the result;

 LoadImage( NULL, "some_path/cursor.ico", IMAGE_ICON, //also tried IMAGE_CURSOR 0, //width. 0 uses the width of the file provided 0, //height. 0 uses the height of the file provided LR_LOADFROMFILE ); 

The cursor loads fine, but its clicks exit the lower left corner of the cursor image, not the top left.

The wikipedia article in .ico files says that hotspots are only specified in .cur files, not on .ico files.

Edit: Clarified Question


ref: LoadImage () and SetClassLongPtr () by msdn.

+2
source share
3 answers

You can do this with CreateIconFromResourceEx

You pass a pointer to CURSOR_RES_HDR as the first parameter. This is one of those structures you can find in the documentation, but it is not any header file that I can find. This is pretty simple, although mostly for 16-bit unsigned ints, followed by a BITMAPINFOHEADER containing cursor image data.

 typedef struct { WORD xHot; // x hotspot WORD yHot; // y hotspot BITMAPINFOHEADER bih; } CURSOR_RES_HDR; ... CURSOR_RES_HDR * pImage; // Fill out pImage HCURSOR hcur = CreateIconFromREsourceEx((BYTE*)pImage, cbImage, // size of image data + hotspot (in bytes) FALSE, 0x00030000, // version: value mandated by windows 0, 0, // width & height, 0 means use default LR_DEFAULTSIZE | LR_DEFAULTCOLOR); 
+4
source

Yes, the access point is determined by the contents of the .cur file. The Wikipedia article shows you this, offsets 4 and 6. Windows does not have an API to change the access point after loading the cursor. Just edit the cursor in Visual Studio or any other cursor editor, specify a hot spot and save the file as a .cur file.

+1
source

Source: https://habr.com/ru/post/893391/


All Articles