AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt The VCL LockDrawing method in TWinControl
Thema durchsuchen
Ansicht
Themen-Optionen

The VCL LockDrawing method in TWinControl

Ein Thema von DP News-Robot · begonnen am 27. Mai 2022
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.403 Beiträge
 
#1

The VCL LockDrawing method in TWinControl

  Alt 27. Mai 2022, 16:10
I was going over my to do list earlier this week and found a not that indicated I should follow up to the blog post I did on new VCL features in Delphi and C++Builder 11 (https://blog.marcocantu.com/blog/202...res-rad11.html) about LockDrawing. In fact I wrote: "The base TWinControl class now offers LockDrawing and UnlockDrawing methods, to disable and control updating. This triggers the execution of the WM_SETREDRAW Windows message. This is probably worth its own blog post..." but than I totally forgot about this. So here is the blog post, with a few months delay.

Let's start from the Windows API. The message*WM_SETREDRAW*is send to a*window to "allow changes in that window to be redrawn, or to prevent changes in that window from being redrawn." Any time you need to perform a set of operations, each requiring to repaint, you can put them on hold and repaint only at the end, avoiding flickering and making the process faster. It is of course important to remove the lock and than call a special function to force repaint (RedrawWindow).

All of this*is properly encapsulated in two TWinControl methods added to the VCL library in RAD Studio 11,*LockDrawing and UnlockDrawing, which send the*WM_SETREDRAW message with proper parameters. They also implement a "lock count logic" so that if you lock the same control twice, you need to unlock it twice to resume painting. Additionally, the*UnlockDrawing*automatically calls the*RedrawWindow API.

Having said this, which are the use cases? I wrote a demo showing two scenarios, and you can also check this nice blog post by*Radek Cervinka,*https://delphi.cz/post/TWinControlLo...ckDrawing.aspx.

A first case in my demo is adding a bunch of elements to a list box. While Delphi traditionally offered and still has the ability to avoid repainting by "freezing" the string list update, it does make sense to lock the UI repaint instead. These are the two alternative coding styles:

var I: Integer;begin ListBox1.Items.BeginUpdate; try for I := 1 to 10000 do ListBox1.Items.Add('Line ' + I.ToString); finally ListBox1.Items.EndUpdate; end;var* I: Integer;begin* ListBox1.LockDrawing;* try* * for I := 1 to 10000 do* * * ListBox1.Items.Add('Line ' + I.ToString);* finally* * ListBox1.UnlockDrawing;* end; In both cases the performance is much higher (and visible noticeable) than without one of the two solutions. Another scenario is that of a set of changes to a component causing a repaint. This is not trivial to simulate in a simple example, but one way to to cause flicker "on purpose" with code like this:

var I: Integer;begin Panel1.LockDrawing; try for I := 1 to 10000 do begin Panel1.Color := clBlue; Panel1.Repaint; Panel1.Color := clRed; Panel1.Repaint; end; finally Panel1.UnlockDrawing; end; I know, this is fairly stupid, but you can see there is no flicker with the Lock/Unlock calls, while the effect look quite horrible without locking the UI. Not that I can easily render this in a screenshot, but here is the form at runtime anyway:





Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:20 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz