简体   繁体   中英

How can I stop a parent control's repaints from jumping in line, in front of its child controls' repaints?

I'm building a custom control that has its own set of headers.

The headers (another custom control) exist as a child control of the aforementioned custom control.

When I resize the headers (part of the child control), the child contol is calling Invalidate() on itself, which should queue a message to repaint.

After the child contol has been invalidated, the parent control is notified that the header has resized, and it issues its own Invalidate() command.

This is shown by this trace:

Headers.Invalidate()
List.Invalidate()
Headers.Invalidate()
List.Invalidate()

However, when it comes time to start repainting, the parent control receives its Paint event first, followed by the header control:

List.Paint
Headers.Paint

When I move the mouse rapidly, the mouse move events appear to be queued in front of the paint events, so the invalidates continue to be called, but the parent control's paint events always fire first:

List.Paint
List.Paint
List.Paint
List.Paint
Headers.Paint
List.Paint
List.Paint
Headers.Paint

This causes the header control to lag behind the list substantially. Ideally, both paint events would fire together, and all mouse move events would queue before the paint events (so that the painting is always in sync, but the mouse events are all flushed out in between paints.)

I have no idea where to start on fixing this issue.

Where do I start, to fix the lag that my header experiences?

The answer is to make the redrawing synchronous by manually drawing, using the Update or Refresh methods.

The Update method will simply repaint the controls and all children synchronously.

The Refresh method will invalidate the control and all children, then will call Update .

are you already using double buffering? It might be slower, but it should synchronize the repaints so that both appear to update when an update happens.

You can switch on double buffering in a Form with:

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

Also, you might want to try:

    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.Opaque, true);
    SetStyle(ControlStyles.SupportsTransparentBackColor, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM