Some applications, like drawing programs, use various
mouse cursors depending on what is going on. Unlike most other libraries,
the MFC doesn't make it easy to change a cursor. We will just view an
example of doing that.
- Start Microsoft Visual C++ .Net or MS Visual Studio .Net and create
a new MFC Application named ChangeCursor
- Create it as Dialog Based
- Design or import two cursors. We will consider that one of cursors
has an ID as IDC_CURSOR1 and the other has an ID as IDC_CURSOR2
- Add two buttons to the dialog box. Change the ID of the first to IDC_FIRST_BTN
and its Caption to First. Change the ID of the other to IDC_SECOND_BTN
and its Caption to Second
- Add another button to the dialog box. Change its ID to IDC_DEFAULT_BTN
and its Caption to Default
- Double-click the first button and implement its event as follows:
void CChangeCursorDlg::OnBnClickedFirstBtn()
{
// TODO: Add your control notification handler code here
SetClassLong(m_hWnd,
GCL_HCURSOR,
(LONG)LoadCursor(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_CURSOR1)));
}
|
- Double-click the second button and implement its event as follows:
void CChangeCursorDlg::OnBnClickedSecondBtn()
{
// TODO: Add your control notification handler code here
SetClassLong(m_hWnd,
GCL_HCURSOR,
(LONG)LoadCursor(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_CURSOR2)));
}
|
- Double-click the Default button and implement its event as follows:
void CChangeCursorDlg::OnBnClickedDefaultBtn()
{
// TODO: Add your control notification handler code here
SetClassLong( m_hWnd,
GCL_HCURSOR,
(LONG)LoadCursor(NULL, IDC_ARROW) );
}
|
- Test the application
- Close
|
|