在快速鼠标移动期间,SetCursor()有时会失败

时间:2017-04-16 15:06:17

标签: 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 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;
}

除非我快速移动鼠标,否则此功能完全正常。除了我的自定义鼠标光标之外,还会有一些稀缺的实例,我可以看到默认的Windows鼠标光标。为什么会这样?

1 个答案:

答案 0 :(得分:0)

如果要动态更改光标,则需要在窗口过程中处理WM_SETCURSOR通知。将光标更改为对WM_MOUSEMOVE的响应可能为时已晚(在您的情况下可能会发生什么)。我不确定在SFML中应该如何处理WM_SETCURSOR,也许你需要隐藏系统光标并自己绘制一个。