Sign in Join
Wiki copytemporary

Lazarus Custom Drawn Controls: what changed

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

Title: Lazarus Custom Drawn Controls

OldNewLine
1+The **Lazarus Custom Drawn Controls** are a set of controls equivalent to the standard Lazarus controls, but which draw themselves. They can have many uses, including the ability to fully customize the drawing, the ability to have exactly the same look in different platforms and also a higher consistency of behavior.
2+
3+This set of controls is divided into two parts:
4+
5+- the custom drawn controls which are necessary for implementing Lazarus widgetsets and are located in the unit lazarus/lcl/customdrawncontrols.pas. These are described here.
6+- all other custom drawn controls, which are used often, but aren't indispensable to implement a LCL custom drawn widgetset. Those are located in the package lazarus/components/customdrawn. These are described in the page [[Lazarus Custom Drawn Package]]
7+
8+## How start using these components?
9+
10+To be able to use them in the Lazarus GUI designer install the package lazarus/components/customdrawn.lpk
11+
12+To only create customdrawn components by code one can simply add customdrawncontrols, customdrawndrawers and customdrawn_common to the uses clause, declare them and use them. Note that currently only the Win2000 drawer is in the LCL, so any other drawers need to be obtained from the package customdrawn.lpk
13+
14+It is important to note that styles are only available if you add the corresponding unit of the style to the uses clause.
15+
16+## How do these components work?
17+
18+The basic programming technique utilized by this set of controls is explained at the [[Developing with Graphics|Developing with Graphics#Create a custom control which draws itself]] page.
19+
20+The code of the controls itself is implemented in the unit customdrawncontrols.pas, but this unit has no drawing code at all. Customdrawncontrols.pas provides the code which processes all the control's keyboard and mouse events, implementing all the mouse and key response behaviour. Each control instance has a Drawer connected to it. A Drawer in our nomenclature (an instance of the TCDDrawer class) is similar to a theme rendering engine or suchlike. The unit customdrawndrawers.pas manages the list of all known drawers and it also declares all the basic data types needed for State and StateEx. These properties contain all the information about the control's current state, which is passed to the drawer so that it can have the information it requires to draw the control correctly. Only one instance of each drawer can exist in a program, and all controls refer to that single instance through the drawer's manager in customdrawndrawers.pas. Each control has a property called DrawStyle which allows you to choose (from an enumerated type) which draw style to use, and then customdrawndrawers.pas converts this information into a Drawer instance. You can specify the dsDefault DrawStyle which will then use the style specified in the global variable DefaultStyle. This was done to allow the style of all controls to be changed at once. Simply change this global variable and then call Invalidate on the controls. You can see below a code snippet from customdrawndrawers.pas which shows the methods which have been exposed to manage the list of drawers and also the Default style:
21+
22+``` pascal
23+unit customdrawndrawers;
24+//...
25+ TCDDrawStyle = (
26+ // The default is given by the DefaultStyle global variable
27+ // Don't implement anything for this drawer
28+ dsDefault = 0,
29+ // This is a common drawer, the base for all others. It implements the look from dsWin2000
30+ dsCommon,
31+ // Operating system styles
32+ dsWinCE, dsWin2000, dsWinXP,
33+ dsKDEPlastique, dsGNOME, dsMacOSX,
34+ dsAndroid,
35+ // Other special styles for the user
36+ dsExtra1, dsExtra2, dsExtra3, dsExtra4, dsExtra5,
37+ dsExtra6, dsExtra7, dsExtra8, dsExtra9, dsExtra10
38+ );
39+//...
40+procedure RegisterDrawer(ADrawer: TCDDrawer; AStyle: TCDDrawStyle);
41+function GetDefaultDrawer: TCDDrawer;
42+function GetDrawer(AStyle: TCDDrawStyle): TCDDrawer;
43+
44+var
45+ DefaultStyle: TCDDrawStyle = dsCommon; // For now default to the most complete one, later per platform
46+```
47+
48+Each Drawer class has methods to draw all controls and also all available primitives. All new drawers created by users should inherit from the class TCDCommonDrawer (which is the basic common drawer declared in the unit customdrawn_common.pas). If you don't inherit from TCDCommonDrawer your application might crash if it hits an abstract method. This is the most complete drawer, which implements all abstract methods from its base class, so inheriting from it guarantees that there will never be crashes owing to methods you have not yet implemented. Other drawers can override the desired methods to change the drawing style.
49+
50+The dsCommon drawer uses only TCanvas for all its rendering, but other themes might also use TLazIntfImage and TFPImageCanvas instead, in order to have a faster pixel access. This programming technique is described in [[Developing with Graphics|Developing with Graphics#Working with TLazIntfImage]].
51+
52+### Color Palette
53+
54+Drawers have also access to a color Palette, which has the usual LCL system colors. Default drawers will load their colors from the operating system via the LCL acording to the native platform. However, outside their native platform, drawers will use a standard color palette.
55+
56+``` pascal
57+ TCDColorPalette = class
58+ public
59+ ScrollBar, Background, ActiveCaption, InactiveCaption,
60+ Menu, Window, WindowFrame, MenuText, WindowText, CaptionText,
61+ ActiveBorder, InactiveBorder, AppWorkspace, Highlight, HighlightText,
62+ BtnFace, BtnShadow, GrayText, BtnText, InactiveCaptionText,
63+ BtnHighlight, color3DDkShadow, color3DLight, InfoText, InfoBk,
64+ //
65+ HotLight, GradientActiveCaption, GradientInactiveCaption,
66+ MenuHighlight, MenuBar, Form: TColor;
67+ end;
68+
69+ { There are 5 possible sources of input for color palettes:
70+ palDefault - Uses palNative when the operating system matches the drawer style,
71+ palFallback otherwise
72+ palNative - Obtain from the operating system
73+ palFallback - Use the fallback colors of the drawer
74+ palUserConfig-Load it from the user configuration files, ToDo
75+ palCustom - The user application has set its own palette
76+ }
77+ TCDPaletteKind = (palDefault, palNative, palFallback, palUserConfig, palCustom);
78+
79+ { TCDDrawer }
80+
81+ TCDDrawer = class
82+ protected
83+ public
84+ Palette: TCDColorPalette;
85+ constructor Create; virtual;
86+ destructor Destroy; override;
87+ procedure SetPaletteKind(APaletteKind: TCDPaletteKind);
88+ procedure LoadNativePaletteColors;
89+ procedure LoadFallbackPaletteColors; virtual;
90+```
91+
92+As the code above shows, you can choose where the colors should come from - either from the operating system current theme, from the drawer's default, or from a user-defined scheme provided by your application. This allows you to change all yor application's colors at runtime simply by changing the palette.
93+
94+## The Drawers
95+
96+### dsCommon
97+
98+This is the base drawer for all others and it implements a Win2000-look with some small improvements, like better focus drawings on controls
99+
100+### dsWinCE
101+
102+Imitates the Windows CE look, with some small improvements like better focus drawings on controls
103+
104+### dsWin2000
105+
106+Imitates the Windows 2000 look. Since dsCommon provides the Windows 2000 look already, this class implements no methods at all
107+
108+### dsWinXP
109+
110+Imitates the Windows XP look
111+
112+### dsKDEPlastique
113+
114+Imitates the look of the Plastique Qt/KDE Theme
115+
116+### dsAndroid
117+
118+Imitates the Android look
119+
120+### dsWindows7
121+
122+![](/wiki-images/0/09/Customdrawn_windowsxp.png) ![](/wiki-images/f/f5/Customdrawn_windows7.png) ![](/wiki-images/1/1c/Customdrawn_windows8.png) ![](/wiki-images/1/16/Customdrawn_windows11.png) ![](/wiki-images/b/b8/Customdrawn_3d.png)
123+
124+Imitates the Windows look. Currently is available only with BGRABitmap graphic library, and you can get this drawer in the BGRAControls package: [[BGRAControls]]
125+
126+### BGRAControls Custom Drawn default style
127+
128+<figure>
129+<img src="customdrawn_dark.png" title="customdrawn_dark.png" />
130+<figcaption>customdrawn_dark.png</figcaption>
131+</figure>
132+
133+## Controls which imitate the Standard Palette
134+
135+### TCDButton
136+
137+This is a fully custom drawn button.
138+
139+Usage example:
140+
141+``` pascal
142+uses customdrawnextras;
143+
144+procedure TForm1.FormCreate(Sender: TObject);
145+var
146+ MyButton: TCDButton;
147+begin
148+ MyButton := TCDButton.Create(Self);
149+ MyButton.Parent := Self;
150+ MyButton.DrawStyle := dsWin2000;
151+ MyButton.Left := 100;
152+ MyButton.Top := 100;
153+ MyButton.Width := 200;
154+ MyButton.Height := 50;
155+ MyButton.Caption := 'My Button';
156+ MyButton.Color := clRed;
157+ MyButton.OnClick := @HandleButtonClick;
158+end;
159+```
160+
161+<figure>
162+<img src="Custom_drawn_buttons.png" title="Custom_drawn_buttons.png" />
163+<figcaption>Custom_drawn_buttons.png</figcaption>
164+</figure>
165+
166+### TCDEdit
167+
168+This is a fully custom drawn edit control.
169+
170+<figure>
171+<img src="TCDEdit.png" title="TCDEdit.png" />
172+<figcaption>TCDEdit.png</figcaption>
173+</figure>
174+
175+### TCDCheckBox
176+
177+This is a fully custom drawn check box.
178+
179+<figure>
180+<img src="TCDCheckBox.png" title="TCDCheckBox.png" />
181+<figcaption>TCDCheckBox.png</figcaption>
182+</figure>
183+
184+### TCDRadioButton
185+
186+<figure>
187+<img src="TCDRadioButton.png" title="TCDRadioButton.png" />
188+<figcaption>TCDRadioButton.png</figcaption>
189+</figure>
190+
191+### TCDScrollBar
192+
193+<figure>
194+<img src="TCDScrollBar.png" title="TCDScrollBar.png" />
195+<figcaption>TCDScrollBar.png</figcaption>
196+</figure>
197+
198+### TCDGroupBox
199+
200+This is a fully custom drawn group box.
201+
202+<figure>
203+<img src="TCDGroupBox.png" title="TCDGroupBox.png" />
204+<figcaption>TCDGroupBox.png</figcaption>
205+</figure>
206+
207+## Controls which imitate the Additional Palette
208+
209+### TCDStaticText
210+
211+## Controls which imitate the Common Controls Palette
212+
213+### TCDTrackBar
214+
215+A substitute for TTrackBar
216+
217+<figure>
218+<img src="TCDTrackBar.png" title="TCDTrackBar.png" />
219+<figcaption>TCDTrackBar.png</figcaption>
220+</figure>
221+
222+### TCDProgressBar
223+
224+<figure>
225+<img src="TCDProgressBar.png" title="TCDProgressBar.png" />
226+<figcaption>TCDProgressBar.png</figcaption>
227+</figure>
228+
229+### TCDListView
230+
231+Under construction.
232+
233+### TCDTabControl
234+
235+A substitute for TTabControl
236+
237+### TCDPageControl
238+
239+A substitute for TPageControl
240+
241+<figure>
242+<img src="TCDPageControl.png" title="TCDPageControl.png" />
243+<figcaption>TCDPageControl.png</figcaption>
244+</figure>
245+
246+## Custom Drawn Packages
247+
248+Moved here: [[Lazarus Custom Drawn Package]]
249+
250+## Other good custom drawn components for Lazarus
251+
252+- [[VirtualTreeview]]
253+- [[ATTabs]]
254+- [[ATStatusBar]]
255+- [[ATScrollBar]]
256+
257+## Maintainers
258+
259+- Felipe Monteiro de Carvalho
260+- JiXian Yang
261+
262+## See Also
263+
264+- [[Lazarus Components Directory]]
265+- [[Lazarus Custom Drawn Package]]
266+- [[Custom Drawn Interface]]
267+
268+---
269+*From the Free Pascal wiki, page "Lazarus Custom Drawn Controls", as it stood on 2024-02-08 after a change by 007, 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).*

Back to history