6358|18

89

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

peekmessage的问题 [复制链接]

我用peekmessage 写了个函数来peek mouseup消息,但不知道为什么有时候会peek不到mouseup消息呢(只有一个客户的机器有这个问题,其他客户的机器是没这个问题的)

但实际上消息队列里面是肯定有mouseup消息的,因为我不调用peekmessage 的时候,我是能接收到mouseup消息的。

大家有遇到这种问题吗?

最新回复

看来差不多要揭贴了  详情 回复 发表于 2009-10-2 14:26
点赞 关注

回复
举报

76

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
那你的代码是怎么写的,贴出来大家看看,貌似才好分析问题...
 
 

回复

86

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
peek ()
{
while(1)
{
peekmessage(hwnd,&msg,0,0,pm_remove);

if(msg.message==mousemove)
{
return 1;
}

if(msg.message==mouseup)
{
return 0;
}
}
代码就类似上面的,peek 函数是 在 mousedown  里面调用的 、
 
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

4
 
消息是不是要转换一下.
 
 
 

回复

79

帖子

0

TA的资源

一粒金砂(初级)

5
 
是的,这只是大概代码
 
 
 

回复

67

帖子

0

TA的资源

一粒金砂(初级)

6
 
如何正确的使用PeekMessage()


  1. HOWTO: How to Use PeekMessage() Correctly in Windows
  2. SUMMARY
  3. In the Windows environment, many applications use a PeekMessage() loop to perform background processing. Such applications must allow the Windows system to enter an idle state when their background processing is complete. Otherwise, system performance, "idle-time" system processes such as paging optimizations, and power management on battery-powered systems will be adversely affected.

  4. While an application is in a PeekMessage() loop, the Windows system cannot go idle. Therefore, an application should not remain in a PeekMessage() loop after its background processing has completed.

  5. NOTE: The PeekMessage method described in this article is only needed if your application is a 32-bit application for Windows and is, for some reason, unable to create threads and perform background processing.

  6. MORE INFORMATION

  7. Many Windows-based applications use PeekMessage() to retrieve messages while they are in the middle of a long process, such as printing, repaginating, or recalculating, that must be done "in the background." PeekMessage() is used in these situations because, unlike GetMessage(), it does not wait for a message to be placed in the queue before it returns.

  8. An application should not call PeekMessage() unless it has background processing to do between the calls to PeekMessage(). When an application is waiting for an input event, it should call GetMessage() or WaitMessage().

  9. Remaining in a PeekMessage() loop when there is no background work causes system performance problems. A program in a PeekMessage() loop continues to be rescheduled by the Windows scheduler, consuming CPU time and taking time away from other processes.

  10. In enhanced mode, the Virtual Machine (VM) in which Windows is running will not appear to be idle as long as an application is calling the PeekMessage function. Therefore, the Windows VM will continue to receive a considerable fraction of CPU time.

  11. Many power management methods employed on laptop and notebook computers are based on the system going idle when there is no processing to do. An application that remains in a PeekMessage() loop will make the system appear busy to power management software, resulting in excessive power consumption and shortening the time that the user can run the system.

  12. In the future, the Windows system will make more and more use of idle time to do background processing, which is designed to optimize system performance. Applications that do not allow the system to go idle will adversely affect the performance of these techniques.

  13. All these problems can be avoided by calling the PeekMessage() function only when there is background work to do, and calling GetMessage() or WaitMessage() when there is no background work to do.

  14. For example, consider the following PeekMessage() loop. If there is no background processing to do, this loop will continue to run without waiting for messages, preventing the system from going idle and causing the negative effects described above.

  15.    // This PeekMessage loop will NOT let the system go idle.
  16.    for( ;; )
  17.    {
  18.       while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  19.       {
  20.          if (msg.message == WM_QUIT)
  21.             return TRUE;
  22.          TranslateMessage(&msg);
  23.          DispatchMessage(&msg);
  24.       }
  25.       BackgroundProcessing();
  26.     }

  27. This loop can be rewritten in two ways, as shown below. Both of the following PeekMessage() loops have two desirable properties:


  28. ? They process all input messages before performing background processing, providing good response to user input.  
  29. ? The application "idles" (waits for an input message) when no background processing needs to be done.  
复制代码
 
 
 

回复

65

帖子

0

TA的资源

一粒金砂(初级)

7
 
peekmessage(hwnd,&msg,0,0,pm_remove);


是不是把这个消息从消息队列里面移走了?
 
 
 

回复

64

帖子

0

TA的资源

一粒金砂(中级)

8
 
这个真是很奇怪。不过你一以看一下那个WTL,一般预处理消息都不这么写了。pm_remove这个好像用的也不对。
 
 
 

回复

63

帖子

0

TA的资源

一粒金砂(初级)

9
 
我用getmessage 是可以拿到mouseup消息的,但因为这个函数是等待的,这样我的程序就会不够流畅,大家还有好的办法吗?
 
 
 

回复

87

帖子

0

TA的资源

一粒金砂(初级)

10
 
还有其他办法可以比较流畅的拿到mouseup消息吗?
 
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

11
 
再创建一个子线程,你可以仔细考虑一下到底用主线程,还是子线程来等待...
 
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

12
 
你的意图是?父窗体在子控件上mouseup时do something?
如果是这样,可以自定义消息,在子控件的LBUTTONUP里头postmessage至父窗体
 
 
 

回复

71

帖子

0

TA的资源

一粒金砂(初级)

13
 
我是要做下拉滑块的效果,按下的时候,开始滑动,直到又up消息。
是在一个线程里面做的
 
 
 

回复

80

帖子

0

TA的资源

一粒金砂(初级)

14
 
while(!m_bIsup)
{
.......//滑动
}

LbuttonUp()
{
m_bIsup = true;
}
 
 
 

回复

74

帖子

0

TA的资源

一粒金砂(初级)

15
 
上面的做法对于做滑块是可以,但是我写的这个函数还需要用来移动地图,要平滑的移动,因此要检测 mousemove  
 
 
 

回复

62

帖子

0

TA的资源

一粒金砂(中级)

16
 
在窗体回调函数里面直接捕捉你要的消息,这种方式应该是最快的
 
 
 

回复

64

帖子

0

TA的资源

一粒金砂(初级)

17
 
看下MSDN上关于PEEKMESSAGE的说明:

PeekMessage retrieves only messages associated with the window identified by the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters. If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread making the call. (PeekMessage does not retrieve messages for windows that belong to other threads.) If hWnd is –1, PeekMessage only returns messages with a hWnd value of NULL, as posted by the PostThreadMessage function. If wMsgFilterMin and wMsgFilterMax are both zero, PeekMessage returns all available messages (that is, no range filtering is performed).

The WM_KEYFIRST and WM_KEYLAST constants can be used as filter values to retrieve all keyboard messages; the WM_MOUSEFIRST and WM_MOUSELAST constants can be used to retrieve all mouse messages.

The PeekMessage function does not remove WM_PAINT messages from the queue. WM_PAINT messages remain in the queue until they are processed.

按照上面的说明做2个简单测试就可以,是不是HANDLE不对?
 
 
 

回复

84

帖子

0

TA的资源

一粒金砂(初级)

18
 
handle 肯定是对的,我的程序是在很多机器上跑的,只有一个机器有这个问题
 
 
 

回复

67

帖子

0

TA的资源

一粒金砂(初级)

19
 
看来差不多要揭贴了
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表