Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the flicker problem of C# control

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the flicker problem of C#control". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to solve the flicker problem of C#control" together.

Recently made some code optimization, after the test effect can also be, but found that the interface will flash, specifically TreeView control will flash, language for C#, IDE for VS2005. After consulting some information and using some basic techniques (such as turning on double buffering), it was found that there was no effect.

So use the Profiler tool to find out that the bottleneck lies in the EndUpdate operation of the interface every time it is updated (this is used to reduce the number of interface updates, but it is not ideal because there are many elements in the control). Guess that every time you update, the bottom layer of. Net will update and redraw each element, so the speed will be slow, causing flicker. But if so, double buffering should work better. Looking at the code again, I found that the update action may be too frequent, so I reduced the speed and improved, but still not good.

Continue to search online, and finally find a more appropriate program. It turns out that the bottom redraw clears the canvas every time, and then redraws it all again. This is the main reason for the flicker. So overload the message sending function operation and disable this message. The code is as follows:

protected override void WndProc(ref Message m) { if (m.Msg == 0x0014) //disable clearing background messages return; base.WndProc(ref m); }

Success!

Note: Double-buffering is useful when updates are not frequent and the control does not contain an excessive number of elements. Once there are too many elements, each update time is relatively long, even if double buffering is used, the flicker problem cannot be solved. Personally, I think the best way is to ban background messages.

Note: Some tried and failed records

1) Use setStyle

Some people on the Internet say that use setStyle function to set the parameters of the control, specifically:

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);

The latter of these three option parameters depends on the former and must coexist, otherwise it is invalid. And this function itself is protected, so you need to inherit a control first and then use it.

This goal is consistent with the correct solution above, which is to disable background clearing and enable double buffering, but requires the user drawing option to be used, and it is all left to the user to draw. This requires their own control to achieve all the drawing, more trouble. So this method is not completely impossible, but it requires extra work and is not recommended. I didn't use it either.

2) Use BeginUpdate and EndUpdate

This pair of operations has a good effect on situations where batch operations are required to update controls, such as adding a large number of nodes in batches during initialization. The downside is that it can't be updated instantly. Therefore, it is not suitable for frequent update nodes and want to reflect them to the interface immediately. If you use it and don't disable the Clear UI message, the controls will appear to blink constantly, and the content will be mostly white and almost invisible (depending on the complexity of the video). Because the interface updates are all completed at EndUpdate, too many operations cause EndUpdate to block for too long, and clear first, and update later, causing the interface to look blank for a long time.

3) Use ControlStyles.EnableNotifyMessage option

The effect of this option is consistent with the correct solution. How to use:

SetStyle(ControlStyles.EnableNotifyMessage, true); protected override void onNotifyMessage(Message m) { //write filter message code here} Thank you for reading, the above is the "C#control flicker problem how to solve" content, after learning this article, I believe we have a deeper understanding of how to solve the problem of C#control flicker, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report