65.9K
CodeProject is changing. Read more.
Home

Create a cursor from an ICON

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (4 votes)

Jan 5, 2011

CPOL
viewsIcon

16294

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);