简体   繁体   中英

Can't change the cursor (from inside OnSetCursor)

I'm trying to change the cursor inside my CStatic -derived class by handling OnSetCursor

class CMyStatic : public CStatic
{
    // ....
};

BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
    ON_WM_SETCURSOR()
END_MESSAGE_MAP()

BOOL CMyStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
    TRACE(_T("OnSetCursor\n"));
    SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
    return TRUE;
}

OnSetCursor gets called every time I move the mouse, but the cursor isn't changed. What am I doing wrong?

use ::SetCursor. Or you can call SetCursor once elsewhere and don't capture WM_SETCURSOR, and the cursor will be set automatically.

The CWnd::SetCursor you used is for setting a cursor for a window, and that cursor will be used if you don't override OnSetCursor. That is, the default behavior of OnSetCursor is calling ::SetCursor with the cursor one set by calling CWnd::SetCursor.

You don't need (or want) to use MAKEINTRESOURCE in this case, so change:

SetCursor(AfxGetApp()->LoadStandardCursor(MAKEINTRESOURCE(IDC_CROSS)));

To:

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
m_hHandCursor = LoadCursor(NULL ,MAKEINTRESOURCE(IDC_HAND));

将m_hHandCursor设置为成员变量,然后使用OnInitDialog中的win32 API LoadCursor进行初始化。...然后,OnSetCursor()事件始终使用SetCursor API设置此光标。...因此,您的应用程序将始终获取系统定义的HAND光标

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM