如何更改button上的光标?

static HWND btn; HCURSOR cursor = LoadCursor(0, IDC_CROSS); case WM_CREATE: btn = CreateWindow(TEXT("BUTTON"), TEXT("Press Me"), WS_CHILD|WS_VISIBLE, 50, 50, 80, 30, hwnd, (HMENU) 111, NULL, NULL); 

现在,在WM_COMMAND ,我尝试使用:

 SendMessage(btn, WM_SETCURSOR, 0, (LPARAM) cursor); 

哪个不行

那么点击button后,如何更改鼠标的光标呢? 另外,当鼠标hover在button上时,如何更改鼠标的光标(例如,当鼠标hover在网页浏览器的链接上时,它会变成一只手)?

Solutions Collecting From Web of "如何更改button上的光标?"

使用此代码更改单个控件的光标:

 SetWindowLong(btn, GCL_HCURSOR, (LONG)cursor); 

64位兼容性的首选方法是:

 SetWindowLongPtr(btn, GCL_HCURSOR, (LONG_PTR)cursor); 

您应该在WM_SETCURSOR消息处理程序中使用SetCursor()

 LRESULT CALLBACK DlgProc (HWND hWndDlg, UINT message, WPARAM wParam, LPARAM lParam) { static RECT rcButton; static HWND hButton; static HCURSOR hCursor; // ... switch (message) { case WM_INITDIALOG: hButton = GetDlgItem (hWndDlg, IDC_BUTTON); GetClientRect (hButton, &rcButton); MapWindowPoints (hButton, hWndDlg, (LPPOINT) &rcButton, 2); break; case WM_MOUSEMOVE: if ((LOWORD (lParam) >= rcButton.left - 1 && LOWORD (lParam) <= rcButton.right) && (HIWORD (lParam) >= rcButton.Top - 1 && (HIWORD (lParam) <= rcButton.bottom)) SetClassLong (hButton, GCL_HCURSOR, (LONG) LoadCursor (NULL, IDC_CROSS)); else SetClassLong (hButton, GCL_HCURSOR, (LONG) LoadCursor (NULL, IDC_ARROW)); break; // ... 

它会确保你的工作!

另外,当鼠标悬停在按钮上时,如何更改鼠标的光标(例如,当鼠标悬停在网页浏览器的链接上时,它会变成一只手)?

您可以对按钮进行超类化,并创建一个具有手形图标的新类别的按钮。

在BUTTON类上使用GetClassInfoEx,更改游标,给它一个新的类名,注册它,并使用新的类名创建CreateWindowEx按钮。