Do not call functions that can trigger activity transfer in wm_activate.

Source: Internet
Author: User

2012-7-8

Cswuyg

Do not go to wm_activateCall the function that can trigger the activity Transfer

I. Source

Wm_activate was detected in June 19. It was strange that wm_activate was received twice in the window. However, if wm_activate was not found, it was not investigated in depth. A few days ago, my colleague found that a button was clicked and did not respond because the wm_lbuttondow message was lost and the button event was not generated. Only then did he find that wm_activate had a problem and finally found the cause. But why? This requires detailed analysis.
Ii. Detailed Analysis

Avoid calling functions that may trigger activity transfer in wm_activate (such as showwindow (sw_hide ));

There is a window a that is displayed. Click the B window that uses the same message thread as window a, so that window a is hidden due to activity loss, if showwindow (sw_hide) is called in the response of the wm_activate message in window a, Windows B may not receive wm_lbuttondown. Therefore, do not call showwindow (sw_hide) in wm_activate ). Postmessage should be used to store the showwindow in other places.

In this case, you can use a window to simulate a menu. This menu has a switch effect and a hidden effect of activity loss.

HypothesisThe menu window isMenuwndWhich has the same UI thread as the menu windowMain WindowMainwnd.

The error process is as follows:

In the menu window, when menuwnd is displayed, click the customer area of mainwnd in the main window. At this time, the menu window receives the wm_activate and wm_activate messages and provides the hidden showwindow (sw_hide) in the menu window. At this time, the activity has not been set to the main window (because the menu window uses the same message queue as the main window, the processing of the menu window is blocked (refer to wm_activate in msdn). At this time, the activity is still in the menu window, after it is hidden, wm_activate is triggered again, so the menu window receives wm_activate for the second time. At this time, showwindow (sw_hide) will not be executed (because it is hidden), so there will be no endless loops, then, the main window is used to process wm_activate to obtain the activity. After showwindow (sw_hide) is executed, return the result. Continue to process the first wm_activate and return the result.

The message stream is like this:

Wm_activate in the menu window --> hide in the menu window --> wm_activate in the main window --> return a response to the event in the menu window --> wm_lbuttonup in the main window.

Originally, the system thought that an activity message will be retrieved from the main window and an activity response will be returned from the menu window, because window a loses activity message, window a loses activity return, and window B obtains activity, these three pairs exist, but they are not captured by spy ++. I only pay attention to the main window: The wm_activate in the main window is lost, and the wm_lbuttondown in the main window is also lost.

If you are not clicking the main window area,But other process windows in different UI threads, so the menu window showwindow (sw_hide) will not trigger wm_activate, then there will be no problem.

I can guess why the system no longer sends wm_activate to the main window? It may be because the message that has been sent once is later than this wm_activate update, the message will be discarded, and the wm_lbuttondown associated with it will also be discarded.Guess: When this happensWm_activateWhen you reset the settings, undistributed mouse clicks are lost.

Figure: spy ++ capture, in the wrongCode, Click a button in the main window, how to switch from the menu window to the main window (Button window), the main window (Button window) did not receive the wm_lbuttondow message

 

In addition, you can use SYP ++ to view the test result.ProgramThe order in which the system sends messages, which may be like this (click the non-customer zone): wm_nclbuttondown--> Wm_activate --> wm_lbuttonup is generated in the order of or like this (click the customer area): wm_activate --> wm_lbuttondown --> wm_lbuttonup. The message wm_nclbuttondown is earlier than wm_activate.

Attach simple verification code

Http://files.cnblogs.com/cswuyg/%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%EF%BC%8CActivateTest.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.