C++ SetCursor()有时在快速移动鼠标时失败

C++ SetCursor()有时在快速移动鼠标时失败,c++,windows,winapi,cursor,sfml,C++,Windows,Winapi,Cursor,Sfml,我目前正在SFML/C++应用程序中更改Windows鼠标光标,如下所示: #include <iostream> #include <SFML/Graphics.hpp> #include <Windows.h> int main() { sf::RenderWindow window; window.create(sf::VideoMode(1280, 720), "Window", sf::Style::Close); static int

我目前正在SFML/C++应用程序中更改Windows鼠标光标,如下所示:

#include <iostream>
#include <SFML/Graphics.hpp>
#include <Windows.h>

int main() {
  sf::RenderWindow window;
  window.create(sf::VideoMode(1280, 720), "Window", sf::Style::Close);
  static int cursorNum = 0;

  HCURSOR cursor = LoadCursorFromFile("graphics\\cursors\\cursor.cur");
  HCURSOR cursorTarget = LoadCursorFromFile("graphics\\cursors\\cursor-target.cur");

  while (window.isOpen()) {
    if (window.hasFocus()) {

      //...Irrelevant that decides the value of cursorNum...

      ////Mouse Cursors////
      if (sf::IntRect(0, 0, 1280, 720).contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)) {
        switch (cursorNum) {
          case 0:
            SetCursor(cursor);
            SetClassLongPtr(window.getSystemHandle(), GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(cursor));
          break;
          case 1:
            SetCursor(cursorTarget);
            SetClassLongPtr(window.getSystemHandle(), GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(cursorTarget));
          break;
        }
      }
      /////////////////////
    }

    ////Clear////
    window.clear(sf::Color(30, 0, 30));
    /////////////

    window.display();
  }

  sf::Event event;
  while (window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) {
      window.close();
    }
  }

  return 0;
}
#包括
#包括
#包括
int main(){
sf::RenderWindow窗口;
create(sf::VideoMode(1280720),“window”,sf::Style::Close);
静态int cursorNum=0;
HCURSOR cursor=LoadCursorFromFile(“graphics\\cursors\\cursor.cur”);
HCURSOR cursorTarget=LoadCursorFromFile(“graphics\\cursors\\cursor target.cur”);
while(window.isOpen()){
if(window.hasFocus()){
//…决定cursorNum价值的无关因素。。。
////鼠标光标////
if(sf::IntRect(0,01280720).contains(sf::Mouse::getPosition(window.x,sf::Mouse::getPosition(window.y)){
开关(cursorNum){
案例0:
设置游标(游标);
SetClassLongPtr(window.getSystemHandle(),GCLP_HCURSOR,reinterpret_cast(游标));
打破
案例1:
设置光标(光标目标);
SetClassLongPtr(window.getSystemHandle(),GCLP_HCURSOR,reinterpret_cast(cursorTarget));
打破
}
}
/////////////////////
}
////清楚的////
透明(sf::颜色(30,0,30));
/////////////
window.display();
}
sf::事件;
while(window.pollEvent(事件)){
如果(event.type==sf::event::Closed){
window.close();
}
}
返回0;
}

除了我快速移动鼠标外,它工作得非常好。除了自定义鼠标光标外,我还可以看到默认的windows鼠标光标。为什么会出现这种情况?

如果要动态更改光标,则需要在窗口进程中处理
WM\u SETCURSOR
通知。更改光标作为对
WM_MOUSEMOVE
的响应可能太晚了(在您的情况下可能会发生这种情况)。我不确定在SFML中应该如何处理
WM_SETCURSOR
,也许您需要隐藏系统光标并自己绘制一个。

我不确定您在描述什么。请在您的答案中添加一个代码示例好吗?每个windows都通过它的。当系统即将更新光标时,它会发送
WM_SETCURSOR
通知,如果您想立即更新光标,则需要处理该通知
WindowProc
可能在SFML的内部,因此您可能需要隐藏系统光标并自己绘制一个。