Sign in Join
Wiki copytemporary

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

Revision 2 to 3. 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-
141## Warning and overview
152
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).
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).
174
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.
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.
196
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.
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.
218
229## Why are TFont, TPen, TBrush faster than accessing the LCL interfaces directly?
2310
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.
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.
2613
27-Probably you will only get more speed by accessing directly the interfaces (the gtk, the winapi, the qt, ...).
14+Probably you will only get more speed by accessing directly the interfaces (the gtk, the winapi, the qt, ...).
2815
29-The only reason to access the LCL interfaces is to quickly port some 'optimized' Delphi code.
16+The only reason to access the LCL interfaces is to quickly port some 'optimized' Delphi code.
3017
3118## Device Context
3219
33-Normally a Device Context (DC) is a handle (=LCL interface object) to draw on a TWinControl handle. A TCanvas.Handle is a DC.
20+Normally a Device Context (DC) is a handle (=LCL interface object) to draw on a TWinControl handle. A TCanvas.Handle is a DC.
3421
3522## GetDC
3623
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.
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.
3825
3926## ReleaseDC
4027
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.
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.
4229
4330## CreateFontIndirectEx
4431
4532## SelectObject
4633
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-```
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
4937FontHandle1:=CreateFontIndirect...;
50- OldFontHandle:=HFONT(SelectObject(aDC,FontHandle1));
51- ... some text operations ...
52- SelectObject(aDC,OldFontHandle);
53- DeleteObject(FontHandle1);
38+OldFontHandle:=HFONT(SelectObject(aDC,FontHandle1));
39+... some text operations ...
40+SelectObject(aDC,OldFontHandle);
41+DeleteObject(FontHandle1);
5442```
5543
56-
57-
5844## DeleteObject
5945
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.
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.
6147
6248## SaveDC
6349
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.
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.
6551
6652## RestoreDC
6753
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.
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.
6955
70-Example:
71-```
56+Example:
57+
58+``` pascal
7259Font:=CreateFontIndirect...;
73- DCIndex:=SaveDC(DC);
74- SelectObject(DC,Font);
75- RestoreDC(DCIndex);
76- DeleteObject(Font);
60+DCIndex:=SaveDC(DC);
61+SelectObject(DC,Font);
62+RestoreDC(DCIndex);
63+DeleteObject(Font);
7764```
7865
7966---
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).*
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

Back to history