Technical Information Database TI1375D.txt Avoid using Resource Heap with Tabbed Notebooks Category :General Programming Platform :All Product :Delphi All Description: Intended Audience: ------------------ Anyone who wants to necessarily avoid using User Resource heap in conjunction with TTabbedNotebooks. Prerequisites: -------------- - Familiarity with the TTabbedNotebook and TTimer components. - Understanding of Microsoft Windows Architecture; specifically User resources. Purpose of the TI: ------------------ This document demonstrates how to use Object Pascal to help control the number of active handlesWindows, the User Resource heap specifically, keeps track of. Why does this matter? Simplifying things, Windows keeps track of every Focusable Control via a Handle. The issue is,Windows cannot simply maintain an inexhaustible amount of Windows Handles (4 byte pointers) and this is where this TI's sample code will help "ease" the resource load which might restrict the Delphi Developer. The User DLL actually is the Library which allocates and maintains resources for all Windows and related data structures, including focusable object's and other unmentioned, but nonetheless important USER functions, under Windows. It is this USER DLL resource limitation* which we are temporarily working around. An example of an added resource load is, every control we add to a form, takes up 4 bytes of the USER 64k. With this in mind, what exactly are we doing? We will be destroying** the Handles which Windows is architecturely designed to remember. By destroying these Handles, thus freeing USER resource drain, it does not mean we need to recreate said objects again. On the contrary, currently built into the VCL is the ability to keep track of said objects, which are in fact pointers to structures. So, knowing the VCL will maintain a Handle and windows will recreate a new Handle as NEEDED, instead of maintaining one permanently, as designed, we can take control of the USER resources manually and "ease" the overall USER resource load. This TI will demonstrate the freeing of USER Handle resources via Delphi's TTabbedNoteBook (specifically destroying Page Handles), Delphi's DestroyHandle (TWinControl procedure for removing USER handles), and the Windows API call LockWindowUpdate (Locking unwanted repaints). The technique of freeing a TTabbedNoteBook Page Handle can be extended to anyTWinControl descendant. TWinControl is the ancestor class which introduces the creating/destroying of Windows Handles; CreateHandle & DestroyHandle. * 64K for Win3.1 & 64K for Win95 16-bit subsytem alone. For further information, contact Microsoft or look in the MSDN. ** As a side effect of destroying said Handles, the TTabbedNotebook used in this TI will experience faster page movement. SAMPLE CODE The following attached events are direct excerpts from a Project with a TTimer,TTabbedNotebook (with multiple pages) and an assorted plethora of controls on each notebook page. (The later is to emphasize the benefits of adding the below code) The attached Event snippit's should reside in the OnTimer event of the TTimer control and the OnChange event of the TTabbedNotebook, respectively. With no further adou, let's try our new code:-------------- ... Implementation Type TSurfaceWin = class(TWinControl); procedure TForm1.Timer1Timer(Sender: TObject); begin {This code will update the Form caption with the percentages of free SYSTEM, GDI, &USER for Windows.} caption := 'SYSTEM: ' + inttostr(getfreesystemresources(GFSR_SYSTEMRESOURCES)) + ' GDI: ' + inttostr(getfreesystemresources(GFSR_GDIRESOURCES)) + ' USER: ' + inttostr(getfreesystemresources(GFSR_USERRESOURCES)); end; procedure TForm1.TabbedNotebook1Change(Sender: TObject; NewTab: Integer; var AllowChange: Boolean); begin {LockWindowUpdate prevents any drawing in a given window} LockWindowUpdate(handle); {The reason for TSurfaceWin is because the DestroyHandle call is declared abstract in TWinControl, which means, only descendant classes can surface this Procedure. The rest of the line is meant to flag the current page of the TabbedNotebook and destroy its handle as we move to another page. NOTE: Even though we destroy the handle, Windows itself remembers the page object and will reassign/create a new one when the tab is once more clicked to. } TSurfaceWin(TabbedNotebook1.pages.objects[tabbedNotebook1. pageindex]).DestroyHandle; {Release the Lock on the Form so any Form drawing can work} LockWindowUpdate(0); end; Reference: 7/16/98 4:34:10 PM
Last Modified: 01-SEP-99