In the previous article, we looked at how lockwindowupdate works, and today I'm going to talk about what this function can do.
In fact, LockWindowUpdate was designed to be reduced to one word: dragging. On this topic, I will press the table first. The lockwindowupdate function allows the application to temporarily take over responsibility for drawing windows.
Of course, in order to do this, you have to block the window process (or anyone else) from doing normal drawing activities;Otherwise, two segments (usually the one who draws the window and the one that tries to take over) fight for control of the window, and you end up with an ugly user interface because neither side knows what the other is doing.
But how do you draw the window if you've locked the update window?The answer is: use the dcx lockwindowupdate flag for the getdcex function.
This flag means, "Let me draw to the window, even though it's locked for updating." Of course, this flag should only be passed if the window is locked for an update, otherwise it will cause a conflict that lockwindowupdate avoids in the first place. Since people love ** so much, I've made a ** that summarizes what happens when the lock window is updated:
In other words, when a window is locked for updating, the ability to draw to the window is removed from the normal DC capture functions (BeginPaint and Friends) and given to GetDC(DCX LockWindowUpdate).
Note that if the window is not locked for update, the dcx lockwindowupdate flag should not be passedThe purpose of the flag is to indicate "I'm the one who called LockWindowUpdate. Let me in (draw the window)!”
It's a bit like the window manager in old-fashioned comedy: you tell the guards, "No one is allowed into this room. "Then you come back an hour later and the guards won't let you in. "I'm sorry, sir, but I'm not allowing anyone to enter this room. "But I'm the one who told you not to let anyone into the room. "Yes, sir, I follow your orders. No one is allowed in the room. ”
The error is in the first command to the window administrator. You should say, "No one is allowed into this room except me." "dcx lockwindowupdate is like: how do you tell the window administrator, "It's me." Let me in. ”
If you look back at how the lockwindowupdate function works, you can see that if the locked window doesn't try to draw, nothing will fail when the window is unlocked. While the cs s**ebits class style automatically saves the original pixels and restores them when the window is removed from the screen, lockwindowupdate doesn't do anything like that.
When you call lockwindowupdate(null), it's your responsibility to ensure that any pixels that changed when the lock window was updated have been restored to their original values. This is usually done by saving the original pixels to an off-screen bitmap before making a custom draw and putting them back when you're done.
Ok, this is the expected use case for lockwindowupdate:
If you want to take over the drawing from another window, call LockWindowUpdate on that window. > Save the pixels in the window you want to overwrite. > Draw new pixels. (These pixels are usually modifications of the original pixels.) For example, you can add an image of an object dragged on that window. > repeat as needed as long as your action is still in progress. (If you want to modify a different area of the screen than you originally modified, you may need to "back up" more pixels of the screen to do so.) You can perform this backup incrementally with a restore. For example, instead of accumulating the set of pixels that need to be restored, you can restore all the original pixels, calculate the new position of the dragged image, save those new pixels, and then draw the new image. This way, you only need to deal with a set of "backup pixels". When the operation is complete, restore the original pixel and call lockWindowUpdate(null).
Next time, we'll look more at the word "drag" and how it is closely related to the whole concept of lockwindowupdate. Although we're just getting started talking about lockwindowupdate, you should already have enough knowledge to answer this question.
Note: The purpose of today's article is to describe how LockWindowUpdate is used, not to discuss whether it's a good design in the first place. )
Summary. So, if you want to improve performance by temporarily disabling UI drawing, don't consider using LockWindowUpdate. Because it wasn't designed for that purpose at all, but unfortunately, a lot of people misunderstand how to use it by looking at the name of the function.
At last. Raymond Chen's "The Old New Thing" is one of my favorite blogs, and it has a lot of little knowledge about Windows, which is really helpful for Windows developers. **how is lockwindowupdate meant to be used?》