• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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;
}
0
Piak
Asked:
Piak
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now