Sign in Join
Wiki copytemporary

Device Contexts and GDI objects in the LCL interfaces: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1+## Contents
2+
3+ * 1 Warning and overview
4+ * 2 Why are TFont, TPen, TBrush faster than accessing the LCL interfaces directly?
5+ * 3 Device Context
6+ * 4 GetDC
7+ * 5 ReleaseDC
8+ * 6 CreateFontIndirectEx
9+ * 7 SelectObject
10+ * 8 DeleteObject
11+ * 9 SaveDC
12+ * 10 RestoreDC
13+
114## Warning and overview
215
3-This page is about the graphic Handle objects of the LCL interfaces: **Device contexts** (alias DC, canvas handles) and **GDI objects** (alias font handles, pen handles, brush handles, bitmap handles, palette handles, region handles).
16+This page is about the graphic Handle objects of the LCL interfaces: **Device contexts** (alias DC, canvas handles) and **GDI objects** (alias font handles, pen handles, brush handles, bitmap handles, palette handles, region handles).
417
5-The TCanvas, TFont, TBrush, TPen use them too and handle all the bookkeeping automatically. So, it is better and often faster to use the LCL classes instead of the LCL interface handles.
18+The TCanvas, TFont, TBrush, TPen use them too and handle all the bookkeeping automatically. So, it is better and often faster to use the LCL classes instead of the LCL interface handles.
619
7-The LCL interfaces work all different here, especially if you use these functions not like TCanvas, TFont, TPen, TBrush and TBitmap do. If you miss a function, please ask first on the mailing list, before trying these functions.
20+The LCL interfaces work all different here, especially if you use these functions not like TCanvas, TFont, TPen, TBrush and TBitmap do. If you miss a function, please ask first on the mailing list, before trying these functions.
821
922## Why are TFont, TPen, TBrush faster than accessing the LCL interfaces directly?
1023
11-- They use resource caches. This means if you allocate a font with the same attributes a second time, it will reuse the handle. These caches work global. So, even if you free a TFont, the handle is still in the cache and can be used if you create a TFont with the same attributes.
12-- The LCL interfaces are tested and optimized for these objects.
24+ * They use resource caches. This means if you allocate a font with the same attributes a second time, it will reuse the handle. These caches work global. So, even if you free a TFont, the handle is still in the cache and can be used if you create a TFont with the same attributes.
25+ * The LCL interfaces are tested and optimized for these objects.
1326
14-Probably you will only get more speed by accessing directly the interfaces (the gtk, the winapi, the qt, ...).
27+Probably you will only get more speed by accessing directly the interfaces (the gtk, the winapi, the qt, ...).
1528
16-The only reason to access the LCL interfaces is to quickly port some 'optimized' Delphi code.
29+The only reason to access the LCL interfaces is to quickly port some 'optimized' Delphi code.
1730
1831## Device Context
1932
20-Normally a Device Context (DC) is a handle (=LCL interface object) to draw on a TWinControl handle. A TCanvas.Handle is a DC.
33+Normally a Device Context (DC) is a handle (=LCL interface object) to draw on a TWinControl handle. A TCanvas.Handle is a DC.
2134
2235## GetDC
2336
24-For delphi compatibility the parameter 0 creates a DC for the current screen with some default values. Otherwise it creates a DC for the given TWinControl handle. The LCL interface provides default GDI objects (font, pen, brush, region, palette and bitmap). The current GDI objects can be changed with SelectObject and should be restored before releasing the DC, either with SelectObject or RestoreDC. Otherwise the DeleteObject can fail, which can create mem leaks.
37+For delphi compatibility the parameter 0 creates a DC for the current screen with some default values. Otherwise it creates a DC for the given TWinControl handle. The LCL interface provides default GDI objects (font, pen, brush, region, palette and bitmap). The current GDI objects can be changed with SelectObject and should be restored before releasing the DC, either with SelectObject or RestoreDC. Otherwise the DeleteObject can fail, which can create mem leaks.
2538
2639## ReleaseDC
2740
28-This frees the DC. It is allowed to release a DC, where the default GDI objects have not been restored. The LCL interface will now free the default GDI objects of the DC. If the application selected the default GDI objects of a DC1 in another DC2 and frees DC1 - which is a bug in the application - the behavior depends on the LCL interface. The winapi interface uses global stock objects and will ignore the error. The gtk interface will raise an exception.
41+This frees the DC. It is allowed to release a DC, where the default GDI objects have not been restored. The LCL interface will now free the default GDI objects of the DC. If the application selected the default GDI objects of a DC1 in another DC2 and frees DC1 - which is a bug in the application - the behavior depends on the LCL interface. The winapi interface uses global stock objects and will ignore the error. The gtk interface will raise an exception.
2942
3043## CreateFontIndirectEx
3144
3245## SelectObject
3346
34-This replaces a GDI object in a DC and returns the old GDI object. You can select a GDI object in any number of DC at the same time. The application should unselect the GDI object before calling DeleteObject. For example:
35-
36-``` pascal
47+This replaces a GDI object in a DC and returns the old GDI object. You can select a GDI object in any number of DC at the same time. The application should unselect the GDI object before calling DeleteObject. For example:
48+```
3749FontHandle1:=CreateFontIndirect...;
38-OldFontHandle:=HFONT(SelectObject(aDC,FontHandle1));
39-... some text operations ...
40-SelectObject(aDC,OldFontHandle);
41-DeleteObject(FontHandle1);
50+ OldFontHandle:=HFONT(SelectObject(aDC,FontHandle1));
51+ ... some text operations ...
52+ SelectObject(aDC,OldFontHandle);
53+ DeleteObject(FontHandle1);
4254```
4355
56+
57+
4458## DeleteObject
4559
46-This frees a GDI object. If a GDI object is freed, that is still selected in a DC, it fails and returns 0. This means every LCL interface must bookkeep if a GDI object is selected in a DC.
60+This frees a GDI object. If a GDI object is freed, that is still selected in a DC, it fails and returns 0. This means every LCL interface must bookkeep if a GDI object is selected in a DC.
4761
4862## SaveDC
4963
50-This saves the current state of a DC to an internal stack. The references to the GDI objects are stored, not their values. See below RestoreDC.
64+This saves the current state of a DC to an internal stack. The references to the GDI objects are stored, not their values. See below RestoreDC.
5165
5266## RestoreDC
5367
54-This loads a state from the internal DC stack. It undoes all SelectObject calls. Although the winapi supports restoring several states at once, most LCL interfaces will not support this.
55-
56-Example:
68+This loads a state from the internal DC stack. It undoes all SelectObject calls. Although the winapi supports restoring several states at once, most LCL interfaces will not support this.
5769
58-``` pascal
70+Example:
71+```
5972Font:=CreateFontIndirect...;
60-DCIndex:=SaveDC(DC);
61-SelectObject(DC,Font);
62-RestoreDC(DCIndex);
63-DeleteObject(Font);
73+ DCIndex:=SaveDC(DC);
74+ SelectObject(DC,Font);
75+ RestoreDC(DCIndex);
76+ DeleteObject(Font);
6477```
6578
6679---
67-*From the Free Pascal wiki, page "Device Contexts and GDI objects in the LCL interfaces", as it stood on 2020-02-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
68-Category: Graphics
69-Category: LCL
80+*From the Free Pascal wiki, page "Device Contexts and GDI objects in the LCL interfaces", as the Internet Archive kept it on 2024-09-13. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history