Skip to content

Load Cursor

A Win32 dialog example showing how to load cursors from system resources, compiled resources, and an .ani file on disk, then apply them with SetClassLong.

Published May 14, 1995

Three ways to get a cursor handle, all in one small dialog. LoadCursor with the predefined IDC_ARROW gives you a system cursor; LoadCursor with a resource ID gives you a cursor compiled into your .res; and LoadCursorFromFile loads straight from disk - here an animated .ani cursor, which Windows plays automatically when it is set. Each button swaps the window class cursor via SetClassLong(GCL_HCURSOR). Use this as the reference whenever you need custom or animated cursors in a raw Win32 app.

/////////////////////////////////////////////////////////////////////

BOOL CALLBACK MainDialog(HWND xHandle, UINT xMessage, WPARAM wParam, LPARAM lParam)
{
	static HWND TextEdit_hWnd;
	static HWND TextStatic_hWnd;

	static HMENU SystemMenu;

	static HCURSOR HandCursor;
	static HCURSOR ArrowCursor;
	static HCURSOR FileCursor;

	while(1) // Loop Forever
	{
		if(xMessage == WM_INITDIALOG)
		{
			MainDialog_hWnd = xHandle;

			SystemMenu = GetSystemMenu(MainDialog_hWnd,FALSE);
			AppendMenu(SystemMenu,MF_SEPARATOR,0,0);
			AppendMenu(SystemMenu,MF_STRING,1000,"www.NetWorkDLS.Com");
			AppendMenu(SystemMenu,MF_STRING,1001,"Version");

			SendMessage(MainDialog_hWnd,(UINT)WM_SETTEXT,(WPARAM)0,(LPARAM)TitleCaption);
			SendMessage(MainDialog_hWnd,WM_SETICON,TRUE,(LPARAM)LoadIcon(GetWindowInstance(MainDialog_hWnd),MAKEINTRESOURCE(300)));

			ArrowCursor = LoadCursor(MainInstance,IDC_ARROW);
			HandCursor = LoadCursor(MainInstance,MAKEINTRESOURCE(1000));
			FileCursor = LoadCursorFromFile("FileCursor.Ani");

			CenterWindow(MainDialog_hWnd);
			break;
		}

		if(xMessage == WM_COMMAND)
		{

			if(wParam==100)
			{
				SetClassLong(MainDialog_hWnd,GCL_HCURSOR,(LONG)ArrowCursor); //SetToArrow
			}
			if(wParam==200)
			{
				SetClassLong(MainDialog_hWnd,GCL_HCURSOR,(LONG)HandCursor);  //Set To Hand
			}
			if(wParam==300)
			{
				SetClassLong(MainDialog_hWnd,GCL_HCURSOR,(LONG)FileCursor);  //Set File Cursor
			}

			if(wParam==1500) //- About (Dialog Command)
			{
				ShowWindow(MainDialog_hWnd, SW_HIDE);
				DialogBox(MainInstance,MAKEINTRESOURCE(1000),MainDialog_hWnd,AboutDialog);
				ShowWindow(MainDialog_hWnd, SW_SHOW);
			}

			if(wParam==2000) //- Exit (Dialog Command)
			{
				KillTimer(MainDialog_hWnd,1);
				EndDialog(MainDialog_hWnd,0);
			}
			break;
		}

		if(xMessage == WM_SYSCOMMAND)
		{
			if(LOWORD(wParam)==1000) //- NetWorkDLS.Com (SysMenu Command)
			{
				ShellExecute(0,"Open","Http://www.NetWorkDLS.com",NULL,NULL,SW_SHOWNORMAL);
			}
			if(LOWORD(wParam) == 1001) //- Version (SysMenu Command)
			{
				MessageBox(MainDialog_hWnd, FileVersion, TitleCaption, 0 + MB_ICONASTERISK + MB_TASKMODAL);
			}
		}

		if(xMessage == WM_CLOSE) //Exit (Windows Message)
		{
			EndDialog(MainDialog_hWnd,0);
		}

		break;
	}
	return 0;
}

Notes: SetClassLong affects every window of that class, so for per-window cursors use SetWindowLong with GWL_HCURSOR instead. On 64-bit builds replace both with SetClassLongPtr/SetWindowLongPtr.

An unhandled error has occurred. Reload 🗙