By peter Y
The Code has been optimized recently. After the test, the results are still good, but the interface will flash. Specifically, the TreeView control will flash, the language is C #, And the IDE is VS2005. After reading some materials and using some basic technologies (such as enabling dual-buffering), I found that the performance was ineffective.
Therefore, the Profiler tool is used to find out the bottleneck in the EndUpdate operation of the interface after each update (this is used to reduce the number of page updates, but this is not ideal because there are many elements in the control ), I guess every update ,. net underlying layer will update and repaint each element, so the speed will be slow, resulting in flickering. However, the use of double buffering should have better results. Looking at the code again, we may find that the update operation is too frequent, which reduces the speed and improves, but still does not work.
Continue online, and finally find a suitable solution. It turns out that the canvas is cleared and re-drawn at the underlying layer each time. This is the main cause of flickering. Therefore, the message sending function operation is reloaded to disable this message. The Code is as follows:
Protected override void WndProc (ref Message m)
{
If (m. Msg = 0x0014) // disable background message clearing
Return;
Base. WndProc (ref m );
}
Successful!
Note: Dual-buffering is useful when updates are not frequent and the number of elements in the control is not very large. Once there are too many elements, each update takes a long time. Even if dual buffering is used, the problem still cannot be solved. In my opinion, the ideal method is to disable background message clearing.
Appendix: record of failed attempts
1) Use setStyle
On the Internet, you can use the setStyle function to set the parameters of the control, specifically:
SetStyle (ControlStyles. UserPaint | ControlStyles. AllPaintingInWmPaint | ControlStyles. OptimizedDoubleBuffer, true );
These three option parameters depend on the former and must coexist; otherwise, they are invalid. This function itself is protected, so you must inherit a control before using it.
This goal is consistent with the preceding solution. It also prevents background clearing and dual buffering. However, you need to use the user rendering option, which is all for user plotting. This requires you to plot all controls by yourself, which is troublesome. Therefore, this method is not completely unavailable, but requires additional work and is not recommended. I am not using it either.
2) use BeginUpdate and EndUpdate
These operations have a good effect on scenarios where you need to update controls in batches. For example, a large number of nodes are added in batch during initialization. The disadvantage is that it cannot be updated immediately. Therefore, it is not applicable to scenarios where frequent node updates and immediate display of the interface are required. If you use it and do not disable clearing the interface message, the control will appear to flash continuously, and the content is mostly white-based and almost invisible (depending on the complexity of the video ). Because all interface updates are completed at EndUpdate, too many operations lead to a long blocking time for EndUpdate and are cleared first. After the update, the interface looks blank for a long time.
3) Use ControlStyles. EnableNotifyMessage Option
The role of this option is the same as that of the correct solution. The usage is as follows:
SetStyle (ControlStyles. EnableNotifyMessage, true );
Protected override void onNotifyMessage (Message m)
{
// Write the message filtering code here
}
However, the actual experiment shows no effect. I don't know why, so I didn't go into details.