gpt4 book ai didi

winapi - 移动鼠标 block WM_TIMER 和 WM_PAINT

转载 作者:行者123 更新时间:2023-12-03 22:30:12 25 4
gpt4 key购买 nike

在我正在处理的应用程序中,在某些情况下,应用程序运行速度很慢,在这些情况下,我发现我的移动鼠标、计时器/绘画消息没有得到处理。如果我缓慢移动鼠标,我可以无限期地阻止重新绘制窗口!

我发现这是expected behaviour :

With the exception of the WM_PAINT message, the WM_TIMER message, and the WM_QUIT message, the system always posts messages at the end of a message queue. This ensures that a window receives its input messages in the proper first in, first out (FIFO) sequence. The WM_PAINT message, the WM_TIMER message, and the WM_QUIT message, however, are kept in the queue and are forwarded to the window procedure only when the queue contains no other messages. In addition, multiple WM_PAINT messages for the same window are combined into a single WM_PAINT message, consolidating all invalid parts of the client area into a single area. Combining WM_PAINT messages reduces the number of times a window must redraw the contents of its client area.



但是,我能做些什么呢?有时为了直接响应鼠标移动,我需要尽快重新绘制。

我在我的 CWnd 派生类上通过这样的方法捕获消息:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

最佳答案

WM_PAINTWM_TIMER ,正如您在文档中阅读的那样,与其他消息不同。

实际的区别是它们是最低优先级的消息。也就是说,如果队列中有任何其他消息,它们将不会被处理。

也就是说,您正在移动鼠标,因此发布了很多消息,但是这些消息的频率通常很低(每秒几十个),因此您的程序应该大部分是空闲的,但事实并非如此,可能是因为其中一些消息花费的时间比预期的要长得多,并且阻塞了队列。你只需要检测哪一个以及为什么。

一些与鼠标相关的消息,我不记得了:

  • WM_MOUSEMOVE
  • WM_NCMOUSEMOVE
  • WM_SETCURSOR
  • WM_NCHITTEST

  • 并通过网络搜索:
  • WM_MOUSEOVER
  • WM_MOUSELEAVE
  • WM_NCMOUSEOVER
  • WM_NCMOUSELEAVE

  • 无论如何,如果您想立即重绘,无需等待 WM_PAINT您应该调用 UpdateWindow() .此函数强制立即处理 WM_PAINT (当然,如果有任何无效的东西)并阻塞直到它完成,绕过该消息的低优先级问题。

    更新 :根据您评论中的情况,我认为您的最佳解决方案可能是以下几点:
  • WM_MOUSEMOVE将光标位置保存在成员变量中并设置一个标志,表示鼠标已移动。
  • 实现 OnIdle()检查鼠标移动标志的处理程序。如果不动,什么也不做。如果移动,则进行昂贵的计算。
  • 您可以尝试调用或不调用 UpdateWindow()来自 OnIdle()看看哪个更好。

  • 是的,在慢速计算机中仍然会感觉不稳定,但自从 OnIdle()WM_TIMER 的优先级更低和 WM_PAINT ,这些消息不会被无限期地降级。更重要的是,您不会对昂贵函数的多次调用进行排队。

    关于winapi - 移动鼠标 block WM_TIMER 和 WM_PAINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039413/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com