2017-04-16 186 views
0

我目前更改Windows鼠標光標在我SFML/C++應用程序,像這樣有時會失敗:的setCursor()是在快速移動鼠標

#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; 
} 

當我快速移動鼠標時,此作品除了完美的罰款。除了我自定義的鼠標光標外,還有幾個可以看到默認窗口鼠標光標的稀缺實例。爲什麼會這樣呢?

回答

0

如果你想動態改變遊標,那麼你需要在你的窗口過程中處理WM_SETCURSOR通知。將光標作爲對WM_MOUSEMOVE的響應更改可能爲時已晚(可能發生在您的案例中)。我不知道如何在SFML中處理WM_SETCURSOR,也許你需要隱藏系統光標並自己繪製一個光標。

+0

我不確定你在描述什麼。你能否給你的答案添加一個代碼示例? – TrampolineTales

+0

每個窗口通過它的[WindowProc回調](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v = vs.85).aspx)接收消息。當系統即將更新遊標時,它會發送「WM_SETCURSOR」通知,如果要立即更新遊標,則需要處理該通知。 'WindowProc'可能在SFML的內容中,所以你可能需要隱藏系統光標並自己繪製一個。 – VTT