Solve the Problem of flickering when updating data in the list box (VC is a simple method to prevent the flickering of windows and their controls (such as clistctrl) (a group of useful macros ))

Source: Internet
Author: User
VC is a simple method (a group of useful macros) to prevent the flickering of windows and their controls (such as clistctrl). This article mainly provides a solution through the previous article on the Internet, sorts out a set of macros to prevent flickering. This article focuses on displaying this macro and its usage. For the principle of solution, see: http://www.cndes.cn/Designs/Program/200608052716.html

The methods mentioned above for "effective division" are as follows:
The original author's idea is to rewrite the message processing function onerasebkgnd in wm_erasebkgnd,
Only use the specified background color to repaint the blank area. Do not re-paint the area where controls already exist.
Avoid flickering windows and their controls.

The preceding method works, but when there are many controls, the blank area that needs to be erased and re-painted in a window is calculated.
It is a very troublesome thing. To facilitate the practical application of this method, I wrote a set of macros to complete the "computing blank areas" function:
/*************************************** **************************************** ****************************/
/*************************************** *********************************/
/* Macro function: only blank areas other than the specified control are refreshed when the interface is refreshed. This effectively avoids window flickering.
/* Used in: wm_erasebkgnd message processing function onerasebkgnd ();
/*************************************** *********************************/
# Define erase_bkgnd_begin/
Crect bgrect ;/
Getwindowrect (& bgrect );/
Crgn bgrgn ;/
Bgrgn. createrectrgnindirect (bgrect );
// # Define erase_bkgnd_begin

// Marco parameter 'idcs 'specifies the identifier of the control
# Define add_noerase_control (IDC )/
{/
Crect controlrect ;/
Getdlgitem (IDC)-> getwindowrect (& controlrect );/
Crgn controlrgn ;/
Controlrgn. createrectrgnindirect (controlrect );/
If (bgrgn. combinergn (& bgrgn, & controlrgn, rgn_xor) = Error )/
Return false ;/
}

// Marco parameter 'noeraserect 'specifies a screen coordinates based rect,
// Which needn' t erase.
# Define add_noerase_rect (noeraserect )/
{/
Crgn noerasergn ;/
Noerasergn. createrectrgnindirect (noeraserect );/
If (bgrgn. combinergn (& bgrgn, & noerasergn, rgn_xor) = Error )/
Return false ;/
}

// Marco parameter 'pdc 'is a kind of (CDC *) type.
// Marco parameter 'clbrushcolor' specifies the color to brush the area.
# Define erase_bkgnd_end (PDC, clbrushcolor )/
Cbrush brush ;/
Brush. createsolidbrush (clbrushcolor );/
Cpoint saveorg = (PDC)-> getdomainworg ();/
(PDC)-> set1_worg (bgrect. topleft ());/
(PDC)-> fillrgn (& bgrgn, & brush );/
(PDC)-> setshortworg (saveorg );/
Brush. deleteobject ();/
// # Define erase_bkgnd_end
/*************************************** **************************************** ****************************/
Note:
1) Use the macro erase_bkgnd_begin and erase_bkgnd_end (PDC, clbrushcolor) together.
2) The macros add_noerase_control (IDC) and add_noerase_rect (noeraserect) are placed in the middle of the two macros as needed to add areas that do not need to be repainted (these areas cause flashes ), unlimited usage. The noeraserect parameter is the rect type or crect type of a screen coordinate system.

Example 1:
In the current form class, rewrite the wm_erasebkgnd message processing function as follows:
Bool cmywnd: onerasebkgnd (CDC * PDC)
{
Erase_bkgnd_begin;
Add_noerase_rgn (idc_button2 );
Add_noerase_rgn (idc_button1 );
Add_noerase_rgn (idc_list_stat );
Erase_bkgnd_end (PDC, getsyscolor (color_3dface ));
Return false;
}
The preceding idc_button2, idc_button1, and idc_list_stat are controls on the form.
You can specify other existing controls.
In this way, when the background is erased, the window will only be re-painted for the "blank area" after the above control, effectively avoiding blinking.

Note:
The method of reloading the message processing function onerasebkgnd of wm_erasebkgnd,
Choose View> classwizard> classinfo tab: Message filter drop-down box:
Select window, and then select the message maps tab. In the messages drop-down box, you can find
Wm_erasebkgnd. Double-click to add.

Example 2: Prevent clistctrl from flashing when pulling the window.
/*
* No further full-erasing is required,
* To prevent screen flashing caused by background erase and view repaint.
* Only erase the blank area.
*/
Bool cexlistctrl: onerasebkgnd (CDC * PDC)
{
// Compute the holding-data-items area of this list control
Crect rect;
Cpoint datargntopleftpoint;
Cpoint datargnbottomrightpoint;
Getitemposition (0, & datargntopleftpoint );
Getitemposition (getitemcount (), & datargnbottomrightpoint );
If (! Getheaderctrl ()-> getitemrect (getheaderctrl ()-> getitemcount ()-1, rect ))
Return clistctrl: onerasebkgnd (PDC );
Datargnbottomrightpoint. x = rect. Right;

Rect. setrect (datargntopleftpoint, (cpoint) (datargnbottomrightpoint-cpoint (2, 2 )));
Clienttoscreen (datargnrect );

// Compute and erase the blank area. Using the Marco.
Erase_bkgnd_begin;
Add_noerase_rect (datargnrect );
Erase_bkgnd_end (PDC, getbkcolor ());

Return false;
}

Note: When clistctrl is pulling, refresh the background with the background color before the meeting, and then draw the items with data on it,
The area without data maintains the background color. Therefore, if bool cexlistctrl: onerasebkgnd (CDC * PDC)
If the return false function is simple, the area without data is not displayed normally.
Therefore, in example 2, we first calculate the items region with data, which is a region that does not need to be replayed with the background.
Using the macro in this article can effectively avoid the flash of clistctrl during pulling.

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.