Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Create a cursor from an ICON

4.64/5 (4 votes)
5 Jan 2011CPOL 16.3K  
This tip is about creating a cursor from an icon
HICON hIcon = AfxGetApp()->LoadStandardIcon(IDI_INFORMATION);
				
ICONINFO iconinfo;				
GetIconInfo(hIcon, &iconinfo);

ICONINFO cursorinfo;
cursorinfo = iconinfo;
cursorinfo.fIcon = FALSE;

if(m_hCursor) // make sure m_hCursor the class member variable is initialzed to NULL 
{
   ::DestroyCursor(m_hCursor);
}
m_hCursor = ::CreateIconIndirect(&cursorinfo);			

// must delete the created bitmaps due to GetIconInfo call
::DeleteObject(iconinfo.hbmMask);
::DeleteObject(iconinfo.hbmColor);

// Don't forget to destroy this cursor before application exiting

::SetCursor(m_hCursor);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)