How do I use a custom cursor?

I am trying to change the default OS pointer to a custom one. At the moment I only deal with Windows. I got the image file with the cursor I want (.png, should I change the format?). All in all, I have searched and tried to just change the cursor, with no success. Also, at the moment I am looking for the simplest solution with the minimum number of codes.

If needed:
-I am using a window created with SFML (2.1).
- The following compilations, but not relevant:

HCURSOR hCursor = LoadCursor(NULL, "path/filename.png");
SetCursor(hCursor);

      

So I'm looking for community knowledge, any ideas?

The following works: However, it immediately reverts back to the default mouse:

HCURSOR hCursor = LoadCursorFromFile("path/filename.cur");
SetCursor(hCursor);

      

I found this LINK which seems to have the same problem as mine.
However I cannot apply the answer given in the link

HWND windowHandle;
int GCL_Hcursor = -12; //GCL_HCURSOR
HCURSOR hCursor = LoadCursorFromFile("Graphics/Cursors/Pointer_small.cur");
SetCursor(hCursor);
SetClassLong(windowHandle, GCL_Hcursor, (DWORD)hCursor);

      

I (obviously?) We get:

using uninitialized local variable 'windowHandle'

+3


source to share


2 answers


After about 4 hours and 30 minutes trying to get a custom mouse to work with SFML on Windows, I finally managed to complete a task for which I expected to take no more than 5-10 minutes. So I leave here the answer to my own question as the internet hasn't been able to provide it clean and understandable for a noob like me. May it be useful to those in need.

#include <SFML/Graphics.hpp>
#include <windows.h>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Simple Cursor Demonstration");

    // {This is what matters}
    { 
        sf::WindowHandle wHandle;
        wHandle = window.getSystemHandle();
        HCURSOR Cursor = LoadCursor(NULL, IDC_HAND); // IDC_ARROW IDC_WAIT IDC_HAND...  http://msdn.microsoft.com/en-us/library/ms648391%28v=vs.85%29.aspx
        //HCURSOR Cursor = LoadCursorFromFile("path/filename.cur"); //.cur or .ani
        SetCursor(Cursor);
        SetClassLongPtr(wHandle, GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(Cursor));
    }


    // to prove it works, just move the mouse around
    // not 100% sure the following actually proves it, but the above worked wonders on the project I wanted it for
    window.clear(sf::Color(sf::Color(0, 255, 0))); 
    window.display();
    sf::sleep(sf::milliseconds(3000));

    return 0; //I've read this line or equivalent is a good idea... :)
}

      



Sources:

-This solution has been plundered from all over the web, but mostly from Difficult for Noob , which was also mentioned by the one who deleted his answer, Being [Overcomplicated for Noob], it seems to provide excellent information on how to properly implement custom cursors in the program and also how to do it on the Apple OS thingy instead - This was also helpful.
-colinsmith said the cursor file should be .cur or .ani, .png doesn't really work.

+4


source


  • Creating a cursor from a bitmap is described here http://www.codeproject.com/Articles/5220/Creating-a-color-cursor-from-a-bitmap Converting a png to a bitmap can easily be done using CImage. Just upload PNG and detach HBITMAP. But only Bitmap is not a cursor.
  • Cursors are set on a window that accepts WM_SETCURSOR. Therefore, "replacing" a specific cursor will not work. You must intercept the WM_SETCURSOR message in order to change the cursor to be returned.
  • Your Edit2 is wrong because you need the correct window handle to change the cursor in the window class. But this will never work if the window is handling the cursor on its own (see 2)


PS: It would be better to inform yourself about how Windows handles cursors before you ask how to change it globally ...

0


source







All Articles