Search code examples
c++windowswinapicursor

Win32: Create a class with standard cursor (IDC_CROSS)


Win32 newbie here. I've been having some issues with using one of the standard WinAPI cursors: IDC_CROSS. When creating a new class, I set the class cursor to be IDC_CROSS, a crosshair. However, when I run the program my cursor never changes and is always the standard arrow.

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINSCREENCAP));
    wcex.hCursor        = LoadCursor(NULL, IDC_CROSS);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WINSCREENCAP);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

I'm using VC++ 2010 Express, and this happens even when I create a new (non-empty) Win32 project and change only the class cursor value. Is there something horribly obvious that I'm missing?


Solution

  • The window class cursor is set correctly. You may want to check that the class registration succeeds (RegisterClassEx returns a non-zero value), and that the window is actually created based on this class registration. The main thing to watch out for is that the CreateWindow function is called with the same window class name and HINSTANCE value. You could also check that LoadCursor actually returns a non-zero value. If LoadCursor fails for some reason it returns zero and Windows will default to the standard cursor.