How to change mouse cursor when move mouse pass the button?

How to change mouse cursor to "IDC_IBEAM" when move mouse pass the button?
This is my easy win32 code, please edit or modify this code below. Thank you.

#include "afxwin.h"
#define IDC_BUTTON1 100

class CApp:public CWinApp{
public:
virtual BOOL InitInstance();
};

CApp app;
class CWin:public CFrameWnd{
CButton *button;
public:
CWin();
~CWin();
};

BOOL CApp::InitInstance(){
m_pMainWnd=new CWin();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
};

CWin::CWin(){
Create(NULL,"Hello",
WS_OVERLAPPEDWINDOW,CRect(0,0,200,200));
button=new CButton();
button->Create("OK",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(50,50,100,100),this,IDC_BUTTON1);
}

CWin::~CWin(){
delete button;
}
PiakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
button->Create(...);

::SetClassLong(button->GetSafeHwnd(), GCL_HCURSOR, (LONG)(::AfxGetApp()->LoadStandardCursor(IDC_IBEAM)));
0
nietodCommented:
You also need to override the button's OnSetCursor() procedure so that it can set the cursor usig ::SetCursor().
0
chensuCommented:
SetClassLong is sufficient. You don't have to override the button's OnSetCursor().
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
But set class long will change the cursor of all buttons that use that window class.  That is not likely to be what he wants.  (In MFC deriving a new MFC window class does not mean you are using a new windows API window class, right?)
0
chensuCommented:
Yes, SetClassLong will change the cursor of all buttons in your application. If you want to change the cursor of some certain buttons, derive your own button class from CButton and override OnSetCursor. Use either of these two techniques, but not both.


#include "afxwin.h"
#define IDC_BUTTON1 100
#define IDC_BUTTON2 101

class CMyButton : public CButton
{
protected:
    afx_msg BOOL OnSetCursor(CWnd *pWnd, UINT nHitTest, UINT message);
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyButton, CButton)
    ON_WM_SETCURSOR()
END_MESSAGE_MAP()

BOOL CMyButton::OnSetCursor(CWnd *pWnd, UINT nHitTest, UINT message)
{
    if (pWnd == this)
    {
        ::SetCursor(::AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
        return TRUE;
    }
    else
        return CButton::OnSetCursor(pWnd, nHitTest, message);
}


class CApp:public CWinApp{
public:
virtual BOOL InitInstance();
};

CApp app;
class CWin:public CFrameWnd{
CMyButton *button;
CButton b2;
public:
CWin();
~CWin();
};

BOOL CApp::InitInstance(){
m_pMainWnd=new CWin();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
};

CWin::CWin(){
Create(NULL,"Hello",
WS_OVERLAPPEDWINDOW,CRect(0,0,200,200));
button=new CMyButton();
button->Create("OK",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(50,50,100,100),this,IDC_BUTTON1);

b2.Create("Cancel",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(150,150,200,200),this,IDC_BUTTON2);

//::SetClassLong(button->GetSafeHwnd(), GCL_HCURSOR, (LONG)(::AfxGetApp()->LoadStandardCursor(IDC_IBEAM)));
}

CWin::~CWin(){
delete button;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.